Spring事务管理器和多线程 [英] Spring transaction manager and multithreading

查看:139
本文介绍了Spring事务管理器和多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Callable接口在serviceImpl中编写多线程程序。我正在使用spring事务管理器。当在DB中执行更新操作时,它会成功执行。但更新的数据不会反映在DB中。但是当我运行没有多线程的程序,它在DB中更新

I am writing multithreading program in serviceImpl using Callable interface.I am using spring transaction manager.When update operation is executed in DB ,it is executed successfully .But the updated data is not reflected in DB.But When i run program without multithreading it is updated in DB.

这是我的配置

<tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" />
            <tx:method name="find*" propagation="NOT_SUPPORTED" />
            <tx:method name="get*" propagation="NOT_SUPPORTED" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="serviceOperation" expression="execution(* *..*ServiceImpl.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
    </aop:config>
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

我可以转向另一种方法进行交易管理。我想知道这种方法是否支持或不适合多线程。
所以我的问题是
Spring事务管理器是否支持多线程(我的意思是仅通过声明注释或XML)
为什么在我的情况下更新的数据不会反映在DB中?

什么是最好的替代方法?

I can shift to another approach for transaction manager.Just i want to get confirm if this approach supports or not for multithreading. So my question is Do spring transaction manager supports multithreading(I mean just by declaring annotation Or XML ) Why updated data is not reflected in DB in my case? What can be the best alternative approach ?

推荐答案

使用的交易上下文Spring存储在线程局部变量中。因此,如果您使用可调用函数启动新线程或在另一个线程中执行代码,则此代码将不会成为Spring事务方面启动的事务的一部分。这就是为什么您的数据不会出现在数据库中。

The transactional context used by Spring is stored in a thread-local variable. So if you start a new thread, or execute code in another thread using a callable, this code won't be part of the transaction started by the Spring transactional aspect. That's why your data doesn't appear in the database.

这篇关于Spring事务管理器和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