001 /*****************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the license.html file. *
007 * *
008 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
009 *****************************************************************************/
010 package org.nanocontainer.persistence.hibernate.classic;
011
012 import java.io.Serializable;
013 import java.sql.Connection;
014 import java.util.Collection;
015 import java.util.Iterator;
016 import java.util.List;
017
018 import org.nanocontainer.persistence.hibernate.classic.HibernateExceptionHandler;
019
020 import net.sf.hibernate.Criteria;
021 import net.sf.hibernate.FlushMode;
022 import net.sf.hibernate.HibernateException;
023 import net.sf.hibernate.LockMode;
024 import net.sf.hibernate.Query;
025 import net.sf.hibernate.ReplicationMode;
026 import net.sf.hibernate.Session;
027 import net.sf.hibernate.SessionFactory;
028 import net.sf.hibernate.Transaction;
029 import net.sf.hibernate.type.Type;
030
031 /**
032 * Abstract base class for session delegators, which delegates all calls to
033 * session obtained by implementing class.
034 * Also does error handling. All methods are just delegations to a Hibernate session.
035 *
036 * @author Konstantin Pribluda
037 * @author Jose Peleteiro
038 * @version $Version: $
039 */
040 public abstract class SessionDelegator implements Session {
041
042 private HibernateExceptionHandler exceptionHandler;
043
044 public SessionDelegator() {
045 exceptionHandler = null;
046 }
047
048 /**
049 * @param exceptionHandler Exception handler component to use with created session
050 */
051 public SessionDelegator(HibernateExceptionHandler exceptionHandler) {
052 this.exceptionHandler = exceptionHandler;
053 }
054
055 /**
056 * obtain hibernate session.
057 */
058 protected abstract Session getDelegatedSession();
059
060 /**
061 * perform actions to dispose "burned" session properly
062 */
063 protected abstract void invalidateDelegatedSession() throws HibernateException;
064
065 /**
066 * Invalidates the session calling {@link #invalidateDelegatedSession()} and convert the <code>cause</code> using
067 * a {@link ExceptionHandler} if it's available otherwise throws the <code>cause</code> back.
068 */
069 protected RuntimeException handleException(HibernateException cause) throws HibernateException {
070 try {
071 invalidateDelegatedSession();
072 } catch (HibernateException e) {
073 // Do nothing, the only original cause should be reported.
074 }
075
076 if (exceptionHandler == null) {
077 throw cause;
078 }
079
080 return exceptionHandler.handle(cause);
081 }
082
083 /**
084 * Invalidates the session calling {@link #invalidateDelegatedSession()} and convert the <code>cause</code> using
085 * a {@link ExceptionHandler} if it's available otherwise just return the <code>cause</code> back.
086 */
087 protected RuntimeException handleException(RuntimeException cause) {
088 try {
089 invalidateDelegatedSession();
090 } catch (HibernateException e) {
091 // Do nothing, the only original cause should be reported.
092 }
093
094 if (exceptionHandler == null) {
095 return cause;
096 }
097
098 return exceptionHandler.handle(cause);
099 }
100
101 public void flush() throws HibernateException {
102 try {
103 getDelegatedSession().flush();
104 } catch (HibernateException ex) {
105 throw handleException(ex);
106 } catch (RuntimeException ex) {
107 throw handleException(ex);
108 }
109 }
110
111 public void setFlushMode(FlushMode flushMode) {
112 try {
113 getDelegatedSession().setFlushMode(flushMode);
114 } catch (RuntimeException ex) {
115 throw handleException(ex);
116 }
117 }
118
119 public FlushMode getFlushMode() {
120 try {
121 return getDelegatedSession().getFlushMode();
122 } catch (RuntimeException ex) {
123 throw handleException(ex);
124 }
125 }
126
127 public SessionFactory getSessionFactory() {
128 try {
129 return getDelegatedSession().getSessionFactory();
130 } catch (RuntimeException ex) {
131 throw handleException(ex);
132 }
133 }
134
135 public Connection connection() throws HibernateException {
136 try {
137 return getDelegatedSession().connection();
138 } catch (HibernateException ex) {
139 throw handleException(ex);
140 } catch (RuntimeException ex) {
141 throw handleException(ex);
142 }
143 }
144
145 public Connection disconnect() throws HibernateException {
146 try {
147 return getDelegatedSession().disconnect();
148 } catch (HibernateException ex) {
149 throw handleException(ex);
150 } catch (RuntimeException ex) {
151 throw handleException(ex);
152 }
153 }
154
155 public void reconnect() throws HibernateException {
156 try {
157 getDelegatedSession().reconnect();
158 } catch (HibernateException ex) {
159 throw handleException(ex);
160 } catch (RuntimeException ex) {
161 throw handleException(ex);
162 }
163 }
164
165 public void reconnect(Connection connection) throws HibernateException {
166 try {
167 getDelegatedSession().reconnect(connection);
168 } catch (HibernateException ex) {
169 throw handleException(ex);
170 } catch (RuntimeException ex) {
171 throw handleException(ex);
172 }
173 }
174
175 public Connection close() throws HibernateException {
176 try {
177 return getDelegatedSession().close();
178 } catch (HibernateException ex) {
179 throw handleException(ex);
180 } catch (RuntimeException ex) {
181 throw handleException(ex);
182 }
183 }
184
185 public void cancelQuery() throws HibernateException {
186 try {
187 getDelegatedSession().cancelQuery();
188 } catch (HibernateException ex) {
189 throw handleException(ex);
190 } catch (RuntimeException ex) {
191 throw handleException(ex);
192 }
193 }
194
195 public boolean isOpen() {
196 try {
197 return getDelegatedSession().isOpen();
198 } catch (RuntimeException ex) {
199 throw handleException(ex);
200 }
201 }
202
203 public boolean isConnected() {
204 try {
205 return getDelegatedSession().isConnected();
206 } catch (RuntimeException ex) {
207 throw handleException(ex);
208 }
209 }
210
211 public boolean isDirty() throws HibernateException {
212 try {
213 return getDelegatedSession().isDirty();
214 } catch (HibernateException ex) {
215 throw handleException(ex);
216 } catch (RuntimeException ex) {
217 throw handleException(ex);
218 }
219 }
220
221 public Serializable getIdentifier(Object object) throws HibernateException {
222 try {
223 return getDelegatedSession().getIdentifier(object);
224 } catch (HibernateException ex) {
225 throw handleException(ex);
226 } catch (RuntimeException ex) {
227 throw handleException(ex);
228 }
229 }
230
231 public boolean contains(Object object) {
232 try {
233 return getDelegatedSession().contains(object);
234 } catch (RuntimeException ex) {
235 throw handleException(ex);
236 }
237 }
238
239 public void evict(Object object) throws HibernateException {
240 try {
241 getDelegatedSession().evict(object);
242 } catch (HibernateException ex) {
243 throw handleException(ex);
244 } catch (RuntimeException ex) {
245 throw handleException(ex);
246 }
247 }
248
249 public Object load(Class theClass, Serializable id, LockMode lockMode) throws HibernateException {
250 try {
251 return getDelegatedSession().load(theClass, id, lockMode);
252 } catch (HibernateException ex) {
253 throw handleException(ex);
254 } catch (RuntimeException ex) {
255 throw handleException(ex);
256 }
257 }
258
259 public Object load(Class theClass, Serializable id) throws HibernateException {
260 try {
261 return getDelegatedSession().load(theClass, id);
262 } catch (HibernateException ex) {
263 throw handleException(ex);
264 } catch (RuntimeException ex) {
265 throw handleException(ex);
266 }
267 }
268
269 public void load(Object object, Serializable id) throws HibernateException {
270 try {
271 getDelegatedSession().load(object, id);
272 } catch (HibernateException ex) {
273 throw handleException(ex);
274 } catch (RuntimeException ex) {
275 throw handleException(ex);
276 }
277 }
278
279 public void replicate(Object object, ReplicationMode replicationMode) throws HibernateException {
280 try {
281 getDelegatedSession().replicate(object, replicationMode);
282 } catch (HibernateException ex) {
283 throw handleException(ex);
284 } catch (RuntimeException ex) {
285 throw handleException(ex);
286 }
287 }
288
289 public Serializable save(Object object) throws HibernateException {
290 try {
291
292 return getDelegatedSession().save(object);
293 } catch (HibernateException ex) {
294 throw handleException(ex);
295 } catch (RuntimeException ex) {
296 throw handleException(ex);
297 }
298 }
299
300 public void save(Object object, Serializable id) throws HibernateException {
301 try {
302 getDelegatedSession().save(object, id);
303 } catch (HibernateException ex) {
304 throw handleException(ex);
305 } catch (RuntimeException ex) {
306 throw handleException(ex);
307 }
308 }
309
310 public void saveOrUpdate(Object object) throws HibernateException {
311 try {
312 getDelegatedSession().saveOrUpdate(object);
313 } catch (HibernateException ex) {
314 throw handleException(ex);
315 } catch (RuntimeException ex) {
316 throw handleException(ex);
317 }
318 }
319
320 public void update(Object object) throws HibernateException {
321 try {
322 getDelegatedSession().update(object);
323 } catch (HibernateException ex) {
324 throw handleException(ex);
325 } catch (RuntimeException ex) {
326 throw handleException(ex);
327 }
328 }
329
330 public void update(Object object, Serializable id) throws HibernateException {
331 try {
332 getDelegatedSession().update(object, id);
333 } catch (HibernateException ex) {
334 throw handleException(ex);
335 } catch (RuntimeException ex) {
336 throw handleException(ex);
337 }
338 }
339
340 public Object saveOrUpdateCopy(Object object) throws HibernateException {
341 try {
342 return getDelegatedSession().saveOrUpdateCopy(object);
343 } catch (HibernateException ex) {
344 throw handleException(ex);
345 } catch (RuntimeException ex) {
346 throw handleException(ex);
347 }
348 }
349
350 public Object saveOrUpdateCopy(Object object, Serializable id) throws HibernateException {
351 try {
352 return getDelegatedSession().saveOrUpdateCopy(object, id);
353 } catch (HibernateException ex) {
354 throw handleException(ex);
355 } catch (RuntimeException ex) {
356 throw handleException(ex);
357 }
358 }
359
360 public void delete(Object object) throws HibernateException {
361 try {
362 getDelegatedSession().delete(object);
363 } catch (HibernateException ex) {
364 throw handleException(ex);
365 } catch (RuntimeException ex) {
366 throw handleException(ex);
367 }
368 }
369
370 public List find(String query) throws HibernateException {
371 try {
372 return getDelegatedSession().find(query);
373 } catch (HibernateException ex) {
374 throw handleException(ex);
375 } catch (RuntimeException ex) {
376 throw handleException(ex);
377 }
378 }
379
380 public List find(String query, Object value, Type type) throws HibernateException {
381 try {
382 return getDelegatedSession().find(query, value, type);
383 } catch (HibernateException ex) {
384 throw handleException(ex);
385 } catch (RuntimeException ex) {
386 throw handleException(ex);
387 }
388 }
389
390 public List find(String query, Object[] values, Type[] types) throws HibernateException {
391 try {
392 return getDelegatedSession().find(query, values, types);
393 } catch (HibernateException ex) {
394 throw handleException(ex);
395 } catch (RuntimeException ex) {
396 throw handleException(ex);
397 }
398 }
399
400 public Iterator iterate(String query) throws HibernateException {
401 try {
402 return getDelegatedSession().iterate(query);
403 } catch (HibernateException ex) {
404 throw handleException(ex);
405 } catch (RuntimeException ex) {
406 throw handleException(ex);
407 }
408 }
409
410 public Iterator iterate(String query, Object value, Type type) throws HibernateException {
411 try {
412 return getDelegatedSession().iterate(query, value, type);
413 } catch (HibernateException ex) {
414 throw handleException(ex);
415 } catch (RuntimeException ex) {
416 throw handleException(ex);
417 }
418 }
419
420 public Iterator iterate(String query, Object[] values, Type[] types) throws HibernateException {
421 try {
422 return getDelegatedSession().iterate(query, values, types);
423 } catch (HibernateException ex) {
424 throw handleException(ex);
425 } catch (RuntimeException ex) {
426 throw handleException(ex);
427 }
428 }
429
430 public Collection filter(Object collection, String filter) throws HibernateException {
431 try {
432 return getDelegatedSession().filter(collection, filter);
433 } catch (HibernateException ex) {
434 throw handleException(ex);
435 } catch (RuntimeException ex) {
436 throw handleException(ex);
437 }
438 }
439
440 public Collection filter(Object collection, String filter, Object value, Type type) throws HibernateException {
441 try {
442 return getDelegatedSession().filter(collection, filter, value, type);
443 } catch (HibernateException ex) {
444 throw handleException(ex);
445 } catch (RuntimeException ex) {
446 throw handleException(ex);
447 }
448 }
449
450 public Collection filter(Object collection, String filter, Object[] values, Type[] types) throws HibernateException {
451 try {
452 return getDelegatedSession().filter(collection, filter, values, types);
453 } catch (HibernateException ex) {
454 throw handleException(ex);
455 } catch (RuntimeException ex) {
456 throw handleException(ex);
457 }
458 }
459
460 public int delete(String query) throws HibernateException {
461 try {
462 return getDelegatedSession().delete(query);
463 } catch (HibernateException ex) {
464 throw handleException(ex);
465 } catch (RuntimeException ex) {
466 throw handleException(ex);
467 }
468 }
469
470 public int delete(String query, Object value, Type type) throws HibernateException {
471 try {
472 return getDelegatedSession().delete(query, value, type);
473 } catch (HibernateException ex) {
474 throw handleException(ex);
475 } catch (RuntimeException ex) {
476 throw handleException(ex);
477 }
478
479 }
480
481 public int delete(String query, Object[] values, Type[] types) throws HibernateException {
482 try {
483 return getDelegatedSession().delete(query, values, types);
484 } catch (HibernateException ex) {
485 throw handleException(ex);
486 } catch (RuntimeException ex) {
487 throw handleException(ex);
488 }
489 }
490
491 public void lock(Object object, LockMode lockMode) throws HibernateException {
492 try {
493 getDelegatedSession().lock(object, lockMode);
494 } catch (HibernateException ex) {
495 throw handleException(ex);
496 } catch (RuntimeException ex) {
497 throw handleException(ex);
498 }
499 }
500
501 public void refresh(Object object) throws HibernateException {
502 try {
503 getDelegatedSession().refresh(object);
504 } catch (HibernateException ex) {
505 throw handleException(ex);
506 } catch (RuntimeException ex) {
507 throw handleException(ex);
508 }
509 }
510
511 public void refresh(Object object, LockMode lockMode) throws HibernateException {
512 try {
513 getDelegatedSession().refresh(object, lockMode);
514 } catch (HibernateException ex) {
515 throw handleException(ex);
516 } catch (RuntimeException ex) {
517 throw handleException(ex);
518 }
519 }
520
521 public LockMode getCurrentLockMode(Object object) throws HibernateException {
522 try {
523 return getDelegatedSession().getCurrentLockMode(object);
524 } catch (HibernateException ex) {
525 throw handleException(ex);
526 } catch (RuntimeException ex) {
527 throw handleException(ex);
528 }
529 }
530
531 public Transaction beginTransaction() throws HibernateException {
532 try {
533 return getDelegatedSession().beginTransaction();
534 } catch (HibernateException ex) {
535 throw handleException(ex);
536 } catch (RuntimeException ex) {
537 throw handleException(ex);
538 }
539 }
540
541 public Criteria createCriteria(Class persistentClass) {
542 try {
543 return getDelegatedSession().createCriteria(persistentClass);
544 } catch (RuntimeException ex) {
545 throw handleException(ex);
546 }
547 }
548
549 public Query createQuery(String queryString) throws HibernateException {
550 try {
551 return getDelegatedSession().createQuery(queryString);
552 } catch (HibernateException ex) {
553 throw handleException(ex);
554 } catch (RuntimeException ex) {
555 throw handleException(ex);
556 }
557 }
558
559 public Query createFilter(Object collection, String queryString) throws HibernateException {
560 try {
561 return getDelegatedSession().createFilter(collection, queryString);
562 } catch (HibernateException ex) {
563 throw handleException(ex);
564 } catch (RuntimeException ex) {
565 throw handleException(ex);
566 }
567 }
568
569 public Query getNamedQuery(String queryName) throws HibernateException {
570 try {
571 return getDelegatedSession().getNamedQuery(queryName);
572 } catch (HibernateException ex) {
573 throw handleException(ex);
574 } catch (RuntimeException ex) {
575 throw handleException(ex);
576 }
577 }
578
579 public Query createSQLQuery(String sql, String returnAlias, Class returnClass) {
580 try {
581 return getDelegatedSession().createSQLQuery(sql, returnAlias, returnClass);
582 } catch (RuntimeException ex) {
583 throw handleException(ex);
584 }
585 }
586
587 public Query createSQLQuery(String sql, String[] returnAliases, Class[] returnClasses) {
588 try {
589 return getDelegatedSession().createSQLQuery(sql, returnAliases, returnClasses);
590 } catch (RuntimeException ex) {
591 throw handleException(ex);
592 }
593 }
594
595 public void clear() {
596 try {
597 getDelegatedSession().clear();
598 } catch (RuntimeException ex) {
599 throw handleException(ex);
600 }
601 }
602
603 public Object get(Class clazz, Serializable id) throws HibernateException {
604 try {
605 return getDelegatedSession().get(clazz, id);
606 } catch (HibernateException ex) {
607 throw handleException(ex);
608 } catch (RuntimeException ex) {
609 throw handleException(ex);
610 }
611 }
612
613 public Object get(Class clazz, Serializable id, LockMode lockMode) throws HibernateException {
614 try {
615 return getDelegatedSession().get(clazz, id, lockMode);
616 } catch (HibernateException ex) {
617 throw handleException(ex);
618 } catch (RuntimeException ex) {
619 throw handleException(ex);
620 }
621 }
622 }