1 21 package oracle.toplink.essentials.internal.ejb.cmp3.transaction.base; 23 24 import java.lang.reflect.Proxy ; 25 import java.lang.reflect.InvocationHandler ; 26 import java.util.Vector ; 27 import java.sql.*; 28 import javax.transaction.xa.XAResource ; 29 import javax.transaction.*; 30 import oracle.toplink.essentials.exceptions.TransactionException; 31 import oracle.toplink.essentials.internal.ejb.cmp3.base.ExceptionFactory; 32 import oracle.toplink.essentials.internal.ejb.cmp3.jdbc.base.ConnectionProxyHandler; 33 import oracle.toplink.essentials.internal.ejb.cmp3.jdbc.base.DataSourceImpl; 34 35 41 public class TransactionImpl implements Transaction { 42 boolean markedForRollback; 44 45 int status; 47 48 Vector listeners; 50 51 Connection connection; 53 static Class proxyClass = Proxy.getProxyClass(Connection.class.getClassLoader(), new Class [] { Connection.class }); 54 55 DataSourceImpl dataSource; 57 58 59 60 public static final int STATUS_ACTIVE = 0; 62 public static final int STATUS_MARKED_ROLLBACK = 1; 63 public static final int STATUS_PREPARED = 2; 64 public static final int STATUS_COMMITTED = 3; 65 public static final int STATUS_ROLLEDBACK = 4; 66 public static final int STATUS_UNKNOWN = 5; 67 public static final int STATUS_NO_TRANSACTION = 6; 68 public static final int STATUS_PREPARING = 7; 69 public static final int STATUS_COMMITTING = 8; 70 public static final int STATUS_ROLLING_BACK = 9; 71 72 public static boolean DUMP_AFTER_COMPLETION_ERRORS = true; 74 75 76 77 78 private void debug(String s) { 79 System.out.println(s); 80 } 81 82 85 public TransactionImpl() { 86 markedForRollback = false; 87 status = STATUS_ACTIVE; 88 listeners = new Vector (); 89 } 90 91 95 public Connection getConnection(DataSourceImpl ds, String user, String password) throws SQLException { 96 if (connection == null) { 98 debug("TxImpl - allocating new connection"); 99 dataSource = ds; 100 connection = ds.internalGetConnection(user, password); 101 connection.setAutoCommit(false); 102 } else { 103 if (ds.getName() != dataSource.getName()) { 105 throw TransactionException.multipleResourceException(); 106 } 107 } 108 109 debug("TxImpl - creating connection proxy"); 112 Connection proxyConnection = null; 113 try { 114 InvocationHandler handler = new ConnectionProxyHandler(connection); 115 proxyConnection = (Connection)proxyClass.getConstructor(new Class [] { InvocationHandler .class }).newInstance(new Object [] { handler }); 116 } catch (Exception ex) { 117 throw TransactionException.internalProxyException(ex); 118 } 119 return proxyConnection; 120 } 121 122 130 public void invokeAfterCompletion() { 131 debug("TxImpl - invoking afterCompletion"); 133 int i; 134 int j; 135 for (i = 0, j = listeners.size(); i < j; i++) { 136 try { 137 ((Synchronization)listeners.elementAt(i)).afterCompletion(status); 138 } catch (Throwable t) { 139 if (DUMP_AFTER_COMPLETION_ERRORS) { 140 t.printStackTrace(System.out); 141 } 142 } 143 } 144 } 145 146 149 public void rollbackConnection() throws SQLException { 150 if (connection != null) { 151 debug("TxImpl - rolling back connection"); 152 status = STATUS_ROLLING_BACK; 153 connection.rollback(); 154 status = STATUS_ROLLEDBACK; 155 } 156 } 157 158 161 public void commitConnection() throws SQLException { 162 if (connection != null) { 163 debug("TxImpl - committing connection"); 164 status = STATUS_COMMITTING; 165 connection.commit(); 166 status = STATUS_COMMITTED; 167 } 168 } 169 170 173 public void cleanup() { 174 debug("TxImpl - cleanup"); 175 if (connection != null) { 176 try { 177 connection.close(); 178 } catch (Exception ex) { 179 } 180 181 connection = null; 183 } 184 status = STATUS_NO_TRANSACTION; 185 } 186 187 188 189 190 public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException , IllegalStateException , SystemException { 191 Exception error = null; 192 193 debug("TxImpl - commit"); 194 switch (status) { 196 case STATUS_ACTIVE: break; 198 case STATUS_MARKED_ROLLBACK: { 199 error = new ExceptionFactory().txMarkedForRollbackException(); 201 break; 202 } 203 default: throw new ExceptionFactory().invalidStateException(status); 205 } 206 207 if (error == null) { 209 try { 210 debug("TxImpl - invoking beforeCompletion"); 211 int i; 212 int j; 213 for (i = 0, j = listeners.size(); i < j; i++) { 214 ((Synchronization)listeners.elementAt(i)).beforeCompletion(); 215 } 216 } catch (Exception ex) { 217 error = ex; 218 status = STATUS_ROLLING_BACK; 219 debug("TxImpl - error in beforeCompletion: " + ex); 220 } 221 } 222 223 if ((error == null) && (status == STATUS_ACTIVE)) { 225 try { 226 commitConnection(); 227 } catch (Exception ex) { 228 error = ex; 229 } 230 } else { 231 try { 232 rollbackConnection(); 233 } catch (Exception ex) { 234 error = ex; 235 } 236 } 237 238 invokeAfterCompletion(); 240 cleanup(); 241 242 if (error != null) { 244 throw new ExceptionFactory().newSystemException(error); 245 } 246 } 247 248 public int getStatus() throws SystemException { 249 return status; 250 } 251 252 public void registerSynchronization(Synchronization synchronization) throws RollbackException, IllegalStateException , SystemException { 253 debug("TxImpl - registering sync listener: " + synchronization); 254 listeners.add(synchronization); 255 } 256 257 public void rollback() throws IllegalStateException , SystemException { 258 Exception error = null; 259 260 debug("TxImpl - rollback"); 261 try { 262 rollbackConnection(); 263 } catch (Exception ex) { 264 error = ex; 265 } 266 267 invokeAfterCompletion(); 269 cleanup(); 270 271 if (error != null) { 273 throw new ExceptionFactory().newSystemException(error); 274 } 275 } 276 277 public void setRollbackOnly() throws IllegalStateException , SystemException { 278 debug("TxImpl - setRollbackOnly"); 279 status = STATUS_MARKED_ROLLBACK; 280 } 281 282 283 284 285 public boolean enlistResource(XAResource xaresource) throws RollbackException, IllegalStateException , SystemException { 286 return false; 287 } 288 289 public boolean delistResource(XAResource xaresource, int i) throws IllegalStateException , SystemException { 290 return false; 291 } 292 } 293 | Popular Tags |