01: package org.concern.controller;
02:
03: import org.hibernate.Session;
04: import org.hibernate.HibernateException;
05: import org.hibernate.SessionFactory;
06:
07: import javax.transaction.TransactionManager;
08: import javax.transaction.Transaction;
09: import javax.transaction.Synchronization;
10: import java.util.HashMap;
11: import java.util.Map;
12:
13: public class DefaultSessions implements Sessions {
14: private TransactionManager transactionManager;
15: private SessionFactory sessionFactory;
16: Map map = new HashMap();
17:
18: public SessionFactory getSessionFactory() {
19: return sessionFactory;
20: }
21:
22: public void setSessionFactory(SessionFactory sessionFactory) {
23: this .sessionFactory = sessionFactory;
24: }
25:
26: public void setTransactionManager(
27: TransactionManager transactionManager) {
28: this .transactionManager = transactionManager;
29: }
30:
31: public Session getSession() throws HibernateException {
32: try {
33: final Transaction transaction = transactionManager
34: .getTransaction();
35: Session session = (Session) map.get(transaction);
36: if (session == null) {
37: System.out.println("OPEN OPEN OPEN OPEN OPEN "
38: + new Throwable().getStackTrace()[1]);
39: session = sessionFactory.openSession();
40: final Session session1 = session;
41: transaction
42: .registerSynchronization(new Synchronization() {
43: public void beforeCompletion() {
44: session1.flush();
45: System.out
46: .println("FLUSH FLUSH FLUSH FLUSH FLUSH");
47: }
48:
49: public void afterCompletion(int i) {
50: session1.close();
51: System.out
52: .println("CLOSE CLOSE CLOSE CLOSE CLOSE");
53: map.remove(transaction);
54: }
55: });
56: map.put(transaction, session);
57: }
58: return session;
59: } catch (Exception e) {
60: e.printStackTrace();
61: throw new HibernateException(e);
62: }
63: }
64: }
|