Spring声明事务不起作用(事务未提交)

时间:2012-12-14 08:11:53

标签: java spring transactions

Spring声明式事务无效(未提交)。

Spring配置

  <!-- DATASOURCE -->

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="${jdbc.driver}"/>
      <property name="url" value="${jdbc.url}"/>
      <property name="username" value="${jdbc.user}"/>
      <property name="password" value="${jdbc.password}"/>
      <property name="initialSize" value="${dbcp.initialSize}"/>
      <property name="maxActive" value="${dbcp.maxActive}"/>
      <property name="maxIdle" value="${dbcp.maxIdle}"/>
      <property name="maxWait" value="${dbcp.maxWait}"/>
      <property name="poolPreparedStatements" value="${dbcp.poolPreparedStatements}"/>
      <property name="validationQuery" value="select 1 from dual"/>
      <property name="testOnBorrow" value="${dbcp.testOnBorrow}"/>
      <property name="maxOpenPreparedStatements" value="${dbcp.maxOpenPreparedStatements}"/>
      <property name="logAbandoned" value="${dbcp.logAbandoned}"/>
      <property name="removeAbandoned" value="${dbcp.removeAbandoned}"/>
      <property name="removeAbandonedTimeout" value="${dbcp.removeAbandonedTimeout}"/>
      **<property name="defaultAutoCommit" value="false"/>**
   </bean>

  <!-- IBATIS -->

    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocations" value="classpath:/config/ibatis/sqlMapConfig.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
      <property name="sqlMapClient" ref="sqlMapClient"/>
    </bean>

  <!-- TRANSACTION -->

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

   <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED" timeout="10" read-only="false"/>
      </tx:attributes>
   </tx:advice>

    <aop:config>
        <aop:pointcut id="serviceMethods" expression="execution(public * com.store.web.front.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
    </aop:config>

Controller Bean

package com.store.web.front.controller;

    public interface TestTxControllerIF {
       public ModelAndView transaction();
    }

package com.store.web.front.controller;

    @Controller
    public class TestTxControllerImpl implements TestTxControllerIF {

        @Autowired
        protected TestTxServiceIF testService;

        Logger                    logger = Logger.getLogger(TestTxControllerIF.class);

        @Override
        @RequestMapping(value = "/test/tx.html")
        public ModelAndView transaction() {

            logger.info("# TestTxController.transaction() - " + testService);

            testService.transaction();
            return new ModelAndView("main");
        }
}

服务Bean

    package com.store.web.front.service;

        public interface TestTxServiceIF {
            public void transaction();
        }

    package com.store.web.front.service;

        @Service
        public class TestTxServiceImpl implements TestTxServiceIF {

            Logger logger = Logger.getLogger(TestTxServiceImpl.class);

            @Autowired
            protected TestDaoIF testDao;

            @Override
            public void transaction() {

                logger.info("# TestTxService.transaction()");

                Test test1 = new Test("111", "First");
                Test test2 = new Test("222", "Second");
                Test test3 = new Test("333", "Third");

                testDao.insertTest(test1);        
                logger.info("# Successfully inserted!!! - " + test1);

                testDao.insertTest(test2);        
                testDao.insertTest(test3);
            }
        }

DAO Bean

package com.store.web.front.dao;

    public interface TestDaoIF {
        public void insertTest(Test test);
    }

package com.store.web.front.dao;

    @Repository
    public class TestDaoImpl extends AbstractIBatisDao implements TestDaoIF {    

        Logger logger = Logger.getLogger(TestTxServiceImpl.class);

        @Override
        public void insertTest(Test test) {

            logger.info("# TestDao.insertTest()");

            template.insert("test.insertTest", test);
        }
    }

结果&amp;问题

部署并发送请求“/test/tx.html”后,事务执行没有问题,但数据库记录不会保留。 我想,交易没有提交。 问题是什么?

日志

DEBUG> 11:02:10 - DispatcherServlet with name 'spring-dispatcher' processing request for [/test/tx.html] ☜ DispatcherServlet.java:781
DEBUG> 11:02:10 - Invoking request handler method: public org.springframework.web.servlet.ModelAndView com.store.web.front.controller.TestTxControllerImpl.transaction() ☜ HandlerMethodInvoker.java:134
INFO > 11:02:10 - # TestTxController.transaction() - com.store.web.front.service.TestTxServiceImpl@126cb1a ☜ TestTxControllerImpl.java:23
INFO > 11:02:10 - # TestTxService.transaction() ☜ TestTxServiceImpl.java:25
INFO > 11:02:10 - # TestDao.insertTest() ☜ TestDaoImpl.java:20
DEBUG> 11:02:10 - Opened SqlMapSession [com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl@1bc345a] for iBATIS operation ☜ SqlMapClientTemplate.java:177
DEBUG> 11:02:10 - Fetching JDBC Connection from DataSource ☜ DataSourceUtils.java:112
DEBUG> 11:02:10 - Obtained JDBC Connection [jdbc:oracle:thin:@localhost:1521:XE, UserName=STORE, Oracle JDBC driver] for iBATIS operation ☜ SqlMapClientTemplate.java:194
DEBUG> 11:02:10 - Returning JDBC Connection to DataSource ☜ DataSourceUtils.java:312
INFO > 11:02:10 - # Successfully inserted!!! - Test [id=111, name=First] ☜ TestTxServiceImpl.java:33

2 个答案:

答案 0 :(得分:0)

我发现了这个问题的原因。 当DispatcherServlet加载事务声明文件时, 交易控制成功。 但是文件是由ContextLoaderListener加载的,事务控制并不好。 我不知道为什么会出现这种差异。 你能解释一下吗?

答案 1 :(得分:0)

豆子被BeanPostProcessor s的“AOP东西”装饰(在你的情况下可能是AspectJAwareAdvisorAutoProxyCreator)。这些后处理器bean是特定于上下文实例的

如果您在 servlet上下文中配置AOP并且您的目标bean位于根上下文(反之亦然),那么这就是未应用切入点的原因

通常的做法是在根上下文中使用较低级别的bean(服务和DAO)和AOP配置,并在 servlet上下文中仅保留UI bean(MVC内容)