Ejemplo n.º 1
0
 void doCommit() throws XAException, SystemException {
   boolean onePhase = isOnePhase();
   boolean readOnly = true;
   if (!onePhase) {
     // prepare
     status = Status.STATUS_PREPARING;
     LinkedList<Xid> preparedXids = new LinkedList<Xid>();
     Iterator<ResourceElement> itr = resourceList.iterator();
     while (itr.hasNext()) {
       ResourceElement re = itr.next();
       if (!preparedXids.contains(re.getXid())) {
         preparedXids.add(re.getXid());
         int vote = re.getResource().prepare(re.getXid());
         if (vote == XAResource.XA_OK) {
           readOnly = false;
         } else if (vote == XAResource.XA_RDONLY) {
           re.setStatus(RS_READONLY);
         } else {
           // rollback tx
           status = Status.STATUS_MARKED_ROLLBACK;
           return;
         }
       } else {
         // set it to readonly, only need to commit once
         re.setStatus(RS_READONLY);
       }
     }
     status = Status.STATUS_PREPARED;
   }
   // commit
   if (!onePhase && readOnly) {
     status = Status.STATUS_COMMITTED;
     return;
   }
   if (!onePhase) {
     try {
       txManager.getTxLog().markAsCommitting(getGlobalId());
     } catch (IOException e) {
       log.log(Level.SEVERE, "Error writing transaction log", e);
       txManager.setTmNotOk();
       throw Exceptions.withCause(
           new SystemException("TM encountered a problem, " + " error writing transaction log"),
           e);
     }
   }
   status = Status.STATUS_COMMITTING;
   Iterator<ResourceElement> itr = resourceList.iterator();
   while (itr.hasNext()) {
     ResourceElement re = itr.next();
     if (re.getStatus() != RS_READONLY) {
       re.getResource().commit(re.getXid(), onePhase);
     }
   }
   status = Status.STATUS_COMMITTED;
 }
Ejemplo n.º 2
0
 public void setRollbackOnly() throws IllegalStateException {
   if (status == Status.STATUS_ACTIVE
       || status == Status.STATUS_PREPARING
       || status == Status.STATUS_PREPARED
       || status == Status.STATUS_MARKED_ROLLBACK
       || status == Status.STATUS_ROLLING_BACK) {
     status = Status.STATUS_MARKED_ROLLBACK;
   } else {
     throw new IllegalStateException("Tx status is: " + txManager.getTxStatusAsString(status));
   }
 }
Ejemplo n.º 3
0
 public synchronized void registerSynchronization(Synchronization s)
     throws RollbackException, IllegalStateException {
   if (s == null) {
     throw new IllegalArgumentException("Null parameter");
   }
   if (status == Status.STATUS_ACTIVE
       || status == Status.STATUS_PREPARING
       || status == Status.STATUS_MARKED_ROLLBACK) {
     if (!beforeCompletionRunning) {
       syncHooks.add(s);
     } else {
       // avoid CME if synchronization is added in before completion
       syncHooksAdded.add(s);
     }
   } else if (status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK) {
     throw new RollbackException("Tx status is: " + txManager.getTxStatusAsString(status));
   } else {
     throw new IllegalStateException("Tx status is: " + txManager.getTxStatusAsString(status));
   }
 }
Ejemplo n.º 4
0
 @Override
 public String toString() {
   StringBuffer txString =
       new StringBuffer(
           "Transaction("
               + eventIdentifier
               + ")["
               + txManager.getTxStatusAsString(status)
               + ",Resources="
               + resourceList.size()
               + "]");
   //        Iterator<ResourceElement> itr = resourceList.iterator();
   //        while ( itr.hasNext() )
   //        {
   //            txString.append( itr.next().toString() );
   //            if ( itr.hasNext() )
   //            {
   //                txString.append( "," );
   //            }
   //        }
   return txString.toString();
 }
Ejemplo n.º 5
0
 public synchronized boolean delistResource(XAResource xaRes, int flag)
     throws IllegalStateException {
   if (xaRes == null) {
     throw new IllegalArgumentException("Null xa resource");
   }
   if (flag != XAResource.TMSUCCESS && flag != XAResource.TMSUSPEND && flag != XAResource.TMFAIL) {
     throw new IllegalArgumentException("Illegal flag: " + flag);
   }
   ResourceElement re = null;
   Iterator<ResourceElement> itr = resourceList.iterator();
   while (itr.hasNext()) {
     ResourceElement reMatch = itr.next();
     if (reMatch.getResource() == xaRes) {
       re = reMatch;
       break;
     }
   }
   if (re == null) {
     return false;
   }
   if (status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK) {
     try {
       xaRes.end(re.getXid(), flag);
       if (flag == XAResource.TMSUSPEND || flag == XAResource.TMFAIL) {
         re.setStatus(RS_SUSPENDED);
       } else {
         re.setStatus(RS_DELISTED);
       }
       return true;
     } catch (XAException e) {
       log.log(Level.SEVERE, "Unable to delist resource[" + xaRes + "]", e);
       status = Status.STATUS_MARKED_ROLLBACK;
       return false;
     }
   }
   throw new IllegalStateException("Tx status is: " + txManager.getTxStatusAsString(status));
 }
