All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.jts.jta.TransactionState Maven / Gradle / Ivy

There is a newer version: 8.0.0-JDK17-M7
Show newest version
/*
 * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package com.sun.jts.jta;

import java.util.*;
import javax.transaction.xa.*;
import org.omg.CosTransactions.*;
import com.sun.jts.jtsxa.XID;
import com.sun.jts.codegen.jtsxa.OTSResource;
import com.sun.jts.jtsxa.OTSResourceImpl;
import jakarta.transaction.SystemException;
import jakarta.transaction.RollbackException;
import jakarta.transaction.Synchronization;
import com.sun.jts.jtsxa.Utility;
import com.sun.jts.CosTransactions.Configuration;
import com.sun.jts.CosTransactions.ControlImpl;
import org.omg.CORBA.TRANSACTION_ROLLEDBACK;
import com.sun.jts.CosTransactions.GlobalTID;


import java.util.logging.Logger;
import java.util.logging.Level;
import com.sun.logging.LogDomains;
/**
 * keep track of per-transaction state
 *
 * @author Tony Ng
 */
public class TransactionState {

    /**
     * various association states for an XAResource
     */
    private static final int NOT_EXIST = -1;
    private static final int ASSOCIATED = 0;
    private static final int NOT_ASSOCIATED = 1;
    private static final int ASSOCIATION_SUSPENDED = 2;
    private static final int FAILED = 3;
    private static final int ROLLING_BACK = 4;

    /**
     * a mapping of XAResource -> Integer (state)
     * possible states are listed above
     */
    private Map resourceStates;

    /**
     * Map: XAResource -> Xid
     */
    private Map resourceList;

    /**
     * a set of Xid branches on which xa_start() has been called
     */
    private Set seenXids;

    /**
     * a list of unique resource factory (represented by XAResource)
     * Vector of XAResource objects
     */
    private List factories;

    // the OTS synchronization object for this transaction
    private SynchronizationImpl syncImpl;

    // count of XAResources that are still in active state
    // private int activeResources = 0;

    private GlobalTID gtid;
    private TransactionImpl tran;
    /*
        Logger to log transaction messages
    */

    static Logger _logger = LogDomains.getLogger(TransactionState.class, LogDomains.TRANSACTION_LOGGER);

    // private static TransactionManagerImpl tm = TransactionManagerImpl.getTransactionManagerImpl();

    public TransactionState(GlobalTID gtid, TransactionImpl tran) {
        resourceStates = new HashMap();
        resourceList = new HashMap();
        seenXids = new HashSet();
        factories = new ArrayList();
        this.gtid = gtid;
        this.tran  = tran;
    }


    /**
     * this is called via callback of Synchronization
     * right before a transaction commit or rollback
     * to ensure that all XAResources are properly delisted
     */
    synchronized public void beforeCompletion() {

        boolean exceptionThrown = false;
        XAResource res = null;
        Iterator e = resourceStates.keySet().iterator();
        while (e.hasNext()) {
            try {
                res = (XAResource) e.next();
                int XAState = getXAState(res);
                switch (XAState) {
                    case NOT_ASSOCIATED:
                    case FAILED:
                        break;
                    case ASSOCIATION_SUSPENDED:
                    case ASSOCIATED:
                        Xid xid = (Xid) resourceList.get(res);
                        res.end(xid, XAResource.TMSUCCESS);
                        setXAState(res, NOT_ASSOCIATED);
                        break;
                    case ROLLING_BACK:
                    case NOT_EXIST:
                    default:
                        throw new IllegalStateException("Wrong XA State: " + XAState/*#Frozen*/);
                }
            } catch (Exception ex) {
                setXAState(res, FAILED);
                _logger.log(Level.WARNING,"jts.delist_exception",ex);
                exceptionThrown = true;
            }
        }
        if (exceptionThrown) {
            try {
                tran.setRollbackOnly();
            } catch (Exception ex) {}
        }
    }

    /**
     * This is called from OTS to rollback a particular XAResource
     */
    synchronized public void rollback(XAResource res)
        throws IllegalStateException, XAException {

        // Rollback the requested resource
        _rollback(res);

        // Now rollback all other resources known that are not
        // registered with the RegisteredResources during startAssociation() call
        Iterator e = resourceStates.keySet().iterator();
        while (e.hasNext()) {
            XAResource res0 = (XAResource) e.next();
            if (res0.isSameRM(res) && res0 != res) {
                _end(res0);
            }
        }
    }

    synchronized private void _rollback(XAResource res)
        throws IllegalStateException, XAException {

        Xid xid = (Xid) resourceList.get(res);
        assert_prejdk14(xid != null);
        int XAState = getXAState(res);
        switch (XAState) {
        case NOT_ASSOCIATED:
        case FAILED:
            res.rollback(xid);
            break;
        case ASSOCIATION_SUSPENDED:
        case ASSOCIATED:
            try {
                res.end(xid, XAResource.TMSUCCESS);
            } catch (Exception ex) {
                _logger.log(Level.WARNING,"jts.delist_exception",ex);
            }
            setXAState(res, NOT_ASSOCIATED);
            res.rollback(xid);
            /** was in ASSOCIATED:
            // rollback is deferred until delistment
            setXAState(res, ROLLING_BACK);
            activeResources++;
            **/
            break;
        case ROLLING_BACK:
        case NOT_EXIST:
        default:
            throw new IllegalStateException("Wrong XAState: " +
                                            XAState/*#Frozen*/);
        }
    }

    synchronized private void _end(XAResource res)
            throws IllegalStateException, XAException {

        Xid xid = (Xid) resourceList.get(res);
        assert_prejdk14(xid != null);
        int XAState = getXAState(res);
        switch (XAState) {
        case NOT_ASSOCIATED:
        case FAILED:
            // do nothing
            break;
        case ASSOCIATION_SUSPENDED:
        case ASSOCIATED:
            try {
                res.end(xid, XAResource.TMSUCCESS);
            } catch (Exception ex) {
                _logger.log(Level.WARNING,"jts.delist_exception",ex);
            }
            setXAState(res, NOT_ASSOCIATED);
            break;
        case ROLLING_BACK:
        case NOT_EXIST:
        default:
            throw new IllegalStateException("Wrong XAState: " +
                                            XAState/*#Frozen*/);
        }
    }

    private Xid computeXid(XAResource res, Control control)
        throws Inactive, Unavailable, XAException {

        // one branch id per RM
        int size = factories.size();
        for (int i=0; i 0) {
            res[j--] = (byte) (size % 10);
            size = size / 10;
        }
        int len = 9-j;
        byte [] result = new byte[len];
        System.arraycopy(res, j+1, result, 0, len);
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy