【问题标题】:Very simple Spring transactions of JDBC not roll back (even log said yes)非常简单的 JDBC 的 Spring 事务不回滚(甚至日志都说是)
【发布时间】:2013-05-12 07:25:33
【问题描述】:

我是 Spring 事务的新手。我使用 Spring 3.2.2 和 MySQL 5.5.20(InnoDB)。我可以在日志文件中看到它确实回滚了,但在数据库中,记录仍在更新为 9。我错过了什么?谢谢。

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"/>

Java 代码:

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, UserName=root@localhost, 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, UserName=root@localhost, 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, UserName=root@localhost, 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, UserName=root@localhost, 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>)

【问题讨论】:

    标签: jdbc spring-transactions


    【解决方案1】:

    问题是您没有使用 spring 管理的连接,而是打开了一个新连接。把代码改成下面试试。

    import org.springframework.jdbc.datasource.DataSourceUtils;    
    
    @Transactional
    public void getData()  {
        Connection con=null;
        try {
           // Get the connection associated with the transaction
           con = DataSourceUtils.getConnection(dataSource);
           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{
            DataSourceUtils.releaseConnection(dataSource, con);
         }
      }
    

    如果您正在编写新代码,您应该使用 JdbcTemplate 而不是原始 jdbc。

    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");
        }
    

    【讨论】:

    • 非常感谢,这正是我需要的。
    • 非常感谢。我花了 2 个小时不是这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    相关资源
    最近更新 更多