use of com.sun.enterprise.resource.listener.UnpooledConnectionEventListener 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;
}
Aggregations