1 50 package com.ivata.groupware.container.persistence.hibernate; 51 52 import java.sql.Connection ; 53 54 import net.sf.hibernate.HibernateException; 55 import net.sf.hibernate.Session; 56 import net.sf.hibernate.Transaction; 57 58 import org.apache.log4j.Logger; 59 60 import com.ivata.mask.persistence.PersistenceException; 61 import com.ivata.mask.persistence.PersistenceSession; 62 63 73 public class HibernateSession implements PersistenceSession { 74 77 private static Logger log = Logger.getLogger(HibernateSession.class); 78 81 private boolean cancel = false; 82 87 private Session session; 88 89 private Object systemSession; 90 91 94 private Transaction transaction; 95 96 100 public HibernateSession(Session sessionParam, Transaction transactionParam, 101 Object systemSessionParam) { 102 this.session = sessionParam; 103 this.transaction = transactionParam; 104 this.systemSession = systemSessionParam; 105 } 106 107 113 public void cancel() throws PersistenceException { 114 cancel = true; 115 } 116 117 120 public void close() throws PersistenceException { 121 HibernateException hibernateException = null; 122 if (cancel) { 123 try { 124 transaction.rollback(); 125 } catch (HibernateException e) { 126 log.error("(" 127 + e.getClass().getName() 128 + ") ROLLING BACK TRANSACTION: " 129 + e.getMessage(), e); 130 } finally { 131 try { 132 session.close(); 133 } catch (HibernateException e) { 134 if (hibernateException != null) { 135 hibernateException = e; 136 } 137 } 138 } 139 140 } else { 141 try { 142 if (!transaction.wasRolledBack()) { 143 transaction.commit(); 144 } 145 } catch (HibernateException e) { 146 hibernateException = e; 147 try { 148 transaction.rollback(); 149 } catch (Exception eRollback) { 150 log.error("(" 151 + e.getClass().getName() 152 + ") ROLLING BACK TRANSACTION: " 153 + e.getMessage(), e); 154 } 155 } finally { 156 try { 157 session.close(); 158 } catch (HibernateException e) { 159 if (hibernateException != null) { 160 hibernateException = e; 161 } 162 } 163 } 164 } 165 if (hibernateException != null) { 166 throw new PersistenceException("Error closing hibernate persistence session: ", 167 hibernateException); 168 } 169 } 170 171 176 public final Connection getConnection() throws PersistenceException { 177 try { 178 return session.connection(); 179 } catch (HibernateException e) { 180 throw new PersistenceException(e); 181 } 182 } 183 184 189 Session getSession() { 190 return session; 191 } 192 193 196 public Object getSystemSession() { 197 return systemSession; 198 } 199 202 public Transaction getTransaction() { 203 return transaction; 204 } 205 } 206 | Popular Tags |