Search in sources :

Example 41 with ManagedConnection

use of javax.resource.spi.ManagedConnection in project geode by apache.

the class JCAConnectionManagerImpl method connectionClosed.

/**
   * Callback for Connection Closed.
   * 
   * @param event ConnectionEvent Object.
   */
public void connectionClosed(ConnectionEvent event) {
    if (isActive) {
        ManagedConnection conn = (ManagedConnection) event.getSource();
        XAResource xar = null;
        if (xaResourcesMap.get(conn) != null)
            xar = (XAResource) xaResourcesMap.get(conn);
        xaResourcesMap.remove(conn);
        try {
            Transaction txn = transManager.getTransaction();
            if (txn != null && xar != null) {
                txn.delistResource(xar, XAResource.TMSUCCESS);
            }
        } catch (Exception se) {
            String exception = "JCAConnectionManagerImpl::connectionClosed: Exception occurred due to " + se;
            if (logger.isDebugEnabled()) {
                logger.debug(exception, se);
            }
        }
        mannPoolCache.returnPooledConnectionToPool(conn);
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) Transaction(javax.transaction.Transaction) ManagedConnection(javax.resource.spi.ManagedConnection) ResourceException(javax.resource.ResourceException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException)

Example 42 with ManagedConnection

use of javax.resource.spi.ManagedConnection in project geode by apache.

the class FacetsJCAConnectionManagerImpl method connectionClosed.

/**
   * Callback for Connection Closed.
   * 
   * @param event ConnectionEvent Object.
   */
public void connectionClosed(ConnectionEvent event) {
    if (isActive) {
        ManagedConnection conn = (ManagedConnection) event.getSource();
        TransactionManagerImpl transManager = TransactionManagerImpl.getTransactionManager();
        try {
            Transaction txn = transManager.getTransaction();
            if (txn == null) {
                mannPoolCache.returnPooledConnectionToPool(conn);
            }
        } catch (Exception se) {
            String exception = "FacetsJCAConnectionManagerImpl::connectionClosed: Exception occurred due to " + se.getMessage();
            if (logger.isDebugEnabled()) {
                logger.debug(exception, se);
            }
        }
    }
}
Also used : Transaction(javax.transaction.Transaction) TransactionManagerImpl(org.apache.geode.internal.jta.TransactionManagerImpl) ManagedConnection(javax.resource.spi.ManagedConnection) ResourceException(javax.resource.ResourceException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException)

Example 43 with ManagedConnection

use of javax.resource.spi.ManagedConnection in project geode by apache.

the class FacetsJCAConnectionManagerImpl method allocateConnection.

/*
   * allocates a ManagedConnection from the ConnectionPool or creates a new
   * ManagedConnection. @param javax.resource.spi.ManagedConnectionFactory
   * 
   * @param javax.resource.spi.ConnectionRequestInfo
   * 
   * @throws ResourceException
   */
public Object allocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo reqInfo) throws ResourceException {
    if (!isActive) {
        throw new ResourceException(LocalizedStrings.FacetsJCAConnectionManagerImpl_FACETSJCACONNECTIONMANAGERIMPLALLOCATECONNECTIONNO_VALID_CONNECTION_AVAILABLE.toLocalizedString());
    }
    ManagedConnection conn = null;
    try {
        conn = (ManagedConnection) mannPoolCache.getPooledConnectionFromPool();
    } catch (PoolException ex) {
        ex.printStackTrace();
        throw new ResourceException(LocalizedStrings.FacetsJCAConnectionManagerImpl_FACETSJCACONNECTIONMANAGERIMPL_ALLOCATECONNECTION_IN_GETTING_CONNECTION_FROM_POOL_DUE_TO_0.toLocalizedString(ex.getMessage()), ex);
    }
    // Transaction Manager.
    try {
        synchronized (this) {
            if (transManager == null) {
                transManager = JNDIInvoker.getTransactionManager();
            }
        }
        Transaction txn = transManager.getTransaction();
        if (txn != null) {
            // Check if Data Source provides XATransaction
            // if(configs.getTransactionType = "XATransaction")
            XAResource xar = conn.getXAResource();
            txn.enlistResource(xar);
            java.util.List resList = (List) xalistThreadLocal.get();
            if (resList.size() == 0) {
                // facets specific implementation
                // register syschronisation only once
                txn.registerSynchronization(this);
            }
            resList.add(conn);
        // xalistThreadLocal.set(resList);
        // Asif :Add in the Map after successful registration of XAResource
        // xaResourcesMap.put(conn, xar);
        // else throw a resource exception
        }
    } catch (RollbackException ex) {
        String exception = LocalizedStrings.FacetsJCAConnectionManagerImpl_FACETSJCACONNECTIONMANAGERIMPL_AN_EXCEPTION_WAS_CAUGHT_WHILE_ALLOCATING_A_CONNECTION_DUE_TO_0.toLocalizedString(ex.getMessage());
        throw new ResourceException(exception, ex);
    } catch (SystemException ex) {
        throw new ResourceException(LocalizedStrings.FacetsJCAConnectionManagerImpl_FACETSJCACONNECTIONMANAGERIMPL_ALLOCATECONNECTION_SYSTEM_EXCEPTION_DUE_TO_0.toLocalizedString(ex.getMessage()), ex);
    }
    return conn.getConnection(subject, reqInfo);
}
Also used : XAResource(javax.transaction.xa.XAResource) List(java.util.List) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) ResourceException(javax.resource.ResourceException) ManagedConnection(javax.resource.spi.ManagedConnection) ArrayList(java.util.ArrayList) List(java.util.List) RollbackException(javax.transaction.RollbackException)

Example 44 with ManagedConnection

use of javax.resource.spi.ManagedConnection in project Payara by payara.

the class ConnectorConnectionPoolAdminServiceImpl method getUnpooledConnection.

/**
 * This method is used to provide backend functionality for the
 * ping-connection-pool asadmin command. Briefly the design is as
 * follows:<br>
 * 1. obtainManagedConnectionFactory for the poolname<br>
 * 2. lookup ConnectorDescriptorInfo from InitialContext using poolname<br>
 * 3. from cdi get username and password<br>
 * 4. create ResourcePrincipal using default username and password<br>
 * 5. create a Subject from this (doPriveleged)<br>
 * 6. createManagedConnection using above subject<br>
 * 7. add a dummy ConnectionEventListener to the mc that simply handles connectionClosed
 * 8. getConnection from the ManagedConnection with above subject<br>
 *
 * @param poolInfo               The poolname from whose MCF to obtain the unpooled mc
 * @param principal                   The ResourcePrincipal to use for authenticating the request if not null.
 *                               If null, the pool's default authentication mechanism is used
 * @param returnConnectionHandle If true will return the logical connection handle
 *                               derived from the Managed Connection, else will only return mc
 * @return an unPooled connection
 * @throws ResourceException for various error conditions
 */
public Object getUnpooledConnection(PoolInfo poolInfo, ResourcePrincipal principal, boolean returnConnectionHandle) throws ResourceException {
    ManagedConnectionFactory mcf = null;
    ResourcePool poolToDeploy = null;
    boolean needToUndeployPool = false;
    ConnectorRuntime runtime = ConnectorRuntime.getRuntime();
    try {
        // START CR 6597868
        if (!isPoolReferredByResource(poolInfo)) {
            if (_registry.isMCFCreated(poolInfo)) {
                unloadAndKillPool(poolInfo);
            }
        }
        // END CR 6597868
        mcf = obtainManagedConnectionFactory(poolInfo, new Hashtable());
    } catch (ConnectorRuntimeException re) {
        logFine("getUnpooledConnection :: obtainManagedConnectionFactory " + "threw exception. So doing checkAndLoadPoolResource");
        if (checkAndLoadPool(poolInfo)) {
            logFine("getUnpooledConnection:: checkAndLoadPoolResource is true");
            try {
                // remote instance, the pool will not have been created
                if (!isConnectorConnectionPoolDeployed(poolInfo)) {
                    logFine("getUnpooledConnection :: isConnectorConnectionPoolDeployed is false");
                    try {
                        poolToDeploy = (ResourcePool) ConnectorsUtil.getResourceByName(runtime.getResources(poolInfo), ResourcePool.class, poolInfo.getName());
                        runtime.getResourceDeployer(poolToDeploy).deployResource(poolToDeploy);
                        logFine("getUnpooledConnection :: force deployed the ConnectionPool : " + poolInfo);
                        needToUndeployPool = true;
                    } catch (Exception e) {
                        _logger.log(Level.SEVERE, "jdbc.could_not_do_actual_deploy for : ", poolInfo);
                        throw new ResourceException(e);
                    }
                }
                logFine("getUnpooledConnection :: Now calling obtainManagedConnectionFactory again");
                mcf = obtainManagedConnectionFactory(poolInfo);
                logFine("getUnpooledConnection:: done obtainManagedConnectionFactory again");
            } catch (ConnectorRuntimeException creAgain) {
                String l10nMsg = localStrings.getString("pingpool.cannot_obtain_mcf", poolInfo);
                _logger.log(Level.WARNING, "jdbc.pool_not_reachable", l10nMsg);
                ResourceException e = new ResourceException(l10nMsg);
                e.initCause(creAgain);
                throw e;
            }
        } else {
            _logger.log(Level.WARNING, "jdbc.pool_not_reachable", re.getMessage());
            String l10nMsg = localStrings.getString("pingpool.cannot_obtain_mcf", poolInfo);
            ResourceException e = new ResourceException(l10nMsg);
            e.initCause(re);
            throw e;
        }
    }
    ResourcePrincipal resourcePrincipal = null;
    if (principal == null) {
        try {
            resourcePrincipal = getDefaultResourcePrincipal(poolInfo, mcf);
        } catch (NamingException ne) {
            _logger.log(Level.WARNING, "jdbc.pool_not_reachable", ne.getMessage());
            String l10nMsg = localStrings.getString("pingpool.name_not_bound", poolInfo);
            ResourceException e = new ResourceException(l10nMsg + poolInfo);
            e.initCause(ne);
            throw e;
        }
    } else {
        resourcePrincipal = principal;
    }
    final Subject defaultSubject = ConnectionPoolObjectsUtils.createSubject(mcf, resourcePrincipal);
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine("using subject: " + defaultSubject);
    }
    // Create the ManagedConnection
    ManagedConnection mc = mcf.createManagedConnection(defaultSubject, null);
    // it here
    if (needToUndeployPool) {
        if (poolToDeploy != null) {
            logFine("getUnpooledConnection :: need to force undeploy pool");
            try {
                runtime.getResourceDeployer(poolToDeploy).undeployResource(poolToDeploy);
            } catch (Exception e) {
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.fine("getUnpooledConnection: error undeploying pool");
                }
            }
            logFine("getUnpooledConnection :: done.. force undeploy of pool");
        }
    }
    // Add our dummy ConnectionEventListener impl.
    // This impl only knows how to handle connectionClosed events
    mc.addConnectionEventListener(new UnpooledConnectionEventListener());
    return returnConnectionHandle ? mc.getConnection(defaultSubject, null) : mc;
}
Also used : ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) ResourcePool(com.sun.enterprise.config.serverbeans.ResourcePool) PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException) Subject(javax.security.auth.Subject) ManagedConnectionFactory(javax.resource.spi.ManagedConnectionFactory) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) ManagedConnection(javax.resource.spi.ManagedConnection) UnpooledConnectionEventListener(com.sun.enterprise.resource.listener.UnpooledConnectionEventListener)

Example 45 with ManagedConnection

use of javax.resource.spi.ManagedConnection in project Payara by payara.

the class ConnectorXAResource method getResourceHandle.

private ResourceHandle getResourceHandle(boolean beginTxIfNeeded) throws PoolingException {
    try {
        ResourceHandle h = null;
        JavaEETransaction j2eetran = getCurrentTransaction();
        if (j2eetran == null) {
            // Only if some thing is wrong with tx manager.
            // Just return the local handle.
            h = localHandle_;
        } else {
            h = (ResourceHandle) j2eetran.getNonXAResource();
            // can be acquired. If the resource in question is not the one in transaction, fail
            if (!localHandle_.isShareable()) {
                if (h != localHandle_) {
                    throw new ResourceAllocationException("Cannot use more than one local-tx resource in unshareable scope");
                }
            }
        }
        if (beginTxIfNeeded && 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);
    }
}
Also used : PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) JavaEETransaction(com.sun.enterprise.transaction.api.JavaEETransaction) ManagedConnection(javax.resource.spi.ManagedConnection) ResourceAllocationException(javax.resource.spi.ResourceAllocationException) PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) SystemException(javax.transaction.SystemException) XAException(javax.transaction.xa.XAException) ResourceAllocationException(javax.resource.spi.ResourceAllocationException)

Aggregations

ManagedConnection (javax.resource.spi.ManagedConnection)70 Test (org.junit.Test)24 ResourceException (javax.resource.ResourceException)20 XAResource (javax.transaction.xa.XAResource)17 Iterator (java.util.Iterator)15 PoolingException (com.sun.appserv.connectors.internal.api.PoolingException)14 SystemException (javax.transaction.SystemException)12 Subject (javax.security.auth.Subject)10 Xid (javax.transaction.xa.Xid)9 HashSet (java.util.HashSet)8 Transaction (javax.transaction.Transaction)7 ManagedConnectionFactory (javax.resource.spi.ManagedConnectionFactory)6 ResourceAllocationException (javax.resource.spi.ResourceAllocationException)6 RollbackException (javax.transaction.RollbackException)6 XAException (javax.transaction.xa.XAException)6 Connection (java.sql.Connection)5 NamingException (javax.naming.NamingException)5 ConnectionRequestInfo (javax.resource.spi.ConnectionRequestInfo)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4