4

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>

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

服务豆

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

道豆

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

结果与问题

部署并发送请求“/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
4

2 回答 2

0

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

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

常见的做法是将较低级别的 bean(服务和 DAO)与 AOP 配置一起放在根上下文中,并在servlet 上下文中只保留 UI bean(MVC 的东西)。

于 2013-05-18T13:24:56.737 回答
0

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

于 2013-01-17T14:24:46.607 回答