1 package com.melloware.jukes.db;
2
3 import java.sql.SQLException;
4 import java.sql.Statement;
5 import java.util.Enumeration;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.Properties;
9
10 import org.apache.commons.lang.StringUtils;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.hibernate.HibernateException;
14 import org.hibernate.Interceptor;
15 import org.hibernate.Session;
16 import org.hibernate.SessionFactory;
17 import org.hibernate.Transaction;
18 import org.hibernate.cfg.Configuration;
19 import org.hibernate.cfg.Environment;
20 import org.hibernate.stat.Statistics;
21
22 import com.melloware.jukes.exception.InfrastructureException;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 @SuppressWarnings("unchecked")
62 public final class HibernateUtil {
63
64 private static final Log LOG = LogFactory.getLog(HibernateUtil.class);
65
66 private static final String HIBERNATE = "hibernate";
67 private static final String INTERCEPTOR_CLASS = "hibernate.util.interceptor_class";
68 private static final Map threadSession = new HashMap();
69 private static final Map threadTransaction = new HashMap();
70 private static final Map threadInterceptor = new HashMap();
71 private static Configuration configuration;
72 private static SessionFactory sessionFactory;
73 private static String remoteUrl = null;
74 private static boolean isHSQLDialect = false;
75
76
77
78
79
80 private static boolean compact = false;
81
82
83
84
85 private HibernateUtil() {
86 super();
87 }
88
89
90
91
92
93 public static Configuration getConfiguration() {
94 return configuration;
95 }
96
97
98
99
100
101
102 public static String getRemoteUrl() {
103 return remoteUrl;
104 }
105
106
107
108
109
110
111 @SuppressWarnings("unchecked")
112 public static Session getSession() throws InfrastructureException {
113 Session s = (Session) threadSession.get(HIBERNATE);
114 try {
115 if (s == null) {
116 LOG.debug("Opening new Session for this thread." + Thread.currentThread().getName());
117 if (getInterceptor() == null) {
118 s = getSessionFactory().openSession();
119 } else {
120 LOG.debug("Using interceptor: " + getInterceptor().getClass());
121 s = getSessionFactory().openSession(getInterceptor());
122
123 }
124 threadSession.put(HIBERNATE, s);
125 }
126 } catch (HibernateException ex) {
127 throw new InfrastructureException(ex);
128 }
129 return s;
130 }
131
132
133
134
135
136 public static SessionFactory getSessionFactory() {
137
138
139
140
141
142
143
144
145 return sessionFactory;
146 }
147
148
149
150
151
152
153 public static void setCompact(final boolean aCompact) {
154 compact = aCompact;
155 }
156
157
158
159
160
161
162 public static void setRemoteUrl(final String aRemoteUrl) {
163 remoteUrl = aRemoteUrl;
164 }
165
166
167
168
169
170
171 public static boolean isCompact() {
172 return compact;
173 }
174
175
176
177
178
179
180 public static boolean isHSQLDialect() {
181 return isHSQLDialect;
182 }
183
184
185
186
187 @SuppressWarnings("unchecked")
188 public static void beginTransaction() throws InfrastructureException {
189 Transaction tx = (Transaction) threadTransaction.get(HIBERNATE);
190 try {
191 if (tx == null) {
192 LOG.debug("Starting new database transaction in this thread.");
193 tx = getSession().beginTransaction();
194 threadTransaction.put(HIBERNATE, tx);
195 }
196 } catch (HibernateException ex) {
197 throw new InfrastructureException(ex);
198 }
199 }
200
201
202
203
204 @SuppressWarnings("unchecked")
205 public static void closeSession() throws InfrastructureException {
206 try {
207 final Session s = (Session) threadSession.get(HIBERNATE);
208 threadSession.put(HIBERNATE, null);
209 if ((s != null) && s.isOpen()) {
210 LOG.debug("Closing Session of this thread.");
211 s.close();
212 }
213 } catch (HibernateException ex) {
214 throw new InfrastructureException(ex);
215 }
216 }
217
218
219
220
221 @SuppressWarnings("unchecked")
222 public static void commitTransaction() throws InfrastructureException {
223 final Transaction tx = (Transaction) threadTransaction.get(HIBERNATE);
224 try {
225 if ((tx != null) && !tx.wasCommitted() && !tx.wasRolledBack()) {
226 LOG.debug("Committing database transaction of this thread.");
227 tx.commit();
228 }
229 threadTransaction.put(HIBERNATE, null);
230 } catch (HibernateException ex) {
231 rollbackTransaction();
232 throw new InfrastructureException(ex);
233 }
234 }
235
236
237
238
239
240 public static Session disconnectSession() throws InfrastructureException {
241
242 final Session session = getSession();
243 try {
244 threadSession.put(HIBERNATE, null);
245 if (session.isConnected() && session.isOpen()) {
246 session.disconnect();
247 }
248 } catch (HibernateException ex) {
249 throw new InfrastructureException(ex);
250 }
251 return session;
252 }
253
254
255
256
257 public static void initialize() {
258 if (configuration != null) {
259 return;
260 }
261
262
263 try {
264 LOG.info("Hibernate Initialization");
265
266
267
268
269
270
271
272
273
274
275
276
277 configuration = new Configuration();
278
279 configuration.configure();
280 final Properties cfg = configuration.getProperties();
281
282
283 if (remoteUrl == null) {
284 configuration.getProperties().put("hibernate.hbm2ddl.auto", "update");
285 configuration.setProperty("hibernate.hbm2ddl.auto", "update");
286 } else {
287 LOG.info("Remote URL Connection: " + remoteUrl);
288 configuration.setProperty("hibernate.connection.url", remoteUrl);
289 configuration.getProperties().remove("hbm2ddl.auto");
290 configuration.getProperties().remove("hibernate.hbm2ddl.auto");
291 }
292
293 final Enumeration names = cfg.propertyNames();
294
295 while (names.hasMoreElements()) {
296 final String name = (String) names.nextElement();
297 final String value = cfg.getProperty(name);
298 System.getProperties().put(name, value);
299 }
300 System.getProperties().put("hibernate.version", Environment.VERSION);
301
302
303 final String interceptorName = configuration.getProperty(INTERCEPTOR_CLASS);
304 if (interceptorName != null) {
305 LOG.info("Initializing Interceptor: " + interceptorName);
306 final Class interceptorClass = HibernateUtil.class.getClassLoader().loadClass(interceptorName);
307 final Interceptor interceptor = (Interceptor) interceptorClass.newInstance();
308 configuration.setInterceptor(interceptor);
309 }
310
311 if (configuration.getProperty(Environment.SESSION_FACTORY_NAME) == null) {
312
313
314 sessionFactory = configuration.buildSessionFactory();
315 } else {
316
317 configuration.buildSessionFactory();
318 }
319
320
321 final Statistics stats = sessionFactory.getStatistics();
322 stats.setStatisticsEnabled(true);
323 LOG.info("Hibernate " + Environment.VERSION);
324
325
326
327 final String dialectName = configuration.buildSettings().getDialect().toString();
328 isHSQLDialect = "org.hibernate.dialect.HSQLDialect".equals(dialectName);
329 } catch (Throwable ex) {
330
331
332 LOG.error("Building SessionFactory failed.", ex);
333 throw new ExceptionInInitializerError(ex);
334 }
335 }
336
337
338
339
340 public static void rebuildSessionFactory() throws InfrastructureException {
341 synchronized (sessionFactory) {
342 LOG.info("Rebuilding session factory.");
343 try {
344 sessionFactory = getConfiguration().buildSessionFactory();
345 } catch (Exception ex) {
346 throw new InfrastructureException(ex);
347 }
348 }
349 }
350
351
352
353
354
355 public static void rebuildSessionFactory(final Configuration cfg) throws InfrastructureException {
356 synchronized (sessionFactory) {
357 try {
358 sessionFactory = cfg.buildSessionFactory();
359 configuration = cfg;
360 } catch (Exception ex) {
361 throw new InfrastructureException(ex);
362 }
363 }
364 }
365
366
367
368
369
370
371
372
373 public static void registerInterceptor(final Interceptor interceptor) {
374 threadInterceptor.put(HIBERNATE, interceptor);
375 }
376
377
378
379
380 public static void rollbackTransaction() throws InfrastructureException {
381 final Transaction tx = (Transaction) threadTransaction.get(HIBERNATE);
382 try {
383 threadTransaction.put(HIBERNATE, null);
384 if ((tx != null) && !tx.wasCommitted() && !tx.wasRolledBack()) {
385 LOG.debug("Trying to rollback database transaction of this thread.");
386 tx.rollback();
387 }
388 } catch (HibernateException ex) {
389 throw new InfrastructureException(ex);
390 } finally {
391 closeSession();
392 }
393 }
394
395
396
397
398
399
400
401 @SuppressWarnings("deprecation")
402 public static void shutdown() {
403 LOG.info("Shutting down Hibernate.");
404
405
406 if (sessionFactory == null) {
407 return;
408 }
409
410
411 if ((isHSQLDialect) && (StringUtils.isBlank(getRemoteUrl()))) {
412 Statement stmt = null;
413 try {
414 String command = "SHUTDOWN";
415 if (isCompact()) {
416 command = "SHUTDOWN COMPACT";
417 }
418 final Session session = getSession();
419 LOG.info("HSQLDB: " + command);
420 stmt = session.connection().createStatement();
421 stmt.execute(command);
422 } catch (Throwable ex) {
423 LOG.error("Could not compact database", ex);
424 } finally {
425 if (stmt != null) {
426 try {
427 stmt.close();
428 } catch (SQLException ex) {
429 LOG.error("SQLException", ex);
430 throw new InfrastructureException(ex);
431 }
432 }
433 }
434 }
435
436
437 threadInterceptor.put(HIBERNATE, null);
438 rollbackTransaction();
439
440
441 closeSession();
442
443
444 getSessionFactory().close();
445
446
447 configuration = null;
448 sessionFactory = null;
449 }
450
451
452
453
454
455
456 private static Interceptor getInterceptor() {
457 return (Interceptor) threadInterceptor.get(HIBERNATE);
458 }
459
460 }