001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata groupware may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: HibernateSession.java,v $
031: * Revision 1.8 2005/10/12 18:36:46 colinmacleod
032: * Standardized format of Logger declaration - to make it easier to find instances
033: * which are not both static and final.
034: *
035: * Revision 1.7 2005/10/11 18:51:38 colinmacleod
036: * Fixed some checkstyle and javadoc issues.
037: *
038: * Revision 1.6 2005/10/03 10:21:14 colinmacleod
039: * Fixed some style and javadoc issues.
040: *
041: * Revision 1.5 2005/10/02 14:08:56 colinmacleod
042: * Added/improved log4j logging.
043: *
044: * Revision 1.4 2005/09/15 10:23:24 colinmacleod
045: * Upgraded Maven to 1.1 (beta-2).
046: * Upgraded Hibernate to 3.0.5.
047: *
048: * Revision 1.3 2005/09/14 15:20:29 colinmacleod
049: * Removed unused local and class variables.
050: * Added serialVersionUID.
051: *
052: * Revision 1.2 2005/04/09 17:19:37 colinmacleod
053: * Changed copyright text to GPL v2 explicitly.
054: *
055: * Revision 1.1 2005/03/10 19:23:04 colinmacleod
056: * Moved to ivata groupware.
057: *
058: * Revision 1.3 2004/11/12 15:57:11 colinmacleod
059: * Removed dependencies on SSLEXT.
060: * Moved Persistence classes to ivata masks.
061: *
062: * Revision 1.2 2004/08/01 11:54:50 colinmacleod
063: * Improved transaction handling.
064: *
065: * Revision 1.1 2004/07/13 19:42:44 colinmacleod
066: * Moved project to POJOs from EJBs.
067: * Applied PicoContainer to services layer (replacing session EJBs).
068: * Applied Hibernate to persistence layer (replacing entity EJBs).
069: * -----------------------------------------------------------------------------
070: */
071: package com.ivata.groupware.container.persistence.hibernate;
072:
073: import java.sql.Connection;
074:
075: import org.hibernate.HibernateException;
076: import org.hibernate.Session;
077: import org.hibernate.Transaction;
078:
079: import org.apache.log4j.Logger;
080:
081: import com.ivata.mask.persistence.PersistenceException;
082: import com.ivata.mask.persistence.PersistenceSession;
083:
084: /**
085: * <p>
086: * Implementation of {@link com.ivata.mask.persistence.PersistenceSession} for
087: * Hibernate.
088: * </p>
089: *
090: * <copyDoc>Refer to {@link com.ivata.mask.persistence.PersistenceSession}.
091: * </copyDoc>
092: *
093: * @author Colin MacLeod
094: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
095: * @since ivata groupware 0.10 (Mar 27, 2004)
096: * @version $Revision: 1.8 $
097: */
098: public class HibernateSession implements PersistenceSession {
099: /**
100: * Serialization version (for <code>Serializable</code> interface).
101: */
102: private static final long serialVersionUID = 1L;
103: /**
104: * Logger for this class.
105: */
106: private static final Logger logger = Logger
107: .getLogger(HibernateSession.class);
108: /**
109: * If <code>true</code> the transaction will be rolled back.
110: */
111: private boolean cancel = false;
112: /**
113: * <p>
114: * The Hibernate session we're adapting.
115: * </p>
116: */
117: private Session session;
118:
119: /**
120: * <copyDoc>Refer to {@link #getSystemSession}.</copyDoc>
121: */
122: private Object systemSession;
123:
124: /**
125: * Hibernate transaction being wrapped.
126: */
127: private Transaction transaction;
128:
129: /**
130: * Creates a new instance wrapping the given Hibernate session, and
131: * transaction.
132: *
133: * @param sessionParam The actual Hibernate session which this adaptor
134: * class is wrapping.
135: * @param transactionParam Current Hibernate transaction.
136: * @param systemSessionParam
137: * <copyDoc>Refer to {@link #getSystemSession}.</copyDoc>
138: */
139: public HibernateSession(final Session sessionParam,
140: final Transaction transactionParam,
141: final Object systemSessionParam) {
142: this .session = sessionParam;
143: this .transaction = transactionParam;
144: this .systemSession = systemSessionParam;
145: }
146:
147: /**
148: * {@inheritDoc}
149: *
150: * @throws PersistenceException {@inheritDoc}
151: */
152: public void cancel() throws PersistenceException {
153: cancel = true;
154: }
155:
156: /**
157: * {@inheritDoc}
158: *
159: * @throws PersistenceException {@inheritDoc}
160: */
161: public void close() throws PersistenceException {
162: HibernateException hibernateException = null;
163: if (cancel) {
164: try {
165: transaction.rollback();
166: } catch (HibernateException e) {
167: logger.error("(" + e.getClass().getName()
168: + ") ROLLING BACK TRANSACTION: "
169: + e.getMessage(), e);
170: } finally {
171: try {
172: session.close();
173: } catch (HibernateException e) {
174: if (hibernateException != null) {
175: hibernateException = e;
176: }
177: }
178: }
179:
180: } else {
181: try {
182: if (!transaction.wasRolledBack()) {
183: transaction.commit();
184: }
185: } catch (HibernateException e) {
186: hibernateException = e;
187: try {
188: transaction.rollback();
189: } catch (Exception eRollback) {
190: logger.error("(" + e.getClass().getName()
191: + ") ROLLING BACK TRANSACTION: "
192: + e.getMessage(), e);
193: }
194: } finally {
195: try {
196: session.close();
197: } catch (HibernateException e) {
198: if (hibernateException != null) {
199: hibernateException = e;
200: }
201: }
202: }
203: }
204: if (hibernateException != null) {
205: throw new PersistenceException(
206: "Error closing hibernate persistence session: ",
207: hibernateException);
208: }
209: }
210:
211: /**
212: * {@inheritDoc}
213: *
214: * @return {@inheritDoc}
215: * @throws PersistenceException {@inheritDoc}
216: */
217: public final Connection getConnection() throws PersistenceException {
218: try {
219: return session.connection();
220: } catch (HibernateException e) {
221: throw new PersistenceException(e);
222: }
223: }
224:
225: /**
226: * Get the Hibernate session this session adapts.
227: *
228: * @return valid hibernate session.
229: */
230: Session getSession() {
231: return session;
232: }
233:
234: /**
235: * The system session is a system-specific security session - used for
236: * validation. In <em>ivata groupware</em>, you should use
237: * <code>SecuritySession</code> instances.
238: *
239: * @return Returns the system session.
240: */
241: public Object getSystemSession() {
242: return systemSession;
243: }
244:
245: /**
246: * Get the current Hibernate transaction.
247: *
248: * @return The current Hibernate transaction.
249: */
250: public Transaction getTransaction() {
251: return transaction;
252: }
253: }
|