Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.geronimo.transaction.manager;
 
 import java.util.Map;
Adds implementations of XATerminator and XAWork interfaces to basic TransactionManagerImpl

Version:
$Rev: 911974 $ $Date: 2010-02-19 21:36:27 +0100 (Fri, 19 Feb 2010) $
 
 public class GeronimoTransactionManager extends TransactionManagerImpl implements XATerminatorXAWork {
     private final Map importedTransactions = new HashMap();
     private boolean isInRecovery = false;
 
     public GeronimoTransactionManager() throws XAException {
     }
 
     public GeronimoTransactionManager(int defaultTransactionTimeoutSecondsthrows XAException {
         super(defaultTransactionTimeoutSeconds);
     }
 
     public GeronimoTransactionManager(int defaultTransactionTimeoutSecondsTransactionLog transactionLogthrows XAException {
         super(defaultTransactionTimeoutSecondstransactionLog);
     }
 
     public GeronimoTransactionManager(int defaultTransactionTimeoutSecondsXidFactory xidFactoryTransactionLog transactionLogthrows XAException {
         super(defaultTransactionTimeoutSecondsxidFactorytransactionLog);
     }

    
 
     public void commit(Xid xidboolean onePhasethrows XAException {
         Transaction importedTransaction;
         synchronized () {
             importedTransaction = (Transaction.remove(xid);
         }
         if (importedTransaction == null) {
             throw new XAException("No imported transaction for xid: " + xid);
         }
 
         try {
             int status = importedTransaction.getStatus();
             assert status == . || status == ."invalid status: " + status;
         } catch (SystemException e) {
             throw (XAException)new XAException().initCause(e);
         }
         commit(importedTransactiononePhase);
     }

    
 
     public void forget(Xid xidthrows XAException {
         Transaction importedTransaction;
         synchronized () {
             importedTransaction = (Transaction.remove(xid);
         }
         if (importedTransaction == null) {
             throw new XAException("No imported transaction for xid: " + xid);
         }
         //todo is there a correct status test here?
 //        try {
 //            int status = tx.getStatus();
 //            assert status == Status.STATUS_ACTIVE || status == Status.STATUS_PREPARED;
 //        } catch (SystemException e) {
 //            throw new XAException();
 //        }
         forget(importedTransaction);
     }

    
    public int prepare(Xid xidthrows XAException {
        Transaction importedTransaction;
        synchronized () {
            importedTransaction = (Transaction.get(xid);
        }
        if (importedTransaction == null) {
            throw new XAException("No imported transaction for xid: " + xid);
        }
        try {
            int status = importedTransaction.getStatus();
            assert status == .;
        } catch (SystemException e) {
            throw (XAException)new XAException().initCause(e);
        }
        return prepare(importedTransaction);
    }

    
    public Xid[] recover(int flagthrows XAException {
        if (!) {
            if ((flag & .) == 0) {
                throw new XAException(.);
            }
             = true;
        }
        if ((flag & .) != 0) {
             = false;
        }
        //we always return all xids in first call.
        //calling "startrscan" repeatedly starts at beginning of list again.
        if ((flag & .) != 0) {
            Map recoveredXidMap = getExternalXids();
            Xid[] recoveredXids = new Xid[recoveredXidMap.size()];
            int i = 0;
            synchronized () {
                for (Iterator iterator = recoveredXidMap.entrySet().iterator(); iterator.hasNext();) {
                    Map.Entry entry = (Map.Entryiterator.next();
                    Xid xid = (Xidentry.getKey();
                    recoveredXids[i++] = xid;
                    Transaction transaction = (Transactionentry.getValue();
                    .put(xidtransaction);
                }
            }
            return recoveredXids;
        } else {
            return new Xid[0];
        }
    }

    
    public void rollback(Xid xidthrows XAException {
        Transaction importedTransaction;
        synchronized () {
            importedTransaction = (Transaction.remove(xid);
        }
        if (importedTransaction == null) {
            throw new XAException("No imported transaction for xid: " + xid);
        }
        try {
            int status = importedTransaction.getStatus();
            assert status == . || status == .;
        } catch (SystemException e) {
            throw (XAException)new XAException().initCause(e);
        }
        rollback(importedTransaction);
    }
    //XAWork implementation
    public void begin(Xid xidlong txTimeoutMillisthrows XAExceptionInvalidTransactionExceptionSystemExceptionImportedTransactionActiveException {
        Transaction importedTransaction;
        synchronized () {
            importedTransaction = (Transaction.get(xid);
            if (importedTransaction == null) {
                // this does not associate tx with current thread.
                importedTransaction = importXid(xidtxTimeoutMillis);
                .put(xidimportedTransaction);
            }
            // associate the the imported transaction with the current thread
            try {
                resume(importedTransaction);
            } catch (InvalidTransactionException e) {
                // this occures if our transaciton is associated with another thread
                throw (ImportedTransactionActiveException)new ImportedTransactionActiveException(xid).initCause(e);
            }
        }
    }
    public void end(Xid xidthrows XAExceptionSystemException {
        synchronized () {
            Transaction importedTransaction = (Transaction.get(xid);
            if (importedTransaction == null) {
                throw new XAException("No imported transaction for xid: " + xid);
            }
            if (importedTransaction != getTransaction()) {
                throw new XAException("Imported transaction is not associated with the current thread xid: " + xid);
            }
            suspend();
        }
    }
New to GrepCode? Check out our FAQ X