Ejemplo n.º 6
0
 public synchronized void commit()
     throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
         IllegalStateException, SystemException {
   // make sure tx not suspended
   txManager.commit();
 }
Ejemplo n.º 7
0
 TransactionImpl(TxManager txManager) {
   this.txManager = txManager;
   globalId = XidImpl.getNewGlobalId();
   eventIdentifier = txManager.getNextEventIdentifier();
 }
Ejemplo n.º 8
0
 public synchronized boolean enlistResource(XAResource xaRes)
     throws RollbackException, IllegalStateException, SystemException {
   if (xaRes == null) {
     throw new IllegalArgumentException("Null xa resource");
   }
   if (status == Status.STATUS_ACTIVE || status == Status.STATUS_PREPARING) {
     try {
       if (resourceList.size() == 0) {
         if (!globalStartRecordWritten) {
           txManager.writeStartRecord(globalId);
           globalStartRecordWritten = true;
         }
         //
         byte branchId[] = txManager.getBranchId(xaRes);
         Xid xid = new XidImpl(globalId, branchId);
         resourceList.add(new ResourceElement(xid, xaRes));
         xaRes.start(xid, XAResource.TMNOFLAGS);
         try {
           txManager.getTxLog().addBranch(globalId, branchId);
         } catch (IOException e) {
           log.log(Level.SEVERE, "Error writing transaction log", e);
           txManager.setTmNotOk();
           throw Exceptions.withCause(
               new SystemException(
                   "TM encountered a problem, " + " error writing transaction log"),
               e);
         }
         return true;
       }
       Xid sameRmXid = null;
       Iterator<ResourceElement> itr = resourceList.iterator();
       while (itr.hasNext()) {
         ResourceElement re = itr.next();
         if (sameRmXid == null && re.getResource().isSameRM(xaRes)) {
           sameRmXid = re.getXid();
         }
         if (xaRes == re.getResource()) {
           if (re.getStatus() == RS_SUSPENDED) {
             xaRes.start(re.getXid(), XAResource.TMRESUME);
           } else {
             // either enlisted or delisted
             // is TMJOIN correct then?
             xaRes.start(re.getXid(), XAResource.TMJOIN);
           }
           re.setStatus(RS_ENLISTED);
           return true;
         }
       }
       if (sameRmXid != null) // should we join?
       {
         addResourceToList(sameRmXid, xaRes);
         xaRes.start(sameRmXid, XAResource.TMJOIN);
       } else
       // new branch
       {
         // ResourceElement re = resourceList.getFirst();
         byte branchId[] = txManager.getBranchId(xaRes);
         Xid xid = new XidImpl(globalId, branchId);
         addResourceToList(xid, xaRes);
         xaRes.start(xid, XAResource.TMNOFLAGS);
         try {
           txManager.getTxLog().addBranch(globalId, branchId);
         } catch (IOException e) {
           log.log(Level.SEVERE, "Error writing transaction log", e);
           txManager.setTmNotOk();
           throw Exceptions.withCause(
               new SystemException(
                   "TM encountered a problem, " + " error writing transaction log"),
               e);
         }
       }
       return true;
     } catch (XAException e) {
       log.log(Level.SEVERE, "Unable to enlist resource[" + xaRes + "]", e);
       status = Status.STATUS_MARKED_ROLLBACK;
       return false;
     }
   } else if (status == Status.STATUS_ROLLING_BACK
       || status == Status.STATUS_ROLLEDBACK
       || status == Status.STATUS_MARKED_ROLLBACK) {
     throw new RollbackException("Tx status is: " + txManager.getTxStatusAsString(status));
   }
   throw new IllegalStateException("Tx status is: " + txManager.getTxStatusAsString(status));
 }
Ejemplo n.º 9
0
 public synchronized void rollback() throws IllegalStateException, SystemException {
   // make sure tx not suspended
   txManager.rollback();
 }
Ejemplo n.º 10
0
 private void commit(HighlyAvailableGraphDatabase db, javax.transaction.Transaction tx)
     throws Exception {
   TxManager txManager = db.getDependencyResolver().resolveDependency(TxManager.class);
   txManager.resume(tx);
   txManager.commit();
 }
Ejemplo n.º 11
0
 private void initializeTxIfFirst() {
   // The main point of initializing transaction (for HA) is in TransactionImpl, so this is
   // for that extra point where grabbing a lock
   Transaction tx = tm.getTransaction();
   if (!txHook.hasAnyLocks(tx)) txHook.initializeTransaction(tm.getEventIdentifier());
 }
Ejemplo n.º 12
0
 private int getLocalTxId() {
   return tm.getEventIdentifier();
 }