java - 回滚在 jdbc spring 编程事务中不起作用

标签 java spring jdbc oracle11g

我无法在运行于 Oracle 11.2 上的 spring (3.0.5) jdbc 应用程序中进行回滚

当我在 Controller 中抛出 NoClassesException 时,updateB() 插入的行仍保留在 dB 中。 我认为这是因为自动提交在我的数据源中(默认情况下)处于打开状态,因此提交已经发生并且回滚显然不起作用, 但我认为 Spring DataSourceTransactionManager 处理了所有这些并强制回滚?

有趣的是,当我在数据源中关闭 autoCommit 时,即注释中:

"defaultAutoCommit" value="false" 

并亲自明确调用提交,即评论:

this.jdbcTemplate.getDataSource().getConnection().commit();

什么也没发生,即该行根本没有提交,所以看起来我做了一些愚蠢的事情。 如果有人可以指出这个错误,我将非常感激

我的代码是:


public static void main(String[] args) {

        String [] configList ={"database.xml","spring.xml"};

        ApplicationContext ctx = new ClassPathXmlApplicationContext(configList);
        cont = (Controller)ctx.getBean("controller");
        cont.transactionTest();
}


// Controller , called from Main()


public class Controller {

    private JdbcTemplate jdbcTemplate;


    public void transactionTest()
    {       
        int retCode=0;


        try {
            retCode = updatedB("param1","param2");
            //this.jdbcTemplate.getDataSource().getConnection().commit();
            throw new NoClassesException(); 
        }catch (NoClassesException e){
            System.out.println(e.getMessage() + "2 patents ");
        }
    }

    public int updatedB(String param1,String param2 ) 
    {
        int stat = 0;

        stat = this.jdbcTemplate.update("INSERT INTO myTable"
                                + "(param1,param2)"
                                + " VALUES(?,?)"  , 
                new Object[] { param1,param2});

        return stat;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}


public class NoClassesException extends RuntimeException  {

    public NoClassesException() {
        super("Rolled back ");
    }
}

and my spring.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


    <bean id="controller" class="Controller">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>


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

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="transaction*" propagation="REQUIRED" rollback-for="NoClassesException" />
            <tx:method name="update*" propagation="SUPPORTS" />
            <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>



    <aop:config>    
        <aop:pointcut id="myMethods" expression="execution(* *..Controller.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myMethods" />
    </aop:config>



    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

我的database.xml文件是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">



    <bean id="dataConfigPropertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="searchSystemEnvironment" value="true" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="initialSize" value="2" />
        <property name="maxActive" value="2" />

        <property name="url" value="my connection details" />
        <property name="username" value="xxx" />
        <property name="password" value="xxx" />
        <!-- <property name="defaultAutoCommit" value="false" /> -->
    </bean>

</beans>

最佳答案

您的代码应该更新,例如

 public void transactionTest()
    {       
        int retCode=0;

            retCode = updatedB("param1","param2");
            //this.jdbcTemplate.getDataSource().getConnection().commit();
            throw new NoClassesException(); 

    }

    public int updatedB(String param1,String param2 ) 
    {
        int stat = 0;

        stat = this.jdbcTemplate.update("INSERT INTO myTable"
                                + "(param1,param2)"
                                + " VALUES(?,?)"  , 
                new Object[] { param1,param2});

        return stat;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}

关于java - 回滚在 jdbc spring 编程事务中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25721818/

相关文章:

java - Mahout 中的 IllegalArgumentException

hibernate - 在 Spring Context 中创建 JPA EntityMananger 的问题

java - 当子类未实现所有构造函数时实例化基类

java - JDBC API 规范和实现

java - Android - 奇怪的 EscapeUtil.unescapeString 崩溃

java - Netezza Java UDTF 自定义函数

Spring 安全 : Authentication method not supported: GET

postgresql - 当一行有一个外键到另一行时,如何原子地写入两行?

Java Servlet SQL 表

java - 在非常简单的示例中使用 EasyMock.expect() 时编译错误?