从 sessionFactory.openSession() 休眠、监听或覆盖 opneSession
Hibernate, listen or override to opneSession from a sessionFactory.openSession()
问题是如果你使用
properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
然后 Hibernate 将只对 threadlocal
进行 sessionFactory.getCurrentSession()
次调用而不是 sessionFactory.openSession()
次调用。
getCurrentSession
被委托给 CurrentSessionContext
但 openSession
没有,他们也没有注册。
如果有人也试图注册这些,就会出现问题。我们要使用 ThreadLocal
List
,其中 getCurrentSession
将使用堆栈中的最后一个。
当前的实现似乎认为没有必要管理 openSession 调用,因为它们无法预见一些其他潜在用途(更正确的用途)。
那么,我是否只剩下重写实体 SessionFactoryImpl
class 来挂接,或者是否有一些 事件被触发并且我可以在 Session 打开时挂接?
查看源代码,无法插入 openSession
的行为。您可以将会话工厂包装在 SessionFactoryDelegatingImpl
中以减少样板文件。
连接到 getCurrentSession
更容易,您只需要实现一个自定义的 org.hibernate.context.CurrentSessionContext
。
问题是如果你使用
properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
然后 Hibernate 将只对 threadlocal
进行 sessionFactory.getCurrentSession()
次调用而不是 sessionFactory.openSession()
次调用。
getCurrentSession
被委托给 CurrentSessionContext
但 openSession
没有,他们也没有注册。
如果有人也试图注册这些,就会出现问题。我们要使用 ThreadLocal
List
,其中 getCurrentSession
将使用堆栈中的最后一个。
当前的实现似乎认为没有必要管理 openSession 调用,因为它们无法预见一些其他潜在用途(更正确的用途)。
那么,我是否只剩下重写实体 SessionFactoryImpl
class 来挂接,或者是否有一些 事件被触发并且我可以在 Session 打开时挂接?
查看源代码,无法插入 openSession
的行为。您可以将会话工厂包装在 SessionFactoryDelegatingImpl
中以减少样板文件。
连接到 getCurrentSession
更容易,您只需要实现一个自定义的 org.hibernate.context.CurrentSessionContext
。