KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > db > JbpmSession


1 package org.jbpm.db;
2
3 import java.sql.Connection JavaDoc;
4 import java.util.LinkedList JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.hibernate.HibernateException;
9 import org.hibernate.Session;
10 import org.hibernate.Transaction;
11
12 /**
13  * represents the connection to the jbpm database.
14  * You can obtain a DbSession with
15  * <pre>
16  * DbSession dbSession = dbSessionFactory.openDbSession();
17  * </pre>
18  * or
19  * <pre>
20  * Connection jdbcConnection = ...;
21  * DbSession dbSession = dbSessionFactory.openDbSession(jdbcConnection);
22  * </pre>
23  * The actual database operations are defined in the modules :
24  * <ul>
25  * <li>{@link org.jbpm.db.GraphSession}</li>
26  * <li>{@link org.jbpm.db.TaskMgmtSession}</li>
27  * <li>{@link org.jbpm.db.LoggingSession}</li>
28  * <li>{@link org.jbpm.db.FileSession}</li>
29  * </ul>
30  * The easiest way to obtain the operations is like this :
31  * <ul>
32  * <li><pre>jbpmSession.getGraphSession().someGraphDbMethod(...)</pre></li>
33  * <li><pre>jbpmSession.getTaskMgmtSession().someTaskDbMethod(...)</pre></li>
34  * <li><pre>jbpmSession.getLoggingSession().someLoggingDbMethod(...)</pre></li>
35  * <li><pre>jbpmSession.getContextSession().someFileDbMethod(...)</pre></li>
36  * <li><pre>jbpmSession.getFileSession().someFileDbMethod(...)</pre></li>
37  * </ul>
38  */

39 public class JbpmSession {
40   
41   static ThreadLocal JavaDoc currentJbpmSessionStack = new ThreadLocal JavaDoc();
42   
43   private JbpmSessionFactory jbpmSessionFactory = null;
44   private Session session = null;
45   private Transaction transaction = null;
46   
47   private GraphSession graphSession = null;
48   private ContextSession contextSession = null;
49   private TaskMgmtSession taskMgmtSession = null;
50   private LoggingSession loggingSession = null;
51   private SchedulerSession schedulerSession = null;
52   
53   public JbpmSession( JbpmSessionFactory jbpmSessionFactory, Session session ) {
54     this.jbpmSessionFactory = jbpmSessionFactory;
55     this.session = session;
56     this.graphSession = new GraphSession(this);
57     this.contextSession = new ContextSession(this);
58     this.taskMgmtSession = new TaskMgmtSession(this);
59     this.loggingSession = new LoggingSession(this);
60     this.schedulerSession = new SchedulerSession(this);
61     
62     pushCurrentSession();
63   }
64   
65   public JbpmSessionFactory getJbpmSessionFactory() {
66     return jbpmSessionFactory;
67   }
68
69   public Connection JavaDoc getConnection() {
70     try {
71       return session.connection();
72     } catch (Exception JavaDoc e) {
73       log.error(e);
74       handleException();
75       throw new RuntimeException JavaDoc( "couldn't get the jdbc connection from hibernate", e );
76     }
77   }
78
79   public Session getSession() {
80     return session;
81   }
82
83   public Transaction getTransaction() {
84     return transaction;
85   }
86
87   public void beginTransaction() {
88     try {
89       transaction = session.beginTransaction();
90     } catch (Exception JavaDoc e) {
91       log.error(e);
92       handleException();
93       throw new RuntimeException JavaDoc( "couldn't begin a transaction", e );
94     }
95   }
96
97   public void commitTransaction() {
98     if ( transaction == null ) {
99       throw new RuntimeException JavaDoc("can't commit : no transaction started" );
100     }
101     try {
102       session.flush();
103       transaction.commit();
104     } catch (Exception JavaDoc e) {
105       log.error(e);
106       handleException();
107       throw new RuntimeException JavaDoc( "couldn't commit transaction", e );
108     } finally {
109       transaction = null;
110     }
111   }
112
113   public void rollbackTransaction() {
114     if ( transaction == null ) {
115       throw new RuntimeException JavaDoc("can't rollback : no transaction started" );
116     }
117     try {
118       transaction.rollback();
119     } catch (Exception JavaDoc e) {
120       log.error(e);
121       handleException();
122       throw new RuntimeException JavaDoc( "couldn't rollback transaction", e );
123     } finally {
124       transaction = null;
125     }
126   }
127   
128   public void commitTransactionAndClose() {
129     commitTransaction();
130     close();
131   }
132   public void rollbackTransactionAndClose() {
133     rollbackTransaction();
134     close();
135   }
136   
137   public GraphSession getGraphSession() {
138     return graphSession;
139   }
140   public ContextSession getContextSession() {
141     return contextSession;
142   }
143   public TaskMgmtSession getTaskMgmtSession() {
144     return taskMgmtSession;
145   }
146   public LoggingSession getLoggingSession() {
147     return loggingSession;
148   }
149   public SchedulerSession getSchedulerSession() {
150     return schedulerSession;
151   }
152
153   public void close() {
154     try {
155       if ( (session!=null)
156            && (session.isOpen())
157          ) {
158         session.close();
159       }
160     } catch (Exception JavaDoc e) {
161       log.error(e);
162       throw new RuntimeException JavaDoc( "couldn't close the hibernate connection", e );
163     } finally {
164       popCurrentSession();
165       session = null;
166     }
167   }
168
169   /**
170    * handles an exception that is thrown by hibernate.
171    */

172   void handleException() {
173     // if hibernate throws an exception,
174
if (transaction!=null) {
175       try {
176         // the transaction should be rolled back
177
transaction.rollback();
178       } catch (HibernateException e) {
179         log.error("couldn't rollback hibernate transaction", e);
180       }
181       // and the hibernate session should be closed.
182
close();
183     }
184   }
185
186   private void pushCurrentSession() {
187     LinkedList JavaDoc stack = (LinkedList JavaDoc) currentJbpmSessionStack.get();
188     if (stack==null) {
189       stack = new LinkedList JavaDoc();
190       currentJbpmSessionStack.set(stack);
191     }
192     stack.addFirst(this);
193   }
194   
195   public static JbpmSession getCurrentJbpmSession() {
196     JbpmSession jbpmSession = null;
197     LinkedList JavaDoc stack = (LinkedList JavaDoc) currentJbpmSessionStack.get();
198     if ( (stack!=null)
199          && (! stack.isEmpty())
200        ) {
201       jbpmSession = (JbpmSession) stack.getFirst();
202     }
203     return jbpmSession;
204   }
205
206   private void popCurrentSession() {
207     LinkedList JavaDoc stack = (LinkedList JavaDoc) currentJbpmSessionStack.get();
208     if ( (stack==null)
209          || (stack.isEmpty())
210          || (stack.getFirst()!=this)
211        ) {
212       log.warn("can't pop current session: are you calling JbpmSession.close() multiple times ?");
213     } else {
214       stack.removeFirst();
215     }
216   }
217
218   private static final Log log = LogFactory.getLog(JbpmSession.class);
219 }
220
Popular Tags