0 0

Mybatis整合Spring3,事务注解为何不起作用?5

spring核心配置
<?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:p="http://www.springframework.org/schema/p"
	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-3.1.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
		p:location="/WEB-INF/jdbc.properties" />
		
	<!-- 数据源配置, 使用应用中的DBCP数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<!-- Connection Info -->
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.databaseurl}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<!-- Connection Pooling Info -->
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="defaultAutoCommit" value="false" />
		<!-- 连接Idle一个小时后超时 -->
		<property name="timeBetweenEvictionRunsMillis" value="3600000" />
		<property name="minEvictableIdleTimeMillis" value="3600000" />
	</bean>

	<!-- define the SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.kimho" />
    </bean>

    <!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.kimho" />
    </bean>

	<!-- enable transaction demarcation with annotations-->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
	 
	<!-- transaction manager, use JtaTransactionManager for global tx-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

[b]Service层通过注解@Transactional注入事务[/b]:

@Service
//默认将类中的所有函数纳入事务管理.
@Transactional
public class UserService{
	@Autowired
	private UserDao userDao;
	/**
	 * 获取主键id
	 * @return
	 */
	@Transactional(readOnly = true)
	public Long getId() {
		return userDao.getId();
	}
          
          /**
	 * 编辑用户
	 * @param user
	 * @param param
	 */
	public void updateUser(User user,Map<String,Object> param) {
		//编辑用户
		userDao.updateUser(user);
		//删除原有的用户角色关联
		userDao.deleteUserRole(param);
		String s = null;
		s.getBytes();
		//新增用户角色关联
		insertUserRole(param);
	}
         这里用来测试事务回滚
         ...
}

测试过几次updateUser的调用,事务就是不起作用的,后台log信息如下:

DEBUG 2011-09-04 16:19:46,672 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: JDBC Connection [org.apache.commons.dbcp.PoolableConnection@67aece] will not be managed by Spring

DEBUG 2011-09-04 16:19:46,672 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19006c9] was not registered for synchronization because synchronization is not active

DEBUG 2011-09-04 16:19:46,687 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: Committing JDBC Connection [org.apache.commons.dbcp.PoolableConnection@67aece]

DEBUG 2011-09-04 16:19:46,687 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: Closing no transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19006c9]

DEBUG 2011-09-04 16:19:46,687 org.springframework.jdbc.datasource.DataSourceUtils: Returning JDBC Connection to DataSource


折腾了很长时间,一直都搞不定。

mybatis版本:3.0.6 spring版本:3.1M2

问题补充:
macrotea 写道
你好,会不会是漏了:
<context:component-scan base-package="com.mtea" />

导致没有把@Service 的类被spring托管.

没漏,在另一个spring的配置文件里面,已经配置了<context:component-scan base-package="com.kimho" />

问题补充:
kxys422834 写道
试试看把事务注解放在方法上能不能成功啊,如果成功了,在定位问题吧。

试过了,也不行

问题补充: 貌似找到了一篇文章,里面有提到事务失效的原因,晚上回去试下。
文章链接:http://blog.sina.com.cn/s/blog_5ddc071f0100uf7x.html

问题补充:<!-- 扫描业务组件,让spring不扫描带有@Service注解的类,防止事务失效 -->
<context:component-scan base-package="com.kimho">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
springmvc配置改成这样,connection显示有spring的事务控制了。不过问题是,数据还是回滚不了,继续找原因……
2011年9月04日 16:32

4个答案 按时间排序 按投票排序

0 0

在需要启用spring transaction的service 上加上 @EnableTransactionManagement 就行了,可以和 @Service 同时存在,不需要添加exclude-filter.

2016年10月24日 10:04
0 0

你好,你的问题找到原因了吗?我也遇到这样的问题。请赐教...

2012年12月28日 22:17
0 0

试试看把事务注解放在方法上能不能成功啊,如果成功了,在定位问题吧。

2011年9月05日 09:35
0 0

你好,会不会是漏了:
<context:component-scan base-package="com.mtea" />

导致没有把@Service 的类被spring托管.

2011年9月04日 18:56

相关推荐

Global site tag (gtag.js) - Google Analytics