com.sun.jts.jta.TransactionState Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-embedded-all Show documentation
Show all versions of payara-embedded-all Show documentation
Payara-Embedded-All Distribution of the Payara Project
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2013 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.jts.jta;
import java.util.*;
import javax.transaction.xa.*;
import javax.transaction.*;
import org.omg.CosTransactions.*;
import com.sun.jts.jta.NativeXAResource;
import com.sun.jts.jtsxa.XID;
import com.sun.jts.codegen.jtsxa.OTSResource;
import com.sun.jts.jtsxa.OTSResourceImpl;
import javax.transaction.SystemException;
import javax.transaction.RollbackException;
import javax.transaction.Synchronization;
import com.sun.jts.jtsxa.Utility;
import com.sun.jts.CosTransactions.Configuration;
import com.sun.jts.CosTransactions.ControlImpl;
import com.sun.jts.CosTransactions.TopCoordinator;
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;
import com.sun.jts.utils.LogFormatter;
/**
* 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;
}
}