问题描述
我正在尝试建立交易,但没有成功.这是我的代码:
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似乎是有问题的, Spring文档说:
Putting @transactional on a private method looks problematic, the Spring documentation says:
同一部分没有此内容:
使用代理时,应仅将@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设置交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!