session是hibernate运做的核心,是有SessionFactory所创建,sessionFactory是线程安全的,你可以让多个线程同时存取SessionFactory,而不会有资源共用的问题,然而session不是设计为线程安全的,所以让多个线程共用一个session,将发生资料共用而发生混乱的问题.下面是一个标准类.
import java.io.Serializable;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
public class HibernateSessionUtil implements Serializable
{
//创建线程局部变量 tLocalsess
public static final ThreadLocal tLocalsess = new ThreadLocal();
//创建线程局部变量 tLocaltx
public static final ThreadLocal tLocaltx = new ThreadLocal();
//取得session
public static Session currentSession(){
//从线程变量tLocalsess中,取得当前session
Session session = (Session) tLocalsess.get();
//判断session是否为空,如果为空,将创建一个session,并付给线程变量tLocalsess
try{
if (session == null){
session = openSession();
tLocalsess.set(session);
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
return session;
}
//关闭当前session
public static void closeSession(){
//从线程变量tLocalsess中,取得当前session
Session session = (Session) tLocalsess.get();
//设置线程变量tLocalsess为空
tLocalsess.set(null);
try{
//关闭session
if (session != null && session.isOpen()){
session.close();
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
//事物处理
public static void beginTransaction(){
//从线程变量tLocaltx中取得事物管理对象Transaction
Transaction tx = (Transaction) tLocaltx.get();
try{
//如果为空就从session中创建一个tx
if (tx == null){
tx = currentSession().beginTransaction();
tLocaltx.set(tx);
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
//提交事物
public static void commitTransaction(){
//取得事物
Transaction tx = (Transaction) tLocaltx.get();
try{
//如果不为空就提交
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
tLocaltx.set(null);
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
//事物回滚
public static void rollbackTransaction(){
//取得tx事物
Transaction tx = (Transaction) tLocaltx.get();
try{
//将变量清空
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
//事物回滚
tx.rollback();
}
}catch (HibernateException e){
throw new InfrastructureException(e);
}
}
//取得session
private static Session openSession() throws HibernateException{
return getSessionFactory().openSession();
}
//取得sessionFactory
private static SessionFactory getSessionFactory() throws HibernateException{
return SingletonSessionFactory.getInstance();
}
}
filter的代码:
public class HibernateSessionCloser implements Filter{
protected FilterConfig filterConfig = null;
public void init(FilterConfig filterConfig)throws ServletException{
this.filterConfig = filterConfig;
}
public void destroy(){
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
try{
chain.doFilter(request, response);
}
finally{
try{
HibernateSessionUtil.commitTransaction();
}catch (InfrastructureException e){
HibernateSessionUtil.rollbackTransaction();
}finally{
HibernateSessionUtil.closeSession();
}
}
}
}