1 21 22 package org.apache.derby.impl.store.raw.xact; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 26 import org.apache.derby.iapi.services.context.ContextManager; 27 28 import org.apache.derby.iapi.services.sanity.SanityManager; 29 30 import org.apache.derby.iapi.error.StandardException; 31 32 import org.apache.derby.iapi.store.access.xa.XAResourceManager; 33 import org.apache.derby.iapi.store.access.xa.XAXactId; 34 import org.apache.derby.iapi.store.access.AccessFactoryGlobals; 35 36 import org.apache.derby.iapi.store.raw.GlobalTransactionId; 37 import org.apache.derby.iapi.store.raw.RawStoreFactory; 38 import org.apache.derby.iapi.store.raw.Transaction; 39 40 41 import org.apache.derby.impl.store.raw.xact.GlobalXactId; 42 import org.apache.derby.impl.store.raw.xact.TransactionTable; 43 import org.apache.derby.impl.store.raw.xact.TransactionTableEntry; 44 import org.apache.derby.impl.store.raw.xact.Xact; 45 46 import java.util.Enumeration ; 47 import java.util.Hashtable ; 48 49 import javax.transaction.xa.Xid ; 50 import javax.transaction.xa.XAResource ; 51 52 78 79 public class XactXAResourceManager implements XAResourceManager 80 { 81 85 private TransactionTable transaction_table; 86 private RawStoreFactory rsf; 87 88 92 public XactXAResourceManager( 93 RawStoreFactory rsf, 94 TransactionTable tt) 95 { 96 this.rsf = rsf; 97 this.transaction_table = tt; 98 } 99 100 104 105 109 110 124 public void commit( 125 ContextManager cm, 126 Xid xid, 127 boolean onePhase) 128 throws StandardException 129 { 130 Transaction rawtran = 131 rsf.findUserTransaction(cm, AccessFactoryGlobals.USER_TRANS_NAME); 132 133 if (rawtran == null) 136 { 137 throw StandardException.newException( 138 SQLState.STORE_XA_PROTOCOL_VIOLATION); 139 } 140 141 if (SanityManager.DEBUG) 142 { 143 SanityManager.ASSERT(rawtran != null); 144 145 SanityManager.ASSERT( 146 (new GlobalXactId( 147 xid.getFormatId(), 148 xid.getGlobalTransactionId(), 149 xid.getBranchQualifier())).equals(rawtran.getGlobalId())); 150 } 151 152 rawtran.xa_commit(onePhase); 153 } 154 155 174 public ContextManager find( 175 Xid xid) 176 { 177 return( 178 transaction_table.findTransactionContextByGlobalId( 179 new GlobalXactId( 180 xid.getFormatId(), 181 xid.getGlobalTransactionId(), 182 xid.getBranchQualifier()))); 183 } 184 185 200 public void forget( 201 ContextManager cm, 202 Xid xid) 203 throws StandardException 204 { 205 Transaction rawtran = 206 rsf.findUserTransaction(cm, AccessFactoryGlobals.USER_TRANS_NAME); 207 208 if (SanityManager.DEBUG) 209 { 210 SanityManager.ASSERT( 211 new GlobalXactId( 212 xid.getFormatId(), 213 xid.getGlobalTransactionId(), 214 xid.getBranchQualifier()).equals(rawtran.getGlobalId())); 215 } 216 217 throw StandardException.newException( 220 SQLState.STORE_XA_PROTOCOL_VIOLATION); 221 } 222 223 224 246 public Xid [] recover(int flags) 247 throws StandardException 248 { 249 XAXactId[] ret_xid_list; 250 251 if ((flags & XAResource.TMSTARTRSCAN) != 0) 252 { 253 Hashtable trans_hashtable = transaction_table.getTableForXA(); 254 XAXactId[] xid_list = new XAXactId[trans_hashtable.size()]; 255 int num_prepared = 0; 256 257 synchronized (trans_hashtable) 259 { 260 int i = 0; 261 262 for (Enumeration e = trans_hashtable.elements(); 263 e.hasMoreElements(); i++) 264 { 265 Xact xact = 266 ((TransactionTableEntry) e.nextElement()).getXact(); 267 268 if (xact.isPrepared()) 269 { 270 GlobalTransactionId xa_id = xact.getGlobalId(); 271 272 xid_list[i] = 273 new XAXactId( 274 xa_id.getFormat_Id(), 275 xa_id.getGlobalTransactionId(), 276 xa_id.getBranchQualifier()); 277 num_prepared++; 278 } 279 } 280 } 281 282 ret_xid_list = new XAXactId[num_prepared]; 284 int ret_index = 0; 285 for (int i = xid_list.length; i-- > 0; ) 286 { 287 if (xid_list[i] != null) 288 ret_xid_list[ret_index++] = xid_list[i]; 289 } 290 291 if (SanityManager.DEBUG) 292 { 293 SanityManager.ASSERT(ret_index == num_prepared); 294 } 295 } 296 else 297 { 298 ret_xid_list = new XAXactId[0]; 299 } 300 301 return(ret_xid_list); 302 } 303 304 316 public void rollback( 317 ContextManager cm, 318 Xid xid) 319 throws StandardException 320 { 321 Transaction rawtran = 322 rsf.findUserTransaction(cm, AccessFactoryGlobals.USER_TRANS_NAME); 323 324 if (rawtran == null) 327 { 328 throw StandardException.newException( 329 SQLState.STORE_XA_PROTOCOL_VIOLATION); 330 } 331 332 if (SanityManager.DEBUG) 333 { 334 335 SanityManager.ASSERT( 336 new GlobalXactId( 337 xid.getFormatId(), 338 xid.getGlobalTransactionId(), 339 xid.getBranchQualifier()).equals(rawtran.getGlobalId())); 340 } 341 342 rawtran.xa_rollback(); 343 } 344 345 349 } 350 | Popular Tags |