2013-05-17 1 views
4

Spring 트랜잭션이 처음이다. 나는 Spring 3.2.2와 MySQL 5.5.20 (InnoDB)을 사용한다. 로그 파일에서 롤백되었음을 알 수 있지만 데이터베이스에서 여전히 9로 업데이트되는 레코드가 있습니다. 무엇을 놓쳤습니까? 감사. JDBC의 매우 간단한 Spring 트랜잭션이 롤백되지 않음 (심지어 로그가 yes라고 말함)

config.xml 파일 :

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://127.0.0.1:3306/javatest?useUnicode=true&amp;characterEncoding=UTF-8" /> 
     <property name="username" value="root" /> 
     <property name="password" value="xxx" /> 
</bean> 

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 

<bean id="hello" class="com.xol.oss.HelloService"> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 

<tx:annotation-driven transaction-manager="txManager"/> 

자바 코드 : 로그는 롤백 않았다

public void setDataSource(BasicDataSource dataSource) { 
    this.dataSource = dataSource; 
} 

@Transactional 
public void getData() { 
    Connection con=null; 
    try { 
     con = dataSource.getConnection(); 
     Statement stat = con.createStatement(); 
     stat.executeUpdate("update testdata set foo=9 where id=1"); 
     throw new RuntimeException("an Exception for test"); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } finally{ 
     try { 
      con.close(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

:

15:15:36,936 DEBUG DataSourceTransactionManager:366 - Creating new transaction with name [com.xol.oss.HelloService.getData]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' 
15:15:37,525 DEBUG DataSourceTransactionManager:205 - Acquired Connection [jdbc:mysql://127.0.0.1:3306/javatest?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL-AB JDBC Driver] for JDBC transaction 
15:15:37,535 DEBUG DataSourceTransactionManager:222 - Switching JDBC Connection [jdbc:mysql://127.0.0.1:3306/javatest?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL-AB JDBC Driver] to manual commit 
15:15:37,581 DEBUG DataSourceTransactionManager:844 - Initiating transaction rollback 
15:15:37,582 DEBUG DataSourceTransactionManager:280 - Rolling back JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/javatest?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL-AB JDBC Driver] 
15:15:37,583 DEBUG DataSourceTransactionManager:323 - Releasing JDBC Connection [jdbc:mysql://127.0.0.1:3306/javatest?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL-AB JDBC Driver] after transaction 
15:15:37,583 DEBUG DataSourceUtils:327 - Returning JDBC Connection to DataSource 
Exception in thread "main" java.lang.RuntimeException: an RuntimeException for test 
    at com.xol.oss.HelloService.getData(HelloService.java:31) 
    at com.xol.oss.HelloService$$FastClassByCGLIB$$3d7d84e8.invoke(<generated>) 

답변

5

문제는 당신이 사용하지 않는 것입니다 대신 봄에 의해 관리되는 연결, 당신은 새로운 연결을 열고 있습니다. 코드를 다음과 같이 변경하고 시도하십시오.

새 코드를 작성하려면 raw jdbc 대신 JdbcTemplate을 사용해야합니다.

class HelloService { 
    JdbcTemplate jdbcTemplate; 
    public setDataSource(DataSource dataSource) { 
     jdbcTemplate = new JDBCTemplate(dataSource); 
    } 

    @Transactional 
    public void getData() { 
     jdbcTemplate.update(update testdata set foo=9 where id=1); 
     throw new RuntimeException("an Exception for test"); 
    } 
+0

감사합니다. – user2392773

+0

+1 그리고 부끄럽지 않은 나를 위해 부끄러워 :) –

+0

고마워. 나는 2 시간을 보냈다. – SalutonMondo