hibernatesessionfactory.java
来自「该车间信息管理系统」· Java 代码 · 共 41 行
JAVA
41 行
package com.briup.common;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.cfg.Configuration;public class HibernateSessionFactory { private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static final Configuration cfg = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; public static Session getSession() throws HibernateException { Session session = (Session)threadLocal.get(); if(session == null || !session.isOpen()) { if(sessionFactory == null) { try { cfg.configure(CONFIG_FILE_LOCATION); sessionFactory = cfg.buildSessionFactory(); }catch(Exception e) { System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); } return session; } public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if(session != null) { session.close(); } } private HibernateSessionFactory() {}}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?