Search in sources :

Example 51 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 52 with ManagedConnection

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

the class NoTxConnectorAllocator method destroyResource.

public void destroyResource(ResourceHandle resource) throws PoolingException {
    try {
        ManagedConnection mc = (ManagedConnection) resource.getResource();
        mc.destroy();
    } catch (Exception ex) {
        throw new PoolingException(ex);
    }
}
Also used : PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) ManagedConnection(javax.resource.spi.ManagedConnection) PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) ResourceException(javax.resource.ResourceException)

Example 53 with ManagedConnection

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

the class NoTxConnectorAllocator method fillInResourceObjects.

public void fillInResourceObjects(ResourceHandle resource) throws PoolingException {
    try {
        ManagedConnection mc = (ManagedConnection) resource.getResource();
        Object con = mc.getConnection(subject, reqInfo);
        resource.fillInResourceObjects(con, null);
    } catch (ResourceException ex) {
        throw new PoolingException(ex);
    }
}
Also used : PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) ManagedConnection(javax.resource.spi.ManagedConnection) ResourceException(javax.resource.ResourceException)

Example 54 with ManagedConnection

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

the class LocalTxConnectionEventListener method badConnectionClosed.

/**
 * Resource adapters will signal that the connection being closed is bad.
 *
 * @param evt ConnectionEvent
 */
@Override
public void badConnectionClosed(ConnectionEvent evt) {
    Object connectionHandle = evt.getConnectionHandle();
    synchronized (this) {
        ResourceHandle handle = getResourceHandle(connectionHandle);
        if (handle != null) {
            poolMgr.badResourceClosed(handle);
            ManagedConnection mc = (ManagedConnection) evt.getSource();
            mc.removeConnectionEventListener(this);
        }
    }
}
Also used : ResourceHandle(com.sun.enterprise.resource.ResourceHandle) ManagedConnection(javax.resource.spi.ManagedConnection)

Example 55 with ManagedConnection

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

the class LocalTxConnectionEventListener method connectionErrorOccurred.

@Override
public synchronized void connectionErrorOccurred(ConnectionEvent evt) {
    resource.setConnectionErrorOccurred();
    ManagedConnection mc = (ManagedConnection) evt.getSource();
    mc.removeConnectionEventListener(this);
    poolMgr.resourceErrorOccurred(resource);
}
Also used : ManagedConnection(javax.resource.spi.ManagedConnection)

Aggregations

ManagedConnection (javax.resource.spi.ManagedConnection)73 Test (org.junit.Test)24 ResourceException (javax.resource.ResourceException)20 XAResource (javax.transaction.xa.XAResource)17 Iterator (java.util.Iterator)16 PoolingException (com.sun.appserv.connectors.internal.api.PoolingException)13 SystemException (javax.transaction.SystemException)11 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 RollbackException (javax.transaction.RollbackException)6 ResourceHandle (com.sun.enterprise.resource.ResourceHandle)5 Connection (java.sql.Connection)5 NamingException (javax.naming.NamingException)5 ConnectionRequestInfo (javax.resource.spi.ConnectionRequestInfo)5 ResourceAllocationException (javax.resource.spi.ResourceAllocationException)5 XAException (javax.transaction.xa.XAException)5 ArrayList (java.util.ArrayList)4