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

解决办法:

  1. new Runnable() {
  2. @Override
  3. public void run() {
  4. //                  ----------绑定session到当前线程------------
  5. SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
  6. boolean participate = ConcurrentUtil.bindHibernateSessionToThread(sessionFactory);
  7. //                  ---------你的业务---------------
  8. <pre name="code" class="java">//                  ----------关闭session------------
  9. ConcurrentUtil.closeHibernateSessionFromThread(participate, sessionFactory);
  10. }
  11. }

bindHibernateSessionToThread方法:

  1. public static boolean bindHibernateSessionToThread(SessionFactory sessionFactory) {
  2. if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
  3. // Do not modify the Session: just set the participate flag.
  4. return true;
  5. } else {
  6. Session session = sessionFactory.openSession();
  7. session.setFlushMode(FlushMode.MANUAL);
  8. SessionHolder sessionHolder = new SessionHolder(session);
  9. TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
  10. }
  11. return false;
  12. }

closeHibernateSessionFromThread方法

  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。

java多线程使用HibernateSession 时报 No session 的解决办法(转)的更多相关文章

  1. paip java.net.SocketException No buffer space available的解决办法及总结

    java.net.SocketException No buffer space available的解决办法及总结 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来 ...

  2. 安装J2EE的SDK报错:could not find the required version of the Java(TM)2 Runtime Environment in '(null)'的解决办法。

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  3. org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session异常解决办法

    org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...

  4. com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1问题出现的原因及解决办法

    转自:https://blog.csdn.net/shinchan_/article/details/37818927 com/opensymphony/xwork2/spring/SpringObj ...

  5. java下载文件时文件名出现乱码的解决办法

    转: java下载文件时文件名出现乱码的解决办法 2018年01月12日 15:43:32 橙子橙 阅读数:6249   java下载文件时文件名出现乱码的解决办法: String userAgent ...

  6. SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法

    原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...

  7. RobotFramework+Appium 升级Appium v1.10.0后,执行click element时报错:InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported for this session,解决办法

    报错信息如下: debug] [35m[XCUITest][39m Connection to WDA timed out[debug] [35m[XCUITest][39m Connection t ...

  8. Weblogic跨域session冲突解决办法

    一.现象: 在WebLogic中,有两个不同域A(端口:9000)和B(端口:8000),应用CA在域A中,应用CB在域B中,进行如下操作: 1.先登录应用CA,再登录应用CB,然后,切换回应用CA, ...

  9. 在cmd窗口下运行Java程序时无法找到主类的解决办法

    我是Java的初学者,昨天在cmd窗口下运行一段Java程序时总是有问题,可以编译但无法执行. 也就是javac时正确,一旦java时就不对了,提示找不到或无法加载主类,经百度谷歌再加上自己的摸索终于 ...

随机推荐

  1. 【BZOJ 1033】 [ZJOI2008]杀蚂蚁antbuster

    Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的任 ...

  2. 【转】PLSQL developer 连接不上64位Oracle 的解决方法

    PLSQL developer 连接不上64位Oracle 的解决方法 快乐无极 , 2012/06/13 10:10 , 开发文档 , 评论(6) , 阅读(140430) , Via 本站原创 大 ...

  3. 3、颜色的字符串、十进制、十六进制相互转换(color convert between dec、hex and string )

    int color_int=***; 1.(十进制整数)转换成(十六进制的字符串) String color_hex = String.format("#%06X", (0xFFF ...

  4. HDU 5763 Another Meaning KMP+DP

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Time Limit: 2000/1000 MS (Java/ ...

  5. 在C#中创建word文档

    在下面文档中  首先引用word组件:Microsoft.Office.Interop.Word 在头文件中写上 using Word = Microsoft.Office.Interop.Word; ...

  6. Codeforces Round #204 (Div. 2)->D. Jeff and Furik

    D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. jQuery1.9.1源码分析--Events模块

    var rformElems = /^(?:input|select|textarea)$/i, rkeyEvent = /^key/, rmouseEvent = /^(?:mouse|contex ...

  8. linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制)

    linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制) 发表于2012//07由feng linux 本身的ugo rwx的权限,对于精确的权限控制很是力不从心的,ac ...

  9. POJ 2151 Check the difficulty of problems (概率dp)

    题意:给出m.t.n,接着给出t行m列,表示第i个队伍解决第j题的概率. 现在让你求:每个队伍都至少解出1题,且解出题目最多的队伍至少要解出n道题的概率是多少? 思路:求补集. 即所有队伍都解出题目的 ...

  10. SPL学习 迭代器

    主要学习内容: 慕课网的spl视频教程 阮一峰SPL学习笔记 http://www.ruanyifeng.com/blog/2008/07/php_spl_notes.html SPL类详解 http ...