找出是否有与当前线程关联的事务?
用户事务类中有一个方法commit。当我们提交它时,首先它会发现是否存在与该线程关联的事务。我的问题是,在核心java级别,我们如何找出是否存在与当前线程关联的事务? (我知道它是 JTA api 的内部流程,但仍然感兴趣)
There is a method commit in user transaction class. When we do commit on it, first it finds that if there is a transaction associated with this thread. My question is how at core java level we can find out if there is a transaction associated with current thread?
(i know its a internal process to JTA apis but still interested )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的一种简单方法是保留一个映射,其中线程引用作为“键”,事务对象作为“值”。对于过去没有创建事务的线程,映射中不会有这样的键,因此 UserTransaction 实现可以决定抛出异常。
任何一行代码都可以通过Thread.currentThread()知道自己是哪个Thread。
希望有帮助。
谢谢,
镍丁
One simple way I can think of is to keep a map with Thread reference as 'key' and the Transaction object as 'value'. For a thread with no transaction created in the past, there will be no such key in the map and hence UserTransaction implementation can decide to throw an exception.
Any line of code can know which Thread it is by Thread.currentThread().
Hope that helps.
Thanks,
Nitin