Spring - 使用 TransactionManager
我有两个事务管理器,很好奇是否有可能获得已使用的事务管理器。
更具体地说,underlyingMethod(..)
如何找出使用了哪个 transactionManager(无需向其发送附加参数“transactionManagerName/Ref”)
@Transactional("transactionManager1")
public void transactionFromFirstTM() {
someClass.underlyingMethod()
}
@Transactional("transactionManager2")
public void transactionFromSecondTM() {
someClass.underlyingMethod()
}
:?
好的,我已经使用它从实际事务管理器获取休眠会话:
protected Session getSession() {
Map<Object, Object> resourceMap = TransactionSynchronizationManager.getResourceMap();
Session session = null;
for (Object value : resourceMap.values()) {
if (value instanceof SessionHolder) {
session = ((SessionHolder) value).getSession();
break;
}
}
return session;
}
I have two transaction managers and was curious if there is some possibility to get the one that has been used.
To be more concrete, how could underlyingMethod(..)
find out which transactionManager was used (without sending it an additional parameter "transactionManagerName/Ref"):
@Transactional("transactionManager1")
public void transactionFromFirstTM() {
someClass.underlyingMethod()
}
@Transactional("transactionManager2")
public void transactionFromSecondTM() {
someClass.underlyingMethod()
}
?
ok I have used this to get the hibernate Session from actual transaction manager:
protected Session getSession() {
Map<Object, Object> resourceMap = TransactionSynchronizationManager.getResourceMap();
Session session = null;
for (Object value : resourceMap.values()) {
if (value instanceof SessionHolder) {
session = ((SessionHolder) value).getSession();
break;
}
}
return session;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不能,但你不应该对事务管理器做任何事情。当前事务的一些操作可在
TransactionSynchronizationManager
另一个有用的类是
TransactionAspectUtils
。但这并不是说两者都应该在内部使用,并且您不应该在代码中的许多地方依赖它们。I don't think you can, but you shouldn't do anything with the transaction manager. Some actions on the current transaction are available in
TransactionSynchronizationManager
Another useful class is the
TransactionAspectUtils
. But not that both are meant to be used internally, and you should not rely on them in many places in your code.