如何使用myBatis和Spring设置交易 [英] How to set up transaction with myBatis and Spring
问题描述
我正在尝试建立交易,但没有成功. 这是我的代码:
I am trying set up transaction but without success. Here is my code:
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
.......
<property name="defaultAutoCommit" value="false" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-configuration.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
@Transactional
private void prcessTransaction(...) {
delete(...);
//insert:
for(Item item: itemList){
insert(item)
}
}
<delete id="delete" parameterType="map">
delete from .....
</delete>
<insert id="insert" parameterType="Item">
insert into ....
</insert>
看起来prcessTransaction方法不仅是一个事务,而且是多个事务的集合.
It looks like that prcessTransaction method is not only one transaction but sets of multiple transactions.
我正在使用Spring 3.0.5,myBatis 3.0.4,mybatis-spring-1.0.1,Tomcat 7.0.19,Oracle 11.1.0.6.0
I am using Spring 3.0.5, myBatis 3.0.4, mybatis-spring-1.0.1, Tomcat 7.0.19, Oracle 11.1.0.6.0
感谢您的帮助.
推荐答案
在私有方法上放置@transactional似乎是有问题的,
Putting @transactional on a private method looks problematic, the Spring documentation says:
在代理模式(默认)下,仅拦截通过代理传入的外部方法调用.这意味着,实际上,即使被调用的方法标记有@Transactional,自调用实际上也不会导致运行时实际事务在目标对象中调用目标对象的另一种方法.
In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional.
同一部分没有此内容:
方法可见性和@Transactional
使用代理时,应仅将@Transactional注释应用于具有公共可见性的方法.如果使用@Transactional注释对受保护的,私有的或程序包可见的方法进行注释,则不会引发任何错误,但是带注释的方法不会显示已配置的事务设置.如果需要注释非公共方法,请考虑使用AspectJ(请参见下文).
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.
这篇关于如何使用myBatis和Spring设置交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!