服务端新开多线程使用HibernateSession 杜绝No session
新起的线程需要绑定Hibernate session,才能在新线程中使用事务和延迟加载等功能,否则会曝出no session异常;
解决办法:
[java] view plaincopy
- new Runnable() {
- @Override
- public void run() {
- // ----------绑定session到当前线程------------
- SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
- boolean participate = ConcurrentUtil.bindHibernateSessionToThread(sessionFactory);
- // ---------你的业务---------------
- <pre name="code" class="java">// ----------关闭session------------
- ConcurrentUtil.closeHibernateSessionFromThread(participate, sessionFactory);
- }
- }
bindHibernateSessionToThread方法:
[java] view plaincopy
- 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方法
[java] view plaincopy
- 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。
时间: 2024-09-29 17:49:13