Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 回滚在jdbc spring编程事务中不起作用_Java_Spring_Jdbc_Oracle11g - Fatal编程技术网

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

Java 回滚在jdbc spring编程事务中不起作用,java,spring,jdbc,oracle11g,Java,Spring,Jdbc,Oracle11g,我无法在运行在Oracle11.2上的spring(3.0.5)jdbc应用程序中实现回滚 当我在控制器中抛出noclassescexception时,updatedB()插入的行仍保留在数据库中。 我认为这是因为自动提交在我的数据源中是打开的(默认情况下),所以提交已经发生,回滚显然不起作用, 但是我认为SpringDataSourceTransactionManager处理了所有这些并强制回滚了吗 有趣的是,当我在数据源ie注释中关闭自动提交时: "defaultAutoCommit" va

我无法在运行在Oracle11.2上的spring(3.0.5)jdbc应用程序中实现回滚

当我在控制器中抛出
noclassescexception
时,updatedB()插入的行仍保留在数据库中。 我认为这是因为自动提交在我的数据源中是打开的(默认情况下),所以提交已经发生,回滚显然不起作用, 但是我认为Spring
DataSourceTransactionManager
处理了所有这些并强制回滚了吗

有趣的是,当我在数据源ie注释中关闭自动提交时:

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

//控制器,从Main()调用

我的spring.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="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>
<?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>

我的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="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>
<?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;
    }
}

为什么你没有性观念?。。删除sysout并重试您正在抛出、捕获和吞咽异常。
DataSourceTransactionManager
从未看到异常并将提交。感谢您的建议,但当我删除try-catch时,该行仍将提交。堆栈跟踪是:线程“main”NoClassesException中的异常异常异常:在M Denium、Xstian和Sezin Karli的建议下,在MainDS.main(MainDS.java:43)的Controller.transactionTest(Controller.java:34)处回滚,我将main()中的调用从:cont.transactionTest()更改为:;继续mainTest();并将public void mainTest(){try{transactionTest();}catch(noclassescexception e){System.out.println(e.getMessage();}})添加到控制器中,以允许在捕获noclassescexception()之前完成事务,但该行仍然插入到数据库中。注意-我试图编辑原始帖子以包含这些更改,但编辑一直抱怨格式错误。任何其他建议都非常受欢迎。在使用TransactionSynchronizationManager.isActualTransactionActive()调用updateDb()之前,我检查了事务的状态,结果返回false。但为什么交易不活跃?我的切入点表达式错了吗?注意:我使用Eclipse中的默认包。谢谢Sezin。我试过了,但争吵仍在继续。堆栈跟踪是线程“main”NoClassesException中的异常:在MainDS.main(MainDS.java:43)的Controller.transactionTest(Controller.java:34)处回滚