KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > util > HibernateUtil


1 /*
2  * $Id: HibernateUtil.java,v 1.8 2005/01/17 21:36:35 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.util;
27
28 import net.sf.hibernate.HibernateException;
29 import net.sf.hibernate.MappingException;
30 import net.sf.hibernate.Session;
31 import net.sf.hibernate.SessionFactory;
32 import net.sf.hibernate.Transaction;
33 import net.sf.hibernate.cfg.Configuration;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 import com.j2biz.blogunity.IConstants;
39 import com.j2biz.blogunity.exception.BlogunityRuntimeException;
40 import com.j2biz.blogunity.i18n.I18N;
41 import com.j2biz.blogunity.i18n.I18NStatusFactory;
42
43 /**
44  * @author michelson
45  * @version $$
46  * @since 0.1
47  *
48  *
49  */

50 public class HibernateUtil {
51     /**
52      * Logger for this class
53      */

54     private static final Log log = LogFactory.getLog(HibernateUtil.class);
55
56     private static SessionFactory sessionFactory = null;
57
58     private static final ThreadLocal JavaDoc threadSession = new ThreadLocal JavaDoc();
59
60     private static final ThreadLocal JavaDoc threadTransaction = new ThreadLocal JavaDoc();
61
62     static {
63         Configuration cfg = new Configuration();
64         try {
65
66             for (int i = 0; i < IConstants.PERSISTENT_CLASSES.length; i++) {
67                 cfg.addClass(IConstants.PERSISTENT_CLASSES[i]);
68             }
69
70             sessionFactory = cfg.buildSessionFactory();
71
72         } catch (MappingException e) {
73             if (log.isErrorEnabled()) {
74                 log.error("Error initializing SessionFactory!", e);
75             }
76
77         } catch (HibernateException e) {
78             if (log.isErrorEnabled()) {
79                 log.error("Error initializing SessionFactory!", e);
80             }
81
82         }
83
84     }
85
86     // public static void buildSessionFactory(Configuration cfg) throws
87
// HibernateException {
88
// sessionFactory = cfg.buildSessionFactory();
89
// }
90

91     public static Session getSession() {
92         if (log.isDebugEnabled()) {
93             log.debug("getSession() - start");
94         }
95
96         Session s = (Session) threadSession.get();
97         // Open a new Session, if this thread has none yet
98
try {
99             if (s == null) {
100                 s = sessionFactory.openSession();
101                 threadSession.set(s);
102             }
103         } catch (HibernateException ex) {
104             log.error("getSession()", ex);
105             throw new BlogunityRuntimeException(I18NStatusFactory.create(
106                     I18N.ERRORS.OPEN_HIBERNATE_SESSION_FAILED, ex));
107         }
108
109         if (log.isDebugEnabled()) {
110             log.debug("getSession() - end");
111         }
112         return s;
113     }
114
115     public static void closeSession() {
116         if (log.isDebugEnabled()) {
117             log.debug("closeSession() - start");
118         }
119
120         try {
121             Session s = (Session) threadSession.get();
122             threadSession.set(null);
123             if (s != null && s.isOpen()) s.close();
124         } catch (HibernateException ex) {
125             log.error("closeSession()", ex);
126
127             throw new BlogunityRuntimeException(I18NStatusFactory.create(
128                     I18N.ERRORS.CLOSE_HIBERNATE_SESSION_FAILED, ex));
129         }
130
131         if (log.isDebugEnabled()) {
132             log.debug("closeSession() - end");
133         }
134     }
135
136     public static void beginTransaction() {
137         if (log.isDebugEnabled()) {
138             log.debug("beginTransaction() - start");
139         }
140
141         Transaction tx = (Transaction) threadTransaction.get();
142         try {
143             if (tx == null) {
144                 tx = getSession().beginTransaction();
145                 threadTransaction.set(tx);
146             }
147         } catch (HibernateException ex) {
148             log.error("beginTransaction()", ex);
149
150             throw new BlogunityRuntimeException(I18NStatusFactory.create(
151                     I18N.ERRORS.BEGIN_TRANSACTION_FAILED, ex));
152         }
153
154         if (log.isDebugEnabled()) {
155             log.debug("beginTransaction() - end");
156         }
157     }
158
159     public static void commitTransaction() {
160         if (log.isDebugEnabled()) {
161             log.debug("[START] commitTransaction()");
162         }
163
164         Transaction tx = (Transaction) threadTransaction.get();
165         try {
166             if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
167
168                 tx.commit();
169             }
170             threadTransaction.set(null);
171         } catch (HibernateException ex) {
172             log.error("commitTransaction()", ex);
173
174             rollbackTransaction();
175             throw new BlogunityRuntimeException(I18NStatusFactory.create(
176                     I18N.ERRORS.COMMIT_TRANSACTION_FAILED, ex));
177         }
178
179         if (log.isDebugEnabled()) {
180             log.debug("[END] commitTransaction()");
181         }
182     }
183
184     public static void rollbackTransaction() {
185         if (log.isDebugEnabled()) {
186             log.debug("rollbackTransaction() - start");
187         }
188
189         Transaction tx = (Transaction) threadTransaction.get();
190         try {
191             threadTransaction.set(null);
192             if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
193
194                 tx.rollback();
195             }
196         } catch (HibernateException ex) {
197             log.error("rollbackTransaction()", ex);
198
199             throw new BlogunityRuntimeException(I18NStatusFactory.create(
200                     I18N.ERRORS.ROLBACK_TRANSACTION_FAILED, ex));
201         } finally {
202
203             closeSession();
204         }
205
206         if (log.isDebugEnabled()) {
207             log.debug("rollbackTransaction() - end");
208         }
209     }
210 }
211
212
Popular Tags