1 7 package org.jboss.hibernate.session; 8 9 import javax.naming.InitialContext ; 10 import javax.naming.NamingException ; 11 12 import org.hibernate.HibernateException; 13 import org.hibernate.Session; 14 import org.hibernate.SessionFactory; 15 16 28 public class HibernateContext 29 { 30 48 public static Session getUnmanagedSession(String name) throws HibernateException, IllegalStateException 49 { 50 SessionFactory sf = locateSessionFactory( name ); 51 return sf.openSession( sf.getCurrentSession().connection() ); 52 } 53 54 62 public static void releaseUnmanagedSession(Session unmanagedSession) throws HibernateException 63 { 64 unmanagedSession.close(); 65 } 66 67 79 public static Session getSession(String name) 80 { 81 return locateSessionFactory( name ).getCurrentSession(); 82 } 83 84 private static SessionFactory locateSessionFactory(String name) throws HibernateException 85 { 86 InitialContext context = null; 87 try 88 { 89 context = new InitialContext (); 90 final SessionFactory factory = ( SessionFactory ) context.lookup(name); 91 return factory; 92 } 93 catch( NamingException e ) 94 { 95 throw new HibernateException( "Unable to locate SessionFactory in JNDI under name [" + name + "]", e ); 96 } 97 finally 98 { 99 release( context ); 100 } 101 } 102 103 private static void release(InitialContext ctx) 104 { 105 if (ctx != null) 106 { 107 try 108 { 109 ctx.close(); 110 } 111 catch( Throwable ignore ) 112 { 113 } 115 } 116 } 117 } 118 | Popular Tags |