Hibernate Session 不是线程安全的,所以我们需要一个策略来获取当前线程的当前会话。这种策略称为CurrentSessionContext。
当前会话是我们通过此调用获得的会话:
sessionFactory.getCurrentSession()
Hibernate 可以配置各种当前会话策略。 @UnitOfWork 使用这种策略:
hibernate.current_session_context_class = managed
对于此策略,您应该通过显式调用
将会话置于上下文中
ManagedSessionContext.bind(session)
因此,我们知道Session 不是线程安全的,您应该为单独的线程创建一个新会话并将该会话放入ManagedSessionContext。之后,您可以通过与@UnitOfWork 的端点方法相同的方式调用您的 DAO。
请记住,您应该在关闭会话之前取消绑定
ManagedSessionContext.unbind(factory)
您可以使用此实用程序类为单独的线程创建会话:
public final class HibernateSessionUtils {
private HibernateSessionUtils() {
}
public static void request(SessionFactory factory, Runnable request) {
request(factory, () -> {
request.run();
return null;
});
}
public static <T> T request(SessionFactory factory, Supplier<T> request) {
Transaction txn = null;
Session session = factory.openSession();
try {
ManagedSessionContext.bind(session);
txn = session.beginTransaction();
T result = request.get();
commit(txn);
return result;
} catch (Throwable th) {
rollback(txn);
throw Throwables.propagate(th);
} finally {
session.close();
ManagedSessionContext.unbind(factory);
}
}
private static void rollback(Transaction txn) {
if (txn != null && txn.isActive()) {
txn.rollback();
}
}
private static void commit(Transaction txn) {
if (txn != null && txn.isActive()) {
txn.commit();
}
}
}
Throwables 来自guava。
可以这样使用
List<Campaign> getCampaigns(SessionFactory factory, CampaignDao dao) {
return HibernateSessionUtils.request(
factory,
dao::getCampaigns
);
}
在dao.getCampaigns()方法中可以获取会话
sessionFactory.getCurrentSession()
您可以使用Guice 在任何地方注入工厂。
其他选项是使用UnitOfWorkAwareProxyFactory。