| 1 package in.co.daffodil.db.jdbc; 2 3 import javax.transaction.xa.XAResource ; 4 import javax.sql.XAConnection ; 5 import java.sql.SQLException ; 6 import javax.sql.ConnectionEventListener ; 7 import java.sql.Connection ; 8 import javax.transaction.xa.Xid ; 9 import javax.transaction.xa.XAException ; 10 import com.daffodilwoods.daffodildb.server.serversystem._DXAResource; 11 import com.daffodilwoods.daffodildb.server.serversystem._Connection; 12 import java.util.Properties ; 13 import com.daffodilwoods.database.resource.DException; 14 15 public class DBXAConnectionResource extends DBPooledConnection implements XAConnection , XAResource { 16 17 private int transactionTimeOut; 18 19 public DBXAConnectionResource(DBXADataSource dbxaResource0,Connection connection0) { 20 super(dbxaResource0,connection0); 21 transactionTimeOut = 10; 22 } 23 24 public XAResource getXAResource() throws java.sql.SQLException { 25 return this; 26 } 27 28 public synchronized void commit(Xid xid, boolean parm2) throws javax.transaction.xa.XAException { 29 if(xid == null) 30 throw new XAException ( XAException.XAER_INVAL ); 31 try{ 32 xid = getServerXID(xid); 33 synchronized(dbxaResource){ 34 if(parm2) 35 dbxaResource.getDXAResource().commit(xid); 36 else { 37 dbxaResource.getDXAResource().makePersistent(xid); 38 } 39 } 40 }catch (SQLException ex) { 41 throw new javax.transaction.xa.XAException (ex.getMessage()); 42 } 43 } 44 45 public synchronized void end(Xid parm1, int flags) throws javax.transaction.xa.XAException { 46 try{ 47 synchronized(dbxaResource){ 48 parm1 = getServerXID(parm1); 49 dbxaResource.getDXAResource().end(parm1,flags); 50 if (flags == TMFAIL && connection != null){ 51 try { 52 connection.rollback(); 53 }catch(Exception except){ 54 except.printStackTrace(); 55 } 56 } 57 if ( flags == TMSUCCESS || flags == TMSUSPEND) { 58 }else 59 throw new XAException ( XAException.XAER_INVAL ); 60 } 61 }catch (SQLException ex) { 62 throw new javax.transaction.xa.XAException (ex.getMessage()); 63 } 64 } 65 66 public synchronized void forget(Xid parm1) throws javax.transaction.xa.XAException { 67 try{ 68 parm1 = getServerXID(parm1); 69 dbxaResource.getDXAResource().forget(parm1); 70 if (connection != null){ 71 } 72 }catch (SQLException ex) { 73 throw new javax.transaction.xa.XAException (ex.getMessage()); 74 } 75 } 76 77 public synchronized int getTransactionTimeout() throws javax.transaction.xa.XAException { 78 return transactionTimeOut; 79 } 80 81 public synchronized boolean isSameRM(XAResource xaRes) throws javax.transaction.xa.XAException { 82 if ( xaRes == null || ! ( xaRes instanceof DBXAConnectionResource ) ) 83 return false; 84 if (xaRes == this){ 85 ; return true; 87 } 88 return false; 89 } 90 91 public synchronized int prepare(Xid parm1) throws javax.transaction.xa.XAException { 92 try{ 93 parm1 = getServerXID(parm1); 94 return dbxaResource.getDXAResource().prepare(parm1); 95 }catch (SQLException ex) { 96 throw new javax.transaction.xa.XAException (ex.getMessage()); 97 } 98 } 99 100 public Xid [] recover(int parm1) throws javax.transaction.xa.XAException { 101 102 throw new java.lang.UnsupportedOperationException ("Method recover() not yet implemented."); 103 } 104 105 public synchronized void rollback(Xid xid) throws javax.transaction.xa.XAException { 106 if ( xid == null ) 107 throw new XAException ( XAException.XAER_INVAL ); 108 try{ 109 synchronized(dbxaResource){ 110 xid = getServerXID(xid); 111 dbxaResource.getDXAResource().rollback(xid); 112 } 113 }catch (SQLException ex) { 114 throw new javax.transaction.xa.XAException (ex.getMessage()); 115 } 116 } 117 118 public synchronized boolean setTransactionTimeout(int seconds) throws javax.transaction.xa.XAException { 119 if ( seconds < 0 ) 120 throw new XAException ( XAException.XAER_INVAL ); 121 if ( seconds == 0 ) 122 seconds = transactionTimeOut; 123 if ( connection != null ) { 124 return true; 125 } 126 return false; 127 } 128 129 public synchronized void start(Xid parm1, int parm2) throws javax.transaction.xa.XAException { 130 try { 131 synchronized(dbxaResource){ 132 parm1 = getServerXID(parm1); 133 Properties prop = dbxaResource.getConnectionProperties(); 134 _Connection conn = dbxaResource.getDXAResource().start(parm1,parm2,prop); 135 if(conn == null) 136 return; 137 { 138 ((DaffodilDBConnection)connection).setServerConnection(conn); 139 } 140 conn.setAutoCommit(false); 141 } 142 }catch(Exception de){ 143 throw new XAException ( XAException.XAER_RMERR ); 144 } 145 } 146 147 private Xid getServerXID(Xid xid){ 148 return new DBXID(xid); 149 } 150 } 151 152 class DBXID implements Xid ,java.io.Serializable { 153 154 int formatID; 155 byte[] globalTransactionId; 156 byte[] branchQualifier; 157 int hashCode = 1; 158 159 DBXID(Xid xid){ 160 formatID = xid.getFormatId(); 161 globalTransactionId = xid.getGlobalTransactionId(); 162 branchQualifier = xid.getBranchQualifier(); 163 hashCode = formatID; 164 for (int i = 0; i < globalTransactionId.length; i++) { 165 hashCode = 31*hashCode + (int)globalTransactionId[i]; 166 } 167 168 } 169 public String toString() { 170 return "Dxid :: "+ formatID +" : "+ globalTransactionId[0] +globalTransactionId[1]+" "+hashCode()+" "+hashCode; } 172 173 174 public int getFormatId() { 175 return formatID; 176 } 177 178 public byte[] getGlobalTransactionId() { 179 return globalTransactionId; 180 } 181 182 public byte[] getBranchQualifier() { 183 return branchQualifier; 184 } 185 186 public int hashCode(){ 187 return hashCode; 188 } 189 190 public boolean equals(Object o) { 191 if(o == null || !(o instanceof DBXID)) 192 return false; 193 if(formatID != ((Xid )o).getFormatId()) 194 return false; 195 if(!byteArrayComparator(((Xid )o).getGlobalTransactionId(),globalTransactionId)) 196 return false; 197 if(!byteArrayComparator(((Xid )o).getBranchQualifier(),branchQualifier)) 198 return false; 199 return true; 200 } 201 202 private boolean byteArrayComparator(byte[] b1, byte[] b2) { 203 int len = b1.length; 204 if(len != b2.length) 205 return false; 206 int i=0; 207 while( b1[i] == b2[i] ) { 208 i++; 209 if (i >=len) 210 return true; 211 } 212 return false; 213 } 214 } 215 | Popular Tags |