FileDocCategorySizeDatePackage
ConnectorXAResource.javaAPI DocGlassfish v2 API10460Thu Jul 19 03:02:58 BST 2007com.sun.enterprise.resource

ConnectorXAResource

public class ConnectorXAResource extends Object implements XAResource
author
Tony Ng

Fields Summary
private Object
userHandle
private ResourceSpec
spec
private String
poolName
private ResourceAllocator
alloc
private com.sun.enterprise.PoolManager
poolMgr
private ManagedConnection
localConnection
private ClientSecurityInfo
info
private ConnectionEventListener
listener
private ResourceHandle
localHandle_
private static Hashtable
listenerTable
static Logger
_logger
Constructors Summary
public ConnectorXAResource(ResourceHandle handle, ResourceSpec spec, ResourceAllocator alloc, ClientSecurityInfo info)




      
                                
                                
                                  

        // initially userHandle is associated with mc
        this.poolMgr = Switch.getSwitch().getPoolManager();
        this.userHandle = null;
        this.spec = spec;
	    this.poolName = spec.getConnectionPoolName();
        this.alloc = alloc;
        this.info = info;
        localConnection = (ManagedConnection) handle.getResource();
        localHandle_ = handle;
    
Methods Summary
public voidcommit(javax.transaction.xa.Xid xid, boolean onePhase)

        try {
            ResourceHandle handle = getResourceHandle();
            ManagedConnection mc = (ManagedConnection) handle.getResource();
            mc.getLocalTransaction().commit();
        } catch (Exception ex) {
            handleResourceException(ex);
        }finally{
            resetAssociation();
        }
    
public voidend(javax.transaction.xa.Xid xid, int flags)

        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "connection_sharing_end");
        }
        try {
            ResourceHandle handleInTransaction = getResourceHandle();
            if (!localHandle_.equals(handleInTransaction)) {
                LocalTxConnectionEventListener l = (LocalTxConnectionEventListener) handleInTransaction.getListener();

                ResourceHandle handle = l.removeAssociation(userHandle);
                if (handle != null) { // not needed, just to be sure.
                    ManagedConnection associatedConnection = (ManagedConnection) handle.getResource();
                    associatedConnection.associateConnection(userHandle);
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.log(Level.FINE, "connection_sharing_reset_association",
                                userHandle);
                    }
                }
            }
        } catch (Exception e) {
            handleResourceException(e);
        }
    
public voidforget(javax.transaction.xa.Xid xid)

	_logger.fine("Well, forget is called for xid :"+xid);
        // no-op
    
public static voidfreeListener(ManagedConnection mc)

        listenerTable.remove(mc);
    
private ResourceHandlegetResourceHandle()

        try {
            ResourceHandle h = null;
            J2EETransactionManager txMgr = Switch.getSwitch().getTransactionManager();
            J2EETransaction j2eetran = (J2EETransaction) txMgr.getTransaction();
            if (j2eetran == null) {      //Only if some thing is wrong with tx manager.
                h = localHandle_;        //Just return the local handle.
            } else {
                h = j2eetran.getNonXAResource();
            }
            if (h.getResourceState().isUnenlisted()) {
                ManagedConnection mc = (ManagedConnection) h.getResource();
                // begin the local transaction if first time
                // this ManagedConnection is used in this JTA transaction
                mc.getLocalTransaction().begin();
            }
            return h;
        } catch (Exception ex) {
            _logger.log(Level.SEVERE, "poolmgr.system_exception", ex);
            throw new PoolingException(ex.toString(), ex);
        }
    
public intgetTransactionTimeout()

        return 0;
    
private voidhandleResourceException(java.lang.Exception ex)

        _logger.log(Level.SEVERE,"poolmgr.system_exception",ex);
        XAException xae = new XAException(ex.toString());
        xae.errorCode = XAException.XAER_RMERR;
        throw xae;
    
public booleanisSameRM(javax.transaction.xa.XAResource other)

        if (this == other) return true;
        if (other == null) return false;
        if (other instanceof ConnectorXAResource) {
            ConnectorXAResource obj = (ConnectorXAResource) other;
            return (this.spec.equals(obj.spec) &&
                    this.info.equals(obj.info));
        } else {
            return false;
        }
    
public intprepare(javax.transaction.xa.Xid xid)

	    return Configuration.LAO_PREPARE_OK;
    
public javax.transaction.xa.Xid[]recover(int flag)

        return new Xid[0];
    
private voidresetAssociation()

        try {
        ResourceHandle handle = getResourceHandle();

            LocalTxConnectionEventListener l = (LocalTxConnectionEventListener)handle.getListener();
            //Get all associated Handles and reset their ManagedConnection association.
            Map associatedHandles = l.getAssociatedHandles();
            if(associatedHandles != null ){
                Set<Map.Entry> userHandles = associatedHandles.entrySet();
                for(Map.Entry userHandleEntry : userHandles ){
                    ResourceHandle associatedHandle = (ResourceHandle)userHandleEntry.getValue();
                    ManagedConnection associatedConnection = (ManagedConnection)associatedHandle.getResource();
                    associatedConnection.associateConnection(userHandleEntry.getKey());
                    if(_logger.isLoggable(Level.FINE)){
                        _logger.log(Level.FINE, "connection_sharing_reset_association",
                                userHandleEntry.getKey());
                    }
                }
                //all associated handles are mapped back to their actual Managed Connection. Clear the associations.
                associatedHandles.clear();
            }

        } catch (Exception ex) {
            handleResourceException(ex);
        }
    
public voidrollback(javax.transaction.xa.Xid xid)

        try {
	    ResourceHandle handle = getResourceHandle();
	    ManagedConnection mc = (ManagedConnection) handle.getResource();
            mc.getLocalTransaction().rollback();
        } catch (Exception ex) {
            handleResourceException(ex);
        }finally{
            resetAssociation();
        }
    
public booleansetTransactionTimeout(int seconds)

        return false;
    
public voidsetUserHandle(java.lang.Object userHandle)

        this.userHandle = userHandle;
    
public voidstart(javax.transaction.xa.Xid xid, int flags)

        try {
            ResourceHandle handle = getResourceHandle();
            if (!localHandle_.equals(handle)) {
                ManagedConnection mc = (ManagedConnection) handle.getResource();
                mc.associateConnection(userHandle);
                LocalTxConnectionEventListener l =
                        (LocalTxConnectionEventListener) handle.getListener();
                if(_logger.isLoggable(Level.FINE)){
                    _logger.log(Level.FINE, "connection_sharing_start",  userHandle);
                }
                l.associateHandle(userHandle, localHandle_);
            }
        } catch (Exception ex) {
            handleResourceException(ex);
        }