问题描述
- mybatis spring 整合 junit测试。 事务不起作用,不提交。删除无效???
-
applicationContext.xml 中的数据库和 sessionFactory以及事务配置
Xml代码<bean id="bssDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="${bss.jdbc.driver}" /> <property name="url" value="${bss.jdbc.url}" /> <property name="username" value="${bss.jdbc.username}" /> <property name="password" value="${bss.jdbc.password}" /> <!-- Connection Pooling Info --> <property name="maxActive" value="${bss.dbcp.maxActive}" /> <property name="maxIdle" value="${bss.dbcp.maxIdle}" /> <property name="defaultAutoCommit" value="false" /> <!-- 连接Idle一个小时后超时 --> <property name="timeBetweenEvictionRunsMillis" value="3600000" /> <property name="minEvictableIdleTimeMillis" value="3600000" /> <property name="testOnBorrow" value="true" /> <property name="testWhileIdle" value="true" /> <property name="validationQuery" value="${bss.dbcp.validationQuery}" /> </bean> <!-- MyBatis配置 --> <bean id="bssSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="bssDataSource" /> <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 别名 --> <property name="typeAliasesPackage" value="com.jxnc56.bss.entity" /> <!-- 显式指定Mapper文件位置 --> <property name="mapperLocations" > <list> <value> classpath:config/mybatis/sqlmap/bss/**/*.xml </value> </list> </property> </bean> <!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.jxnc56.bss" /> <property name="annotationClass" value="com.jxnc56.bss.dao.MyBatisRepository"/> </bean> <!-- 事务管理,@Transactional("tmBss") --> <bean id="tmBss" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="bssDataSource"></property> <qualifier type="org.springframework.beans.factory.annotation.Qualifier" value="tmBss" /> </bean> <!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 --> <tx:annotation-driven transaction-manager="tmBss" proxy-target-class="true" />
java TEST:
Java代码@Transactional("tmBss") public class ProductServiceTest extends SpringTransactionalTestCase { @Autowired private ProductDao pDao; @Test public void deleteTest (){ pDao.delete(4); Product product = new Product(); product.setId(4); System.out.println("-------------------------------------------->"+Arrays.toString(pDao.select(product).toArray())); } }
其中ProductDao 是被@MyBatisRepository标示的接口
log:
2013-06-07 09:40:57 107 - org.mybatis.spring.transaction.SpringManagedTransaction -915 [main] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [jdbc:mysql://localhost/nrcompany?useUnicode=true&characterEncoding=utf-8, UserName=root@localhost, MySQL-AB JDBC Driver] will be managed by Spring
2013-06-07 09:40:57 110 - com.jxnc56.bss.dao.ProductDao.delete -918 [main] DEBUG com.jxnc56.bss.dao.ProductDao.delete - ooo Using Connection [jdbc:mysql://localhost/nrcompany?useUnicode=true&characterEncoding=utf-8, UserName=root@localhost, MySQL-AB JDBC Driver]
2013-06-07 09:40:57 115 - com.jxnc56.bss.dao.ProductDao.delete -923 [main] DEBUG com.jxnc56.bss.dao.ProductDao.delete - ==> Preparing: delete from product where id=?
2013-06-07 09:40:57 140 - com.jxnc56.bss.dao.ProductDao.delete -948 [main] DEBUG com.jxnc56.bss.dao.ProductDao.delete - ==> Parameters: 4(Integer)
2013-06-07 09:40:57 142 - org.mybatis.spring.SqlSessionUtils -950 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3a9d95]
2013-06-07 09:40:57 142 - org.mybatis.spring.SqlSessionUtils -950 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3a9d95] from current transaction
解决方案
http://blog.csdn.net/will_awoke/article/details/12002705
其实真正的问题在于spring 和springmvc 分父子容器,ContextLoaderListener才会给@Service加上事务,而springmvc 只是生成普通的bean