我用代理接口和Filter实现了一个自动打开session和关闭session的辅助程序,原理是每一个请求在开始处理时给他分配一个连接,在请求结束后关闭这个连接.其代码如下(可能比较多)<1>代理类public abstract class TransactionManager implements InvocationHandler
{ /**
 * Comment for <code>LOG_FILE</code>
 */
public final String LOG_FILE = "dao.log" ; // 代理对象
protected Object orignalObj ; protected TransactionManager(Object orignalObj)
{ this.orignalObj = orignalObj ;
} /**
 * 使用InvocationHandler来完成Hibernate事务管理
 * 
 * @param proxy
 * @param method
 * @param args
 * @return java.lang.Object
 * @throws Throwable
 */
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{ Object rtv = null ;
Session session = HibernateUtil.currentSession( ) ;
if (!session.isConnected( ))
{// 如果Session没有连接
session.reconnect( ) ;
} try
{
rtv = processTransaction( proxy , method , args , session ) ;
}
catch (Exception e)
{
e.printStackTrace( ) ;
PrintStream writer = new PrintStream( new FileOutputStream( LOG_FILE ,
true ) , true ) ;
e.printStackTrace( writer ) ; // 打印日志信息到dao.log
writer.close( ) ;
throw e ;
}
finally
{
session.disconnect( ) ; // 断开和conn的连接
} return rtv ;
}
         ///这里是每个处理的具体实现,就不再列出
/**
 * 处理事务管理
 * <p>
 * 其子类可以实现此方法以便提供更加精确的事务控制功能
 * 
 * @param proxy
 * @param method
 * @param args
 * @param session
 * @return java.lang.Object
 * @throws Exception
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
protected abstract Object processTransaction(Object proxy, Method method,
Object[] args, Session session) throws Exception ;
}<2>Filter类public class SessionFilter implements Filter
{ /*
 * (non-Javadoc)
 * 
 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
 */
public void init(FilterConfig arg0) throws ServletException
{ // TODO Auto-generated method stub } /**
 * 绑定session到每个请求 (non-Javadoc)
 * 
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException
{ // TODO Auto-generated method stub
HibernateUtil.binding( ) ;
/*
 * 执行请求
 */
try{
arg2.doFilter( arg0 , arg1 ) ;
if(arg0 instanceof HttpServletRequest){
HttpServletRequest request = (HttpServletRequest)arg0;
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
arg0.setAttribute("basePath",basePath);
}
/*
 * 关闭当前请求上绑定的session
 */
}catch(Throwable e){
if(e.getClass().isInstance(IOException.class)){
throw new IOException();
}
if(e.getClass().isInstance(ServletException.class))
throw new ServletException();
/*
 * 关闭当前请求上绑定的session
 */
}finally{
System.out.println("调用完成,开始释放资源");
HibernateUtil.close( ) ;
}
} /*
 * (non-Javadoc)
 * 
 * @see javax.servlet.Filter#destroy()
 */
public void destroy()
{ // TODO Auto-generated method stub }}<3>最后是HibernateUtilpublic class HibernateUtil
{ private static SessionFactory sf ; // private static LazyInteceptor inteceptor = new LazyInteceptor();
private static final Logger logger = Logger.getLogger( HibernateUtil.class ) ; // 使用threadLocal来解决用户并发访问的问题以及实现线程级的Session重用
private static ThreadLocal threadLocal = new ThreadLocal( ) ; /**
 * 获取Hibernate会话,并将其绑定到当前线程
 * 
 * @return net.sf.hibernate.Session;
 */
public static Session binding()
{ Session session = (Session) threadLocal.get( ) ; try
{
if (session == null)
{
session = sf.openSession( ) ;
System.out.println( "start bingdin" ) ;
threadLocal.set( session ) ;
}
return session ;
}
catch (HibernateException e)
{
e.printStackTrace( ) ;
logger.error( "获取Session失败" ) ;
}
return null ;
} /**
 * 获取和当前线程相关的Session
 * 
 * @return net.sf.hibernate.Session;
 */
public static Session currentSession()
{ return binding( ) ;
} /**
 * 关闭和当前线程相关的Session
 * 
 * @throws HibernateException
 */
public static void close()
{ Session session = (Session) threadLocal.get( ) ; try
{
if (session != null)
{
session.close( ) ; // 关闭连接
System.out.println( "to close" ) ;
}
session = null ;
threadLocal.set( null ) ;
}
catch (HibernateException e)
{
e.printStackTrace( ) ;
System.out.println("关闭Session失败!");
logger.error( "关闭Session失败!" ) ; }
} /**
 * 加载Hibernate资源
 */
public static void load()
{ try
{
Configuration cfg = new Configuration( ).configure( ) ; // 读取hibernate.cfg.xml文件以便配置Hibernate
sf = cfg.buildSessionFactory( ) ;
}
catch (HibernateException e)
{
e.printStackTrace( ) ;
throw new RuntimeException( "Hibernate初始化失败!Cause:" + e.getMessage( ) ) ;
}
} /**
 * 销毁对象
 */
public static void destoryed()
{ sf = null ;
threadLocal = null ;
}
}
现在出现的问题是应用的访问量一大之后,应用会逐渐耗尽连接池的连接,出现pool exhausted.
但我从程序中实在无法找出问题,忘大家能够指点迷津,谢谢