package com.vci.server.query.util;
|
|
import java.sql.Connection;
|
|
import org.hibernate.Session;
|
import org.hibernate.SessionFactory;
|
import org.hibernate.cfg.Configuration;
|
import org.hibernate.impl.SessionImpl;
|
import org.hibernate.jdbc.JDBCContext;
|
|
import com.vci.common.log.ServerWithLog4j;
|
|
public class HibernateSessionUtil {
|
|
private static ThreadLocal<Session> session = new ThreadLocal<Session>();
|
private static SessionFactory sessionFactory;
|
|
static{
|
try{
|
sessionFactory = new Configuration().configure("properties/hibernate.cfg.xml").buildSessionFactory();
|
}catch (Throwable e){
|
e.printStackTrace();
|
ServerWithLog4j.logger.error("Initial SessionFactory fail...", e);
|
}
|
}
|
|
|
public static Session getSession(){
|
Session s = session.get();
|
if(s == null){
|
s = sessionFactory.openSession();
|
session.set(s);
|
}
|
return s;
|
}
|
|
public static void closeSession(){
|
Session s = session.get();
|
if(s != null){
|
s.close();
|
session.set(null);
|
}
|
}
|
|
public static Connection getConnection(){
|
SessionImpl sessionImpl = (SessionImpl)getSession();
|
JDBCContext jdbcContext = sessionImpl.getJDBCContext();
|
return jdbcContext.getConnectionManager().getConnection();
|
}
|
}
|