ThreadLocal
来源:互联网 发布:德州力拓软件 编辑:程序博客网 时间:2024/06/09 22:51
package main.cn.happy.entity.cn.hql02.hql.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/** * Created by lenovo on 2017/10/6. */public class hibernateUtil { static ThreadLocal<Session> tlSession; static Configuration cfg; static SessionFactory sessionFactory; // 静态代码块 static { //创建configure 对象 cfg = new Configuration().configure(); // sessionFactory = cfg.buildSessionFactory(); tlSession = new ThreadLocal<Session>(); } public static Session getSession() { Session session = tlSession.get(); if (session == null) { session = sessionFactory.openSession(); tlSession.set(session); } return session; } // 关闭 public void Close() { Session session = tlSession.get(); if (session != null) { session.close(); tlSession.set(null); } }}
------------------------------------------------------
package main.cn.happy.entity.cn.hql02.hql.util;import org.hibernate.Session;/** * Created by lenovo on 2017/10/6. */public class MyThread extends Thread { @Override public void run() { Session session = hibernateUtil.getSession(); Session session2 = hibernateUtil.getSession(); System.out.println(" 子线程=== 1 " + session.hashCode()); System.out.println(" 子线程=== 2 " + session2.hashCode()); }}
阅读全文
0 0
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- ThreadLocal
- threadlocal
- ThreadLocal
- ThreadLocal
- 工厂模式浅述
- virtualbox中ubuntu和windows共享文件夹设置
- @Temporal使用
- Linux系统常用命令
- 菜鸟程序员的自我思想工作-jsp之servlet入门(2)
- ThreadLocal
- SSL2756 2017年10月5提高组T1 独立集(最长上升子序列+二分)
- 二叉树相关面试题总结
- python的几种常用数据类型
- 图
- 写一个函数返回参数二进制中1的个数
- 解决 ”Event filter with query "SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstan“
- PL/SQL
- HDU 5970 最大公约数(找规律)