KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > hibernate > session > HibernateContext


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.hibernate.session;
8
9 import javax.naming.InitialContext JavaDoc;
10 import javax.naming.NamingException JavaDoc;
11
12 import org.hibernate.HibernateException;
13 import org.hibernate.Session;
14 import org.hibernate.SessionFactory;
15
16 /**
17  * Maintains and exposes, for app usage, the current context bound Hibernate Session.
18  * Application code need only deal with the {@link #getSession(java.lang.String)}
19  * as the means to retreive the {@link org.hibernate.Session} associated with
20  * the current context.
21  *
22  * @author <a HREF="mailto:steve@hibernate.org">Steve Ebersole</a>
23  * @version $Revision: 35017 $
24  *
25  * @deprecated Direct use of the new {@link org.hibernate.SessionFactory#getCurrentSession()}
26  * method is the preferred approach to managing "transactionally contextual sessions".
27  */

28 public class HibernateContext
29 {
30    /**
31     * Retreives an "unmanaged" session against the same underlying jdbc
32     * connnection as the session currently bound to the current context for
33     * the given JNDI name. This is simply a convenience method for
34     * SessionFactory.openSession({@link #getSession}.connection()). Unmanaged
35     * here means that the returned session is not controlled by the code
36     * managing the actually bound session; callers are required to cleanup
37     * these sessions manually using {@link #releaseUnmanagedSession}.
38     *
39     * @param name The "name" of the {@link org.hibernate.SessionFactory}
40     * for which an unmanaged session is requested.
41     * @return An unmanaged session.
42     * @throws HibernateException If an error occurs opening the new Session.
43     * @throws IllegalStateException If unable to locate a managed Session for the current context.
44     *
45     * @deprecated Given a SessionFactory, sf, obtained from JNDI, this method is equivalent to
46     * <pre>sf.openSession( sf.getCurrentSession().connection() )</pre>
47     */

48    public static Session getUnmanagedSession(String JavaDoc name) throws HibernateException, IllegalStateException JavaDoc
49    {
50       SessionFactory sf = locateSessionFactory( name );
51       return sf.openSession( sf.getCurrentSession().connection() );
52    }
53
54    /**
55     * Method to release a previously obtained unmanaged session.
56     *
57     * @param unmanagedSession The unmanaged Session to release.
58     * @throws HibernateException If an error occurs releasing the unmanaged Session.
59     *
60     * @deprecated See {@link #getUnmanagedSession(String)}
61     */

62    public static void releaseUnmanagedSession(Session unmanagedSession) throws HibernateException
63    {
64       unmanagedSession.close();
65    }
66
67    /**
68     * Retreives the session currently bound to the current context.
69     *
70     * @param name The "name" of the {@link org.hibernate.SessionFactory}
71     * for which a session is requested.
72     * @return The current session.
73     *
74     * @deprecated This call is equivalent to <pre>
75     * ( ( SessionFactory ) new InitialContext().lookup( name ) ).getCurrentSession()
76     * </pre>.
77     * @see org.hibernate.SessionFactory#getCurrentSession()
78     */

79    public static Session getSession(String JavaDoc name)
80    {
81       return locateSessionFactory( name ).getCurrentSession();
82    }
83
84    private static SessionFactory locateSessionFactory(String JavaDoc name) throws HibernateException
85    {
86       InitialContext JavaDoc context = null;
87       try
88       {
89          context = new InitialContext JavaDoc();
90          final SessionFactory factory = ( SessionFactory ) context.lookup(name);
91          return factory;
92       }
93       catch( NamingException JavaDoc 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 JavaDoc ctx)
104    {
105       if (ctx != null)
106       {
107          try
108          {
109             ctx.close();
110          }
111          catch( Throwable JavaDoc ignore )
112          {
113             // ignore
114
}
115       }
116    }
117 }
118
Popular Tags