Spring+Mybatis整合事务不起作用之解决方案汇总
前言:
公司最近一个项目用到Spring和Mybatis,发现用起来挺方便,比以前的那个struts+hibernate舒服多了。废话少说,直接摆问题,碰到的问题是,mybatis不在事务中运行,后台日志报 “Closing no transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19006c9]”错误。无论是加了@Transactional注解和是没加都报这个信息。一个方法中插入多条数据,某次插入失败也不回滚。
问题描述:
环境: Spring 3.1.0 + Mybatis 3.1.0 + mybatis-spring 1.0.0 RC3 + DB2 9.5 + Tomcat 6.0.35
web工程名称: isap
配置文件:applicationContext.xml + isap-servlet.xml
先看配置信息:
applicationContext.xml
[html]
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- spring配置jndi -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jndi_isap</value>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations">
<list>
<value>classpath:com/cosbulk/isap/*/dao/mapper/*Mapper.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
<qualifier value="isap"/>
</bean>
<!-- 如果是isap的表,用@Transactional("isap"),如果是smis用 @Transactional("smis")管理事务-->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="basePackage" value="com.cosbulk.isap"></property>
</bean>
<!-- /////////////////////////////////////////////////////////////////////////// -->
<!-- smis数据源 -->
<bean id="smisDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jndi_smis</value>
</property>
</bean>
<bean id="smisSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
<property name="dataSource" ref="smisDataSource"/>
<property name="mapperLocations">
<list>
<value>classpath:com/cosbulk/smis/dao/mapper/*Mapper.xml</value>
</list>
</property>
</bean>
<bean id="smisTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="smisDataSource" />
<qualifier value="smis"/>
</bean>
<!-- 如果是isap的表,用@Transactional("isap"),如果是smis用 @Transactional("smis")管理事务-->
<tx:annotation-driven transaction-manager="smisTransactionManager" />
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="smisSqlSessionFactory"/>
<property name="basePackage" value="com.cosbulk.smis"></property>
</bean> &n
补充:软件开发 , Java ,