简体   繁体   English

由春季交易经理管理的“暂停”休眠会话

[英]“suspend” hibernate session managed by spring transaction manager

Is there any way to remove/suspend a current spring managed hibernate session from a thread so a new one can be used, to then place the original session back onto the thread? 有什么方法可以从线程中删除/挂起当前的Spring管理的休眠会话,以便可以使用新的会话,然后将原始会话放回线程中吗? Both are working on the same datasource. 两者都在同一个数据源上工作。

To describe the problem in more detail. 详细描述问题。 I'm trying to create a plugin for a tool who has it's own spring hibernate transaction management. 我正在尝试为拥有自己的Spring Hibernate事务管理工具的插件创建插件。 In this plugin I would like to do some of my own database stuff which is done on our own spring transaction manager. 在这个插件中,我想做一些自己的数据库工作,这些工作是在我们自己的spring事务管理器上完成的。 When I currently try to perform the database actions our transaction manager starts complaining about an incompatibly transactionmanager already being used 当我当前尝试执行数据库操作时,我们的事务管理器开始抱怨已经使用了不兼容的事务管理器

org.springframework.transaction.IllegalTransactionStateException: Pre-bound JDBC Connection found! HibernateTransactionManager does not support running within DataSourceTransactionManager if told to manage the DataSource itself. It is recommended to use a single HibernateTransactionManager for all transactions on a single DataSource, no matter whether Hibernate or JDBC access.

A workaround that seems to do the trick is running my own code in a different thread and waiting for it to complete before I continue with the rest of the code. 一种似乎可以解决问题的解决方法是,在其他线程中运行我自己的代码,并等待其完成,然后再继续执行其余代码。

Is there a better way then that, seems a bit stupid/overkill? 有没有更好的办法呢,似乎有点愚蠢/矫kill过正? Some way to suspend the current hibernate session, then open a new one and afterworths restoring the original session. 某种方式可以挂起当前的休眠会话,然后打开一个新的休眠会话,然后恢复原来的会话。

Is there any reason you can't have the current transaction manager injected into your plugin code? 有什么原因不能将当前的事务管理器注入您的插件代码中? Two tx managers sounds like too many cooks in the kitchen. 两位TX经理听起来厨房里厨师太多。 If you have it injected, then you should be able to require a new session before doing your work using the @transactional annotation's propagation REQUIRES_NEW attribute see the documentation for an example set-up 如果注入了它,那么在使用@transactional批注的 propagation REQUIRES_NEW属性进行工作之前,您应该能够需要一个新的会话,请参见文档中的示例设置

eg 例如

@transactional(propogation = Propogation.REQUIRES_NEW)
public void addXXX(Some class) {
    ...
}

But this would use spring's PlatformTransactionManager rather than leaving it up to hibernate to manage the session / transaction. 但这将使用spring的PlatformTransactionManager而不是让它休眠以管理会话/事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM