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