Skip to main content
 首页 » 编程设计

spring之MyBatis-Spring 设置不使用事务

2024年02月06日47傻小

我有一个使用 MyBatis 和 Spring 设置的 Web 应用程序,但它似乎没有使用任何事务。配置如下:

applicationContext.xml:

<tx:annotation-driven /> 
 
<!-- <tx:jta-transaction-manager /> --> <!-- using WebSphere 7 --> 
 
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 
 
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName" value="jdbc/my_datasource" /> 
</bean> 
 
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="configLocation" value="WEB-INF/mybatis-config.xml" /> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 
 
<bean id="testDAO" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
    <property name="mapperInterface" value="com.lmig.TestDAO" /> 
    <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
</bean> 

带有事务定义的示例 bean 方法:

@Transactional(propagation = Propagation.REQUIRED) 
public void testDelete() { 
    testDAO.firstDelete(); //always successful 
    testDAO.secondDelete(); //always throws SQLServerException 
} 

测试DAO:

public interface TestDAO { 
    public void firstDelete(); 
    public void secondDelete(); 
} 

服务器调试日志:

[11/17/11 16:42:07:998 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.SqlSessionUtils] Creating SqlSession with JDBC Connection [com.ibm.ws.rsadapter.jdbc.WSJdbcConnection@34ba34ba]  
[11/17/11 16:42:07:999 PST] 0000002b SystemOut     O DEBUG [java.sql.Connection] ooo Connection Opened  
[11/17/11 16:42:08:001 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] JDBC Connection [com.ibm.ws.rsadapter.jdbc.WSJdbcConnection@34ba34ba] will be managed by Spring  
[11/17/11 16:42:08:005 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.SqlSessionUtils] Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74247424]  
[11/17/11 16:42:08:025 PST] 0000002b SystemOut     O DEBUG [java.sql.PreparedStatement] ==>  Executing: delete from test1   
[11/17/11 16:42:08:025 PST] 0000002b SystemOut     O DEBUG [java.sql.PreparedStatement] ==> Parameters:   
[11/17/11 16:42:08:097 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.SqlSessionUtils] Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74247424]  
[11/17/11 16:42:08:099 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.SqlSessionUtils] Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74247424]  
[11/17/11 16:42:08:123 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.SqlSessionUtils] Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74247424]  
[11/17/11 16:42:08:136 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.SqlSessionUtils] Creating SqlSession with JDBC Connection [com.ibm.ws.rsadapter.jdbc.WSJdbcConnection@17351735]  
[11/17/11 16:42:08:137 PST] 0000002b SystemOut     O DEBUG [java.sql.Connection] ooo Connection Opened  
[11/17/11 16:42:08:138 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] JDBC Connection [com.ibm.ws.rsadapter.jdbc.WSJdbcConnection@17351735] will be managed by Spring  
[11/17/11 16:42:08:139 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.SqlSessionUtils] Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d871d87]  
[11/17/11 16:42:08:145 PST] 0000002b SystemOut     O DEBUG [java.sql.PreparedStatement] ==>  Executing: delete from test2   
[11/17/11 16:42:08:146 PST] 0000002b SystemOut     O DEBUG [java.sql.PreparedStatement] ==> Parameters: 
[11/17/11 16:42:08:490 PST] 0000002b XmlBeanDefini I org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml] 
[11/17/11 16:42:08:554 PST] 0000002b SQLErrorCodes I org.springframework.jdbc.support.SQLErrorCodesFactory <init> SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase] 
[11/17/11 16:42:08:560 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.SqlSessionUtils] Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d871d87]  
[11/17/11 16:42:08:597 PST] 0000002b SystemOut     O DEBUG [org.mybatis.spring.SqlSessionUtils] Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d871d87] 
...SQLServerException follows... 

由于 secondDelete() 抛出异常,整个事务预计会回滚。然而,情况并非如此,firstDelete() 仍然被提交。我已经尝试了事务管理器配置的所有组合(Spring 管理、应用程序服务器管理,两者),但我不断得到相同的结果。知道我做错了什么吗?

我在 WebSphere 7 上使用 Mybatis 3、Spring 3,并以 SQL Server 2005 作为数据库。

请您参考如下方法:

我发现了这一点:我的 testDelete() 方法不是 Spring 管理的 bean,因此它不参与 Spring 管理的事务。我将配置更改为这个,现在它可以工作了。