1 9 package org.ozoneDB.xa; 10 11 import org.ozoneDB.DxLib.*; 12 import org.ozoneDB.ExternalDatabase; 13 import org.ozoneDB.TransactionException; 14 15 import javax.transaction.Status ; 16 import javax.transaction.xa.*; 17 import java.io.PrintStream ; 18 19 20 28 public class OzoneXAResource implements XAResource { 29 30 private ExternalDatabase db; 31 32 private DxHashMap xids; 33 34 private PrintStream log; 35 36 37 public OzoneXAResource( ExternalDatabase _db ) { 38 db = _db; 39 xids = new DxHashMap(); 40 log = System.out; 41 } 42 43 44 public String toString() { 45 return "OzoneXAResource [" + db + "]"; 46 } 47 48 49 protected void debug( String msg ) { 50 log.println( "OzoneXAResource: " + msg ); 51 } 52 53 54 63 public void start( Xid xid, int flags ) throws XAException { 64 debug( "start(): xid:" + xid.hashCode() + ", flags:" + flags ); 65 66 if (xid == null) { 67 throw new XAException( XAException.XAER_INVAL ); 68 } 69 if (db == null) { 70 throw new XAException( XAException.XAER_OUTSIDE ); 71 } 72 73 try { 74 switch (flags) { 75 case TMNOFLAGS: { 76 debug( "start(): TMNOFLAGS..." ); 77 XATransaction tx = new XATransaction( db ); 78 if (!xids.addForKey( tx, xid )) { 79 throw new XAException( XAException.XAER_DUPID ); 80 } 81 db.beginTX( tx ); 82 break; 83 } 84 case TMRESUME : { 85 debug( "start(): TMRESUME..." ); 86 XATransaction tx = (XATransaction)xids.elementForKey( xid ); 87 if (tx == null) { 88 throw new XAException( "Specified XID not found." ); 89 } 90 db.joinTX( tx ); 91 break; 92 } 93 case TMJOIN : { 94 debug( "start(): TMJOIN..." ); 95 throw new RuntimeException ( "Not implemented yet." ); 96 } 97 default: { 98 debug( "start(): unknown flag..." ); 99 100 throw new XAException( XAException.XAER_INVAL ); 102 } 103 } 104 } 105 catch (XAException e) { 106 throw e; 107 } 108 catch (Exception e) { 109 log.println( e ); 110 throw new XAException( XAException.XAER_RMERR ); 111 } 112 } 113 114 115 129 public void end( Xid xid, int flags ) throws XAException { 130 debug( "end(): xid:" + xid.hashCode() + ", flags:" + flags ); 131 132 if (xid == null) { 133 throw new XAException( XAException.XAER_INVAL ); 134 } 135 136 try { 137 XATransaction tx = (XATransaction)xids.elementForKey( xid ); 138 if (xid == null) { 139 throw new XAException( XAException.XAER_NOTA ); 140 } 141 142 switch (flags) { 143 case TMSUCCESS: { 144 debug( "end(): TMSUCCESS..." ); 145 db.leaveTX( tx ); 146 break; 147 } 148 case TMFAIL: { 149 debug( "end(): TMFAIL..." ); 150 db.rollbackTX( tx ); 151 break; 152 } 153 case TMSUSPEND: { 154 debug( "end(): TMSUSPEND..." ); 155 db.leaveTX( tx ); 156 break; 157 } 158 default: { 159 debug( "end(): unknown flag..." ); 160 161 throw new XAException( XAException.XAER_INVAL ); 163 } 164 } 165 } 166 catch (XAException e) { 167 throw e; 168 } 169 catch (Exception e) { 170 log.println( e ); 171 throw new XAException( XAException.XAER_RMERR ); 172 } 173 } 174 175 176 180 public int prepare( Xid xid ) throws XAException { 181 debug( "prepare(): xid:" + xid.hashCode() ); 182 183 if (xid == null) { 184 throw new XAException( XAException.XAER_INVAL ); 185 } 186 187 try { 188 XATransaction tx = (XATransaction)xids.elementForKey( xid ); 189 if (xid == null) { 190 throw new XAException( XAException.XAER_NOTA ); 191 } 192 db.prepareTX( tx ); 193 return XA_OK; 194 } 195 catch (XAException e) { 196 throw e; 197 } 198 catch (TransactionException e) { 199 log.println( e ); 200 throw new XAException( XAException.XA_RBROLLBACK ); 201 } 202 catch (Exception e) { 203 log.println( e ); 204 throw new XAException( XAException.XAER_RMERR ); 205 } 206 } 207 208 209 212 public void commit( Xid xid, boolean onePhase ) throws XAException { 213 debug( "commit(): xid:" + xid.hashCode() + ", onePhase:" + onePhase ); 214 215 if (xid == null) { 216 throw new XAException( XAException.XAER_INVAL ); 217 } 218 219 try { 220 XATransaction tx = (XATransaction)xids.elementForKey( xid ); 221 if (xid == null) { 222 throw new XAException( XAException.XAER_NOTA ); 223 } 224 db.commitTX( tx, onePhase ); 225 } 226 catch (XAException e) { 227 throw e; 228 } 229 catch (TransactionException e) { 230 log.println( e ); 231 throw new XAException( XAException.XA_RBROLLBACK ); 232 } 233 catch (Exception e) { 234 log.println( e ); 235 throw new XAException( XAException.XAER_RMERR ); 236 } 237 } 238 239 240 244 public void rollback( Xid xid ) throws XAException { 245 debug( "rollback(): xid:" + xid.hashCode() ); 246 247 if (xid == null) { 248 throw new XAException( XAException.XAER_INVAL ); 249 } 250 251 try { 252 XATransaction tx = (XATransaction)xids.elementForKey( xid ); 253 if (xid == null) { 254 throw new XAException( XAException.XAER_NOTA ); 255 } 256 db.rollbackTX( tx ); 257 } 258 catch (XAException e) { 259 throw e; 260 } 261 catch (Exception e) { 262 log.println( e ); 263 throw new XAException( XAException.XAER_RMERR ); 264 } 265 } 266 267 268 272 public void forget( Xid xid ) throws XAException { 273 debug( "forget(): xid:" + xid.hashCode() ); 274 275 xids.removeForKey( xid ); 276 } 277 278 279 288 public boolean setTransactionTimeout( int seconds ) throws XAException { 289 debug( "setTransactionTimeout(): seconds:" + seconds ); 290 return false; 291 } 292 293 294 301 public int getTransactionTimeout() throws XAException { 302 debug( "getTransactionTimeout():" ); 303 return -1; 304 } 305 306 307 312 public boolean isSameRM( XAResource xares ) throws XAException { 313 debug( "isSameRM(): xares:" + xares ); 314 315 if (xares != null && xares instanceof OzoneXAResource) { 316 return db == ((OzoneXAResource)xares).db; 317 } else { 318 return false; 319 } 320 } 321 322 323 329 public Xid[] recover( int flag ) throws XAException { 330 debug( "recover(): flags:" + flag ); 331 332 try { 333 DxArrayBag result = new DxArrayBag(); 334 DxIterator it = xids.iterator(); 335 while (it.next() != null) { 336 XATransaction tx = (XATransaction)it.object(); 337 if (db.getStatusTX( tx ) == Status.STATUS_PREPARED) { 338 result.add( tx ); 339 } 340 } 341 342 return (Xid[])result.toArray(); 343 } 344 catch (Exception e) { 345 throw new XAException( e.toString() ); 346 } 347 } 348 349 } 350 | Popular Tags |