HibernateUtil 初始化类的创建
来源:互联网 发布:js 数组转json 编辑:程序博客网 时间:2024/06/03 14:24
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
//为保证线程安全,将Seeeion放到ThreadLocal中管理。这样就避免了Session的多线程共享数据的问题
private static final ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
//为保证线程安全,将Seeeion放到ThreadLocal中管理。这样就避免了Session的多线程共享数据的问题
private static SessionFactory sessionFactory=null;
static {
try {
Configuration cfg=new Configuration().configure();
sessionFactory =cfg.buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
System.out.println("创建会话工厂失败");
e.printStackTrace();
}
}
public static Session getSession() throws HibernateException{
Session session=(Session)threadLocal.get();
if (session==null||!session.isOpen()) {
if (sessionFactory==null) {
rebuildSessionFactory();
}
session=(sessionFactory!=null)?sessionFactory.openSession():null;
threadLocal.set(session);//并且为了线程安全,将它放进线程锁中
}
return session;
}
public static void rebuildSessionFactory() {
// TODO Auto-generated method stub
try {
Configuration cfg=new Configuration().configure();
sessionFactory=cfg.buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
System.out.println("创建会话工厂失败");
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void closeSession() throws HibernateException{
Session session=(Session)threadLocal.get();
threadLocal.set(null);
if (session!=null) {
session.close();
}
}
}
static {
try {
Configuration cfg=new Configuration().configure();
sessionFactory =cfg.buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
System.out.println("创建会话工厂失败");
e.printStackTrace();
}
}
public static Session getSession() throws HibernateException{
Session session=(Session)threadLocal.get();
if (session==null||!session.isOpen()) {
if (sessionFactory==null) {
rebuildSessionFactory();
}
session=(sessionFactory!=null)?sessionFactory.openSession():null;
threadLocal.set(session);//并且为了线程安全,将它放进线程锁中
}
return session;
}
public static void rebuildSessionFactory() {
// TODO Auto-generated method stub
try {
Configuration cfg=new Configuration().configure();
sessionFactory=cfg.buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
System.out.println("创建会话工厂失败");
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void closeSession() throws HibernateException{
Session session=(Session)threadLocal.get();
threadLocal.set(null);
if (session!=null) {
session.close();
}
}
}
纯粹是因为懒
阅读全文
0 0
- HibernateUtil 初始化类的创建
- 典型的hibernateUtil类
- HibernateUtil工具类的写法
- Hibernate的HibernateUtil类补充
- HibernateUtil工具类的编写
- hibernateUtil工具类的封装
- HibernateUtil类
- netbean 的 hibernateUtil 类编写的问题
- HibernateUtil类(操作数据源 session 的管理)
- Hibernate的辅助类——HibernateUtil
- hibernate4.0以上的hibernateUtil工具类
- hibernate4之后HibernateUtil类的编写
- 请问各位高手,我出现了一个令我头痛的问题:在HibernateUtil类中创建SessionFactory总是失败:
- 孙未勤的HibernateUtil
- 孙未勤的HibernateUtil
- 类的创建和初始化
- 一个HibernateUtil类
- HibernateUtil 工具类
- 原生js实现对class的增删改
- NOIP2017 金秋杯系列模拟赛 noip 提高组模拟赛 day1 2
- JDK动态代理原理
- html内容适应手机端
- 三分法专练 hdu
- HibernateUtil 初始化类的创建
- LeetCode#513 Find Bottom Left Tree Value
- CDH 5.10 取消kerbers认证
- FACADE 外观模式 --对象结构型模式
- 进程间通讯——信号
- scrapy-cmd下常用命令
- 关于MySQL锁的一些试验
- webpack入门
- linux node环境配置