如何使用 myBatis 和 Spring 设置事务
我正在尝试设置交易但没有成功。 这是我的代码:
<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>
看起来 processTransaction 方法不仅是一个事务,而且是多个事务的集合。
我正在使用 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 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>
It looks like that prcessTransaction method is not only one transaction but sets of multiple transactions.
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
Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 @transactional 放在私有方法上看起来有问题, Spring 文档 说:
同一部分有以下内容:
Putting @transactional on a private method looks problematic, the Spring documentation says:
The same section has this aside:
我们遇到了类似的问题,但在更复杂的环境中,我们有 2 个数据库,每个数据库都有自己的事务管理器。我们让它工作的唯一方法是在 @Transactional("transactionManager") 注释上指定事务管理器实例。
它有效,但我不满意,因为我不明白为什么我们需要在注释上显式指定事务管理器。
We had a similar problem, but in a more complex environment where we have 2 databases with their own transaction manager each. The only way we got it to work was to specify the transaction manager instance on the
@Transactional("transactionManager")
annotation.It works, though I'm left unsatisfied as I don't understand why we need to explicitly specify the transaction manager on the annotation.