package com.mall.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateThreadUtil {
private static SessionFactory sessionFactory = buildSessionFactory();
public static final ThreadLocal session = new ThreadLocal();
private static SessionFactory buildSessionFactory(){
try{
Configuration configuration = new Configuration().configure(); // 实例化配置文件
ServiceRegistry serviceRegistry =new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // 实例化服务登记
sessionFactory = configuration.buildSessionFactory(serviceRegistry); // 获取Session工厂
//sessionFactory = new Configuration().configure().buildSessionFactory();
return sessionFactory;
}catch(Throwable ex){
throw new ExceptionInInitializerError(ex);
}
}
/*static{
try{
Configuration configuration = new Configuration().configure(); // 实例化配置文件
ServiceRegistry serviceRegistry =new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // 实例化服务登记
sessionFactory = configuration.buildSessionFactory(serviceRegistry); // 获取Session工厂
//sessionFactory = new Configuration().configure().buildSessionFactory();
}catch(Throwable ex){
throw new ExceptionInInitializerError(ex);
}
}*/
/**
- 获得当前session
- @return session
- @throws HibernateException */ public static Session getSession()throws HibernateException{ Session s = (Session)session.get(); if(s==null||!s.isOpen()){ s = sessionFactory.openSession(); session.set(s); } return s; }
/**
- 回滚
- */ public static void rollback(Transaction tx){ try{ if(tx!=null){ tx.rollback(); } }catch(HibernateException e){ System.out.println("rollback faild:"+e); } }
/**
- 关闭session
- */ public static void closeSession()throws HibernateException{ Session s = (Session)session.get(); session.set(null); if(s!=null){ s.close(); } } }
警告: The web application [Mall] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
五月 24, 2016 1:14:40 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
警告: The web application [Mall] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(Unknown Source)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:41)
五月 24, 2016 1:14:40 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
警告: The web application [Mall] appears to have started a thread named [pool-1-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(Unknown Source)
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)