Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/backend/SessionHolder.java @ 8566:fe7e9da6312a
(issue1797) Move close into session release and guard it.
To avoid leaking open and Held sessions we centrally close
the session in the SessionHolder on release.
This also removes some duplicated code in SedDBSessionHolder
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Tue, 24 Feb 2015 17:16:14 +0100 |
parents | 4c3ccf2b0304 |
children | 9bbce0edd36b |
comparison
equal
deleted
inserted
replaced
8565:ea17665c5aad | 8566:fe7e9da6312a |
---|---|
25 protected Session initialValue() { | 25 protected Session initialValue() { |
26 return create(); | 26 return create(); |
27 } | 27 } |
28 }; | 28 }; |
29 | 29 |
30 private SessionHolder() { | 30 protected SessionHolder() { |
31 } | 31 } |
32 | 32 |
33 public synchronized static Session create() { | 33 public synchronized static Session create() { |
34 log.debug("create"); | 34 log.debug("create"); |
35 SessionFactory sessionFactory = | 35 SessionFactory sessionFactory = |
44 return session; | 44 return session; |
45 } | 45 } |
46 | 46 |
47 public static void release() { | 47 public static void release() { |
48 log.debug("release"); | 48 log.debug("release"); |
49 Session session = HOLDER.get(); | |
50 if (session != null) { | |
51 try { | |
52 if (!session.isOpen()) { | |
53 /* If this check really works in all cases why does hibernate | |
54 * does not use it when you call close anyway. */ | |
55 log.debug("Session is not open. Calling close anyway."); | |
56 } | |
57 session.close(); | |
58 } catch (Exception e) { | |
59 /* Do not try this at home. But hibernate may fail with an NPE | |
60 * or any other exception depending on the state of the session. | |
61 * According to doc this should only throw a HibernateException but | |
62 * we definetely get NPE's here. | |
63 */ | |
64 log.error("Exception caught on session close! Session already closed?" + e.getMessage()); | |
65 } | |
66 } | |
49 HOLDER.remove(); | 67 HOLDER.remove(); |
50 } | 68 } |
51 } | 69 } |
52 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 70 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |