3
我有一個使用MyBatis和Spring設置的web應用程序,但它似乎沒有使用任何事務。這裏的配置:MyBatis-Spring設置不使用事務
的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>
樣品豆法事務定義:
@Transactional(propagation = Propagation.REQUIRED)
public void testDelete() {
testDAO.firstDelete(); //always successful
testDAO.secondDelete(); //always throws SQLServerException
}
TestDAO:
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 [[email protected]]
[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 [[email protected]] 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 [[email protected]]
[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 [[email protected]]
[11/17/11 16:42:08:099 PST] 0000002b SystemOut O DEBUG [org.mybatis.spring.SqlSessionUtils] Transaction synchronization committing SqlSession [[email protected]]
[11/17/11 16:42:08:123 PST] 0000002b SystemOut O DEBUG [org.mybatis.spring.SqlSessionUtils] Transaction synchronization closing SqlSession [[email protected]]
[11/17/11 16:42:08:136 PST] 0000002b SystemOut O DEBUG [org.mybatis.spring.SqlSessionUtils] Creating SqlSession with JDBC Connection [[email protected]]
[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 [[email protected]] 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 [[email protected]]
[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 [[email protected]]
[11/17/11 16:42:08:597 PST] 0000002b SystemOut O DEBUG [org.mybatis.spring.SqlSessionUtils] Transaction synchronization closing SqlSession [[email protected]]
...SQLServerException follows...
由於secondDelete()
引發異常,因此整個事務預計會回滾。但是,情況並非如此,firstDelete()
仍然承諾。我已經嘗試了事務管理器配置的所有組合(Spring管理,應用服務器管理,都),但我一直得到相同的結果。任何想法我做錯了什麼?
我在使用SQL Server 2005作爲數據庫的WebSphere 7上使用Mybatis 3,Spring 3。