---
title: "多线程中使用HIBERNATE,解决NO SESSION 问题"
author: "lvjun106"
date: "2015-07-22T08:37:06Z"
tags: []
categories: ["开源软件"]
slug: "hibernate-no-session"
draft: false
lastmod: "2015-07-22T08:37:06Z"
id: 2229233
url: "https://lvjun106.iteye.com/blog/2229233"
---

本文转自网络：

 

新起的线程需要绑定Hibernate session，才能在新线程中使用事务和延迟加载等功能，否则会曝出no session异常；

解决办法：

 

**\[java]** [view plain](http://blog.csdn.net/zhengwei223/article/details/30506455 "view plain")[copy](http://blog.csdn.net/zhengwei223/article/details/30506455 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/391334 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/391334/fork "派生到我的代码片")

 

01. new Runnable() {
02. @Override
03. public void run() {
04. //                  ----------绑定session到当前线程------------
05. SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
06. boolean participate = ConcurrentUtil.bindHibernateSessionToThread(sessionFactory);
07. //                  ---------你的业务---------------
08. &lt;pre name="code" class="java"&gt;//                  ----------关闭session------------
09. ConcurrentUtil.closeHibernateSessionFromThread(participate, sessionFactory);
10. }
11. }

 

```

```

bindHibernateSessionToThread方法：

 

 

**\[java]** [view plain](http://blog.csdn.net/zhengwei223/article/details/30506455 "view plain")[copy](http://blog.csdn.net/zhengwei223/article/details/30506455 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/391334 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/391334/fork "派生到我的代码片")

 

01. public static boolean bindHibernateSessionToThread(SessionFactory sessionFactory) {
02. if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
03. // Do not modify the Session: just set the participate flag.
04. return true;
05. } else {
06. Session session = sessionFactory.openSession();
07. session.setFlushMode(FlushMode.MANUAL);
08. SessionHolder sessionHolder = new SessionHolder(session);
09. TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
10. }
11. return false;
12. }

closeHibernateSessionFromThread方法

 

 

**\[java]** [view plain](http://blog.csdn.net/zhengwei223/article/details/30506455 "view plain")[copy](http://blog.csdn.net/zhengwei223/article/details/30506455 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/391334 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/391334/fork "派生到我的代码片")

 

1. public static void closeHibernateSessionFromThread(boolean participate, Object sessionFactory) {
2. if (!participate) {
3. SessionHolder sessionHolder = (SessionHolder)TransactionSynchronizationManager.unbindResource(sessionFactory);
4. SessionFactoryUtils.closeSession(sessionHolder.getSession());
5. }
6. }

 

 

事务边界则由aop或者Transactional标记来控制，示例代码只是保证具备事务性的方法在需要的时候能从当前线程中获得session对象。

上述代码大部分截取自Spring的OpenSessionInViewFilter。