KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > container > persistence > hibernate > HibernateSession


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: HibernateSession.java,v $
31  * Revision 1.2 2005/04/09 17:19:37 colinmacleod
32  * Changed copyright text to GPL v2 explicitly.
33  *
34  * Revision 1.1 2005/03/10 19:23:04 colinmacleod
35  * Moved to ivata groupware.
36  *
37  * Revision 1.3 2004/11/12 15:57:11 colinmacleod
38  * Removed dependencies on SSLEXT.
39  * Moved Persistence classes to ivata masks.
40  *
41  * Revision 1.2 2004/08/01 11:54:50 colinmacleod
42  * Improved transaction handling.
43  *
44  * Revision 1.1 2004/07/13 19:42:44 colinmacleod
45  * Moved project to POJOs from EJBs.
46  * Applied PicoContainer to services layer (replacing session EJBs).
47  * Applied Hibernate to persistence layer (replacing entity EJBs).
48  * -----------------------------------------------------------------------------
49  */

50 package com.ivata.groupware.container.persistence.hibernate;
51
52 import java.sql.Connection JavaDoc;
53
54 import net.sf.hibernate.HibernateException;
55 import net.sf.hibernate.Session;
56 import net.sf.hibernate.Transaction;
57
58 import org.apache.log4j.Logger;
59
60 import com.ivata.mask.persistence.PersistenceException;
61 import com.ivata.mask.persistence.PersistenceSession;
62
63 /**
64  * <p>
65  * Adaptor to wrap the <strong>Hibernate</strong> session.
66  * </p>
67  *
68  * @author Colin MacLeod
69  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
70  * @since Mar 27, 2004
71  * @version $Revision: 1.2 $
72  */

73 public class HibernateSession implements PersistenceSession {
74     /**
75      * Refer to {@link Logger}.
76      */

77     private static Logger log = Logger.getLogger(HibernateSession.class);
78     /**
79      * If <code>true</code> the transaction will be rolled back.
80      */

81     private boolean cancel = false;
82     /**
83      * <p>
84      * The Hibernate session we're adapting.
85      * </p>
86      */

87     private Session session;
88
89     private Object JavaDoc systemSession;
90
91     /**
92      * Hibernate transaction being wrapped.
93      */

94     private Transaction transaction;
95
96     /**
97      * Create a new instance wrapping the given hibernate session, and
98      * transaction,
99      */

100     public HibernateSession(Session sessionParam, Transaction transactionParam,
101             Object JavaDoc systemSessionParam) {
102         this.session = sessionParam;
103         this.transaction = transactionParam;
104         this.systemSession = systemSessionParam;
105     }
106
107     /**
108      * Refer to {@link com.ivata.mask.persistence.PersistenceSession#cancel}.
109      *
110      * @throws PersistenceException
111      * Refer to {@link com.ivata.mask.persistence.PersistenceSession#cancel}.
112      */

113     public void cancel() throws PersistenceException {
114         cancel = true;
115     }
116
117     /**
118      * @see com.ivata.mask.persistence.PersistenceSession#commit()
119      */

120     public void close() throws PersistenceException {
121         HibernateException hibernateException = null;
122         if (cancel) {
123             try {
124                 transaction.rollback();
125             } catch (HibernateException e) {
126                 log.error("("
127                         + e.getClass().getName()
128                         + ") ROLLING BACK TRANSACTION: "
129                         + e.getMessage(), e);
130             } finally {
131                 try {
132                     session.close();
133                 } catch (HibernateException e) {
134                     if (hibernateException != null) {
135                         hibernateException = e;
136                     }
137                 }
138             }
139
140         } else {
141             try {
142                 if (!transaction.wasRolledBack()) {
143                     transaction.commit();
144                 }
145             } catch (HibernateException e) {
146                 hibernateException = e;
147                 try {
148                     transaction.rollback();
149                 } catch (Exception JavaDoc eRollback) {
150                     log.error("("
151                             + e.getClass().getName()
152                             + ") ROLLING BACK TRANSACTION: "
153                             + e.getMessage(), e);
154                 }
155             } finally {
156                 try {
157                     session.close();
158                 } catch (HibernateException e) {
159                     if (hibernateException != null) {
160                         hibernateException = e;
161                     }
162                 }
163             }
164         }
165         if (hibernateException != null) {
166             throw new PersistenceException("Error closing hibernate persistence session: ",
167                 hibernateException);
168         }
169     }
170
171     /**
172      * TODO
173      *
174      * @see com.ivata.mask.persistence.PersistenceSession#getConnection()
175      */

176     public final Connection JavaDoc getConnection() throws PersistenceException {
177         try {
178             return session.connection();
179         } catch (HibernateException e) {
180             throw new PersistenceException(e);
181         }
182     }
183
184     /**
185      * Get the hibernate session this session adapts.
186      *
187      * @param session valid hibernate session.
188      */

189     Session getSession() {
190         return session;
191     }
192
193     /**
194      * @return Returns the systemSession.
195      */

196     public Object JavaDoc getSystemSession() {
197         return systemSession;
198     }
199     /**
200      * @return transaction
201      */

202     public Transaction getTransaction() {
203         return transaction;
204     }
205 }
206
Popular Tags