1 8 9 package com.sleepycat.je.jca.ra; 10 11 import javax.resource.ResourceException ; 12 import javax.resource.spi.ConnectionEvent ; 13 14 import com.sleepycat.je.DatabaseException; 15 import com.sleepycat.je.Transaction; 16 import com.sleepycat.je.TransactionConfig; 17 import com.sleepycat.je.XAEnvironment; 18 19 public class JELocalTransaction 20 implements javax.resource.cci.LocalTransaction , 21 javax.resource.spi.LocalTransaction { 22 23 private static boolean DEBUG = false; 24 25 private transient XAEnvironment env; 26 private transient TransactionConfig transConfig; 27 private transient JEManagedConnection mgdConn; 28 29 JELocalTransaction(XAEnvironment env, 30 TransactionConfig transConfig, 31 JEManagedConnection mgdConn) { 32 this.env = env; 33 this.transConfig = transConfig; 34 this.mgdConn = mgdConn; 35 } 36 37 public Transaction getTransaction() 38 throws DatabaseException { 39 40 return env.getThreadTransaction(); 41 } 42 43 protected XAEnvironment getEnv() { 44 return env; 45 } 46 47 private void checkEnv(String methodName) 48 throws ResourceException { 49 50 if (env == null) { 51 throw new ResourceException ("env is null in " + methodName); 52 } 53 } 54 55 58 59 public void begin() 60 throws ResourceException { 61 62 checkEnv("begin"); 63 long id = -1; 64 try { 65 Transaction txn = env.beginTransaction(null, transConfig); 66 env.setThreadTransaction(txn); 67 id = txn.getId(); 68 } catch (DatabaseException DE) { 69 throw new ResourceException ("During begin: " + DE.toString()); 70 } 71 72 ConnectionEvent connEvent = new ConnectionEvent 73 (mgdConn, ConnectionEvent.LOCAL_TRANSACTION_STARTED); 74 connEvent.setConnectionHandle(mgdConn); 75 mgdConn.sendConnectionEvent(connEvent); 76 77 if (DEBUG) { 78 System.out.println("JELocalTransaction.begin " + id); 79 } 80 } 81 82 public void commit() 83 throws ResourceException { 84 85 checkEnv("commit"); 86 try { 87 env.getThreadTransaction().commit(); 88 } catch (DatabaseException DE) { 89 ResourceException ret = new ResourceException (DE.toString()); 90 ret.initCause(DE); 91 throw ret; 92 } finally { 93 env.setThreadTransaction(null); 94 } 95 96 ConnectionEvent connEvent = new ConnectionEvent 97 (mgdConn, ConnectionEvent.LOCAL_TRANSACTION_COMMITTED); 98 connEvent.setConnectionHandle(mgdConn); 99 mgdConn.sendConnectionEvent(connEvent); 100 101 if (DEBUG) { 102 System.out.println("JELocalTransaction.commit"); 103 } 104 } 105 106 public void rollback() 107 throws ResourceException { 108 109 checkEnv("rollback"); 110 try { 111 env.getThreadTransaction().abort(); 112 } catch (DatabaseException DE) { 113 ResourceException ret = new ResourceException (DE.toString()); 114 ret.initCause(DE); 115 throw ret; 116 } finally { 117 env.setThreadTransaction(null); 118 } 119 120 ConnectionEvent connEvent = new ConnectionEvent 121 (mgdConn, ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK); 122 connEvent.setConnectionHandle(mgdConn); 123 mgdConn.sendConnectionEvent(connEvent); 124 125 if (DEBUG) { 126 System.out.println("JELocalTransaction.rollback"); 127 } 128 } 129 } 130 | Popular Tags |