5
回答
spring4+mybatis3整合,异常时事务不回滚
注册华为云得mate10,2.9折抢先购!>>>   

注解和切面都进行了事务的配置

系统没有异常时,日志台打印如下语句:

2015-10-24 14:30:33 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
2015-10-24 14:30:33 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c71be88]
2015-10-24 14:30:33 [http-bio-80-exec-6] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [2144221929, URL=jdbc:MySQL://127.0.0.1:3306/ckgddyxt?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true, UserName=root@localhost , MySQL-AB JDBC Driver] will be managed by Spring
2015-10-24 14:30:33 [http-bio-80-exec-6] DEBUG com.cms.front.mapper.UserMapper.insert - ==>  Preparing: INSERT INTO t_user (username,password,id) VALUES ( ?,?,? )
2015-10-24 14:30:33 [http-bio-80-exec-6] DEBUG com.cms.front.mapper.UserMapper.insert - ==> Parameters: username(String), password(String), 508ae6d178d34d3d89f1f5cfc88f8990(String)
2015-10-24 14:30:33 [http-bio-80-exec-6] DEBUG com.cms.front.mapper.UserMapper.insert - <==    Updates: 1
2015-10-24 14:30:33 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c71be88]
2015-10-24 14:30:33 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c71be88]
2015-10-24 14:30:33 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c71be88]
2015-10-24 14:30:33 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c71be88]


当在service中的添加insertUser方法下面添加 int i=1/0; 代码时,控制台打印如下语句:

2015-10-24 14:36:36 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
2015-10-24 14:36:36 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c71be88]
2015-10-24 14:36:36 [http-bio-80-exec-6] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [2144221929, URL=jdbc:MySQL://127.0.0.1:3306/ckgddyxt?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true, UserName=root@localhost, MySQL-AB JDBC Driver] will be managed by Spring
2015-10-24 14:36:36 [http-bio-80-exec-6] DEBUG com.cms.front.mapper.UserMapper.insert - ==>  Preparing: INSERT INTO t_user (username,password,id) VALUES ( ?,?,? )
2015-10-24 14:36:36 [http-bio-80-exec-6] DEBUG com.cms.front.mapper.UserMapper.insert - ==> Parameters: username(String), password(String), 99aa92eb70b54160ac16e3d9a6cc9151(String)
2015-10-24 14:36:36 [http-bio-80-exec-6] DEBUG com.cms.front.mapper.UserMapper.insert - <==    Updates: 1
2015-10-24 14:36:36 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c71be88]
2015-10-24 14:36:36 [http-bio-80-exec-6] ERROR com.cms.front.service.UserService - java.lang.ArithmeticException: / by zero
2015-10-24 14:36:36 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c71be88]
2015-10-24 14:36:36 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c71be88]
2015-10-24 14:36:36 [http-bio-80-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c71be88]


数据还是被添加进去了,事务没有回滚。

是否是因为我catch了这个异常?如果我必须要catch异常并记录在日志里,如何手动让事务进行回滚?

跪求大神指导。

举报
wangaowell
发帖于2年前 5回/1K+阅

        Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_servlet.xml)产生的是子容器。子容器Controller进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service,而父容器进行初始化的Service是保证事务的增强处理能力的。如果不在子容器中将Service exclude掉,此时得到的将是原样的无事务处理能力的Service。

在spring的配置文件中

<context:component-scan base-package="com">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

在springMVC的配置文件中

    <context:component-scan base-package="com">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>

 

顶部