1 package org.jbpm.db; 2 3 import java.sql.Connection ; 4 import java.util.LinkedList ; 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 39 public class JbpmSession { 40 41 static ThreadLocal currentJbpmSessionStack = new ThreadLocal (); 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 getConnection() { 70 try { 71 return session.connection(); 72 } catch (Exception e) { 73 log.error(e); 74 handleException(); 75 throw new RuntimeException ( "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 e) { 91 log.error(e); 92 handleException(); 93 throw new RuntimeException ( "couldn't begin a transaction", e ); 94 } 95 } 96 97 public void commitTransaction() { 98 if ( transaction == null ) { 99 throw new RuntimeException ("can't commit : no transaction started" ); 100 } 101 try { 102 session.flush(); 103 transaction.commit(); 104 } catch (Exception e) { 105 log.error(e); 106 handleException(); 107 throw new RuntimeException ( "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 ("can't rollback : no transaction started" ); 116 } 117 try { 118 transaction.rollback(); 119 } catch (Exception e) { 120 log.error(e); 121 handleException(); 122 throw new RuntimeException ( "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 e) { 161 log.error(e); 162 throw new RuntimeException ( "couldn't close the hibernate connection", e ); 163 } finally { 164 popCurrentSession(); 165 session = null; 166 } 167 } 168 169 172 void handleException() { 173 if (transaction!=null) { 175 try { 176 transaction.rollback(); 178 } catch (HibernateException e) { 179 log.error("couldn't rollback hibernate transaction", e); 180 } 181 close(); 183 } 184 } 185 186 private void pushCurrentSession() { 187 LinkedList stack = (LinkedList ) currentJbpmSessionStack.get(); 188 if (stack==null) { 189 stack = new LinkedList (); 190 currentJbpmSessionStack.set(stack); 191 } 192 stack.addFirst(this); 193 } 194 195 public static JbpmSession getCurrentJbpmSession() { 196 JbpmSession jbpmSession = null; 197 LinkedList stack = (LinkedList ) 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 stack = (LinkedList ) 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 |