private static final ThreadLocal threadSession = new ThreadLocal(); public static Session getSession() throws InfrastructureException { Session s = (Session) threadSession.get(); try { if (s == null) { s = getSessionFactory().openSession(); threadSession.set(s); } } catch (HibernateException ex) { throw new InfrastructureException(ex); } return s; }
public class ConnectionUtil { private static ThreadLocaltl = new ThreadLocal (); private static Connection initConn = null; static { try { initConn = DriverManager.getConnection("url, name and password"); } catch (SQLException e) { e.printStackTrace(); } } public Connection getConn() { Connection c = tl.get(); if(null == c) tl.set(initConn); return tl.get(); } }