【发布时间】:2013-05-31 02:37:12
【问题描述】:
在我们的应用程序中,我们使用 Spring 和 Hibernate。
在所有的 DAO 类中,我们都有 SessionFactory 自动连接,并且每个 DAO 方法都调用 getCurrentSession() 方法。
我的问题是为什么我们不在原型范围内注入 Session 对象而不是 SessionFactory 对象?这将为我们节省对 getCurrentSession 的调用。
我认为第一种方法是正确的,但正在寻找第二种方法会引发错误或性能不佳的具体场景?
【问题讨论】:
-
一个案例阅读the doc,上面写着
If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs. -
你最好注入
javax.persistence.EntityManager但为此你必须坚持使用JPA。