View Javadoc

1   package org.controlhaus.hibernate;
2   
3   import net.sf.hibernate.HibernateException;
4   import net.sf.hibernate.Session;
5   import net.sf.hibernate.SessionFactory;
6   import net.sf.hibernate.Transaction;
7   import net.sf.hibernate.cfg.Configuration;
8   
9   import org.apache.beehive.controls.api.bean.ControlImplementation;
10  import org.apache.beehive.controls.api.context.Context;
11  import org.apache.beehive.controls.api.context.ControlBeanContext;
12  import org.apache.beehive.controls.api.events.EventHandler;
13  import org.apache.beehive.controls.api.context.ResourceContext;
14  
15  import org.apache.log4j.Logger;
16  import org.controlhaus.hibernate.HibernateControl.ManagedTransactions;
17  
18  /***
19   * The HibernateControl Implementation.
20   * 
21   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
22   * @since May 10, 2003
23   */
24  @ControlImplementation
25  public class HibernateControlImpl
26      implements HibernateControl
27  {
28      private static Logger logger = Logger.getLogger(HibernateControlImpl.class.getName());
29      
30      private ThreadLocal<Session> session = new ThreadLocal<Session>();
31      private ThreadLocal<Transaction> transactions = new ThreadLocal<Transaction>();
32  
33      private SessionFactory sessionFactory;
34      private Configuration hibConfig;
35  
36      private boolean manageTXs = false;
37      
38      @Context ControlBeanContext context;
39      @Context ResourceContext resourceContext;
40      
41      public HibernateControlImpl()
42      {
43      }
44      
45      /***
46       * @see org.codehaus.plexus.hibernate.HibernateService#getSessionFactory()
47       */
48      public SessionFactory getSessionFactory()
49      {
50          return sessionFactory;
51      }
52  
53      public Configuration getConfiguration()
54      {
55          return hibConfig;
56      }
57  
58  
59      @EventHandler(field="context", 
60                    eventSet=ControlBeanContext.LifeCycle.class, 
61                    eventName="onCreate")
62      public void onCreate()
63      {
64          ManagedTransactions txProp = 
65              (ManagedTransactions) context.getControlPropertySet(ManagedTransactions.class);
66          if (txProp != null)
67          {
68              manageTXs = txProp.value();
69          }
70          
71          sessionFactory = HibernateFactory.getInstance().getSessionFactory(this);
72      }
73      
74      @EventHandler (field="resourceContext", 
75                     eventSet=ResourceContext.ResourceEvents.class, 
76                     eventName="onRelease")
77      public void onRelease()
78      {
79          try
80          {
81              logger.debug("Closing open hibernate session.");
82              if ( manageTXs )
83              {
84                  Transaction t = getTransaction();
85                  if ( t != null )
86                  {
87                      try
88                      {
89                          t.commit();
90                      }
91                      catch(HibernateException e)
92                      {
93                          t.rollback();
94                      }
95                      finally
96                      {
97                          closeSession();
98                      }
99                  }
100             }
101             else
102             {
103                 closeSession();
104             }
105         }
106         catch (HibernateException e)
107         {
108             logger.error("Couldn't close session!", e);
109         }
110     }
111 
112     public Session getSession() 
113         throws HibernateException
114     {
115         Session s = (Session) session.get();
116         if (s == null)
117         {
118             s = sessionFactory.openSession();
119             session.set(s);
120             
121             if ( manageTXs )
122             {
123                 transactions.set( s.beginTransaction() );
124             }
125         }
126         return s;
127     }
128 
129     public Transaction getTransaction() 
130     {
131         return (Transaction) transactions.get();
132     }
133     
134     public void closeSession() 
135         throws HibernateException
136     {
137         logger.info("Closing session for thread.");
138         
139         if ( manageTXs )
140         {
141             transactions.remove();
142         }
143 
144         Session s = (Session) session.get();
145         if ( s != null )
146         {
147             session.remove();
148             s.close();
149         }
150 
151     }
152 }