KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > hibernate > HibernateUtils


1 package org.apache.turbine.util.hibernate;
2  
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import net.sf.hibernate.HibernateException;
20 import net.sf.hibernate.JDBCException;
21 import net.sf.hibernate.Session;
22 import net.sf.hibernate.SessionFactory;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28  * This class is used to get Hibernate Sessions and may
29  * also contain methods (in the future) to get DBConnections
30  * or Transactions from JNDI.
31  *
32  * @version $Id: HibernateUtils.java,v 1.1.2.1 2004/02/27 05:12:43 seade Exp $
33  */

34 public class HibernateUtils
35 {
36     //~ Static fields/initializers =============================================
37
public final static String JavaDoc SESSION_FACTORY = "hibernate/sessionFactory";
38     public static final ThreadLocal JavaDoc session = new ThreadLocal JavaDoc();
39     private static SessionFactory sf = null;
40     private static HibernateUtils me;
41     private static Log log = LogFactory.getLog(HibernateUtils.class);
42
43     static {
44         try
45         {
46             me = new HibernateUtils();
47         }
48         catch (Exception JavaDoc e)
49         {
50             log.fatal("Error occurred initializing HibernateUtils");
51             e.printStackTrace();
52         }
53     }
54
55     //~ Constructors ===========================================================
56

57     private HibernateUtils() throws HibernateException, JDBCException
58     {}
59
60     //~ Methods ================================================================
61

62     public static Session currentSession() throws PersistenceException
63     {
64         Session s = (Session) session.get();
65
66         if (s == null)
67         {
68             s = PersistenceManager.openSession();
69             if (log.isDebugEnabled())
70             {
71                 log.debug("Opened hibernate session.");
72             }
73
74             session.set(s);
75         }
76
77         return s;
78     }
79
80     public static void closeSession() throws HibernateException, JDBCException
81     {
82         Session s = (Session) session.get();
83         session.set(null);
84
85         if (s != null)
86         {
87             if (s.isOpen())
88             {
89                 s.flush();
90                 s.close();
91
92                 if (log.isDebugEnabled())
93                 {
94                     log.debug("Closed hibernate session.");
95                 }
96             }
97         }
98         else
99         {
100             log.warn("Hibernate session was inadvertently already closed.");
101
102         }
103     }
104 }
105
Popular Tags