1 28 package de.nava.informa.utils.manager.hibernate; 29 30 import de.nava.informa.impl.hibernate.SessionHandler; 31 import de.nava.informa.utils.manager.PersistenceManagerException; 32 import net.sf.hibernate.HibernateException; 33 import net.sf.hibernate.LockMode; 34 import net.sf.hibernate.Session; 35 36 import java.util.logging.Level ; 37 import java.util.logging.Logger ; 38 39 45 final class HibernateUtil { 46 47 private static final Logger LOG = Logger.getLogger(HibernateUtil.class.getName()); 48 49 private static SessionHandler sessionHandler; 50 private static final ThreadLocal SESSION = new ThreadLocal (); 51 private static boolean inited = false; 52 53 private static Object lock = new Object (); 54 private static boolean sessionOpened = false; 55 56 59 private HibernateUtil() { 60 } 61 62 67 private static void init() 68 throws HibernateException { 69 70 sessionHandler = SessionHandler.getInstance(System.getProperties()); 72 inited = true; 73 } 74 75 82 public static synchronized Session openSession() throws HibernateException { 83 if (!inited) { 84 init(); 85 } 86 87 synchronized (lock) { 88 if (sessionOpened) { 89 try { 90 lock.wait(); 91 } catch (InterruptedException e) { 92 } 94 } 95 } 96 97 Session s = (Session) SESSION.get(); 98 99 if (s == null) { 101 s = sessionHandler.getSession(); 102 SESSION.set(s); 103 } 104 105 sessionOpened = true; 106 107 return s; 108 } 109 110 113 public static void closeSession() { 114 Session s = (Session) SESSION.get(); 115 116 SESSION.set(null); 117 118 if (s != null) { 119 try { 120 s.close(); 121 } catch (HibernateException e) { 122 } 125 } 126 127 synchronized (lock) { 128 sessionOpened = false; 129 lock.notify(); 130 } 131 } 132 133 140 public static void lock(Object o, Session s) { 141 try { 142 s.lock(o, LockMode.NONE); 143 } catch (HibernateException e) { 144 } 146 } 147 148 155 public static void saveObject(final Object object) 156 throws PersistenceManagerException { 157 158 saveObject(object, null); 160 } 161 162 169 public static void saveObject(final Object object, Session session) 170 throws PersistenceManagerException { 171 boolean isForeignSession = session != null; 172 173 try { 174 if (!isForeignSession) { 176 session = openSession(); 177 } 178 179 session.save(object); 180 181 if (!isForeignSession) { 183 session.flush(); 184 session.connection().commit(); 185 } 186 } catch (Exception e) { 187 if (!isForeignSession) { 189 try { 190 session.connection().rollback(); 191 } catch (Exception e1) { 192 } 194 } 195 196 LOG.log(Level.SEVERE, "Couldn't save object.", e); 197 throw new PersistenceManagerException("Couldn't save object.", e); 198 } finally { 199 if (!isForeignSession) { 201 closeSession(); 202 } 203 } 204 } 205 206 213 public static void updateObject(final Object object) 214 throws PersistenceManagerException { 215 216 updateObject(object, null); 217 } 218 219 227 public static void updateObject(final Object object, Session session) 228 throws PersistenceManagerException { 229 230 boolean isForeignSession = session != null; 231 232 try { 233 if (!isForeignSession) { 235 session = openSession(); 236 } 237 238 session.update(object); 239 240 if (!isForeignSession) { 242 session.flush(); 243 session.connection().commit(); 244 } 245 } catch (Exception e) { 246 if (!isForeignSession) { 248 try { 249 session.connection().rollback(); 250 } catch (Exception e1) { 251 } 253 } 254 255 LOG.log(Level.SEVERE, "Couldn't update object.", e); 256 throw new PersistenceManagerException("Couldn't update object.", e); 257 } finally { 258 if (!isForeignSession) { 260 closeSession(); 261 } 262 } 263 } 264 265 273 public static void deleteObject(final Object object, Session session) 274 throws PersistenceManagerException { 275 276 boolean isForeignSession = session != null; 277 278 try { 279 if (!isForeignSession) { 281 session = openSession(); 282 } 283 284 session.delete(object); 285 286 if (!isForeignSession) { 288 session.flush(); 289 session.connection().commit(); 290 } 291 } catch (Exception e) { 292 if (!isForeignSession) { 294 try { 295 session.connection().rollback(); 296 } catch (Exception e1) { 297 } 299 } 300 LOG.log(Level.SEVERE, "Couldn't delete object.", e); 301 throw new PersistenceManagerException("Couldn't delete object.", e); 302 } finally { 303 if (!isForeignSession) { 305 closeSession(); 306 } 307 } 308 } 309 } 310 | Popular Tags |