博主原文链接: https://blog.csdn.net/zhengwei223/article/details/30506455
新起的线程需要绑定Hibernate session,才能在新线程中使用事务和延迟加载等功能,否则会曝出no session异常;
解决办法:
//业务方法
new Runnable() {
@Override
public void run() {
//----------绑定session到当前线程------------
SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
boolean participate = ConcurrentUtil.bindHibernateSessionToThread(sessionFactory);
//---------你的业务---------------
//TODO 哈喽
// ----------关闭session------------
ConcurrentUtil.closeHibernateSessionFromThread(participate, sessionFactory);
}
}
//工具方法:bindHibernateSessionToThread
public static boolean bindHibernateSessionToThread(SessionFactory sessionFactory) {
if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
// Do not modify the Session: just set the participate flag.
return true;
} else {
Session session = sessionFactory.openSession();
session.setFlushMode(FlushMode.MANUAL);
SessionHolder sessionHolder = new SessionHolder(session);
TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
}
return false;
}
//工具方法:closeHibernateSessionFromThread
public static void closeHibernateSessionFromThread(boolean participate, Object sessionFactory) {
if (!participate) {
SessionHolder sessionHolder = (SessionHolder)TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSession(sessionHolder.getSession());
}
}
事务边界则由aop或者Transactional标记来控制,
示例代码只是保证具备事务性的方法在需要的时候能从当前线程中获得session对象。
上述代码大部分截取自Spring的OpenSessionInViewFilter。
PS:以上内容转载自https://blog.csdn.net/zhengwei223/article/details/30506455