use of javax.resource.ResourceException 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);
}
use of javax.resource.ResourceException in project tomee by apache.
the class ActiveMQResourceAdapter method endpointActivation.
@Override
public void endpointActivation(final MessageEndpointFactory endpointFactory, final ActivationSpec activationSpec) throws ResourceException {
final BeanContext current = MdbContainer.current();
if (current != null && "false".equalsIgnoreCase(current.getProperties().getProperty("MdbActiveOnStartup"))) {
if (!equals(activationSpec.getResourceAdapter())) {
throw new ResourceException("Activation spec not initialized with this ResourceAdapter instance (" + activationSpec.getResourceAdapter() + " != " + this + ")");
}
if (!(activationSpec instanceof MessageActivationSpec)) {
throw new NotSupportedException("That type of ActivationSpec not supported: " + activationSpec.getClass());
}
final ActiveMQEndpointActivationKey key = new ActiveMQEndpointActivationKey(endpointFactory, MessageActivationSpec.class.cast(activationSpec));
Map.class.cast(Reflections.get(this, "endpointWorkers")).put(key, new ActiveMQEndpointWorker(this, key) {
});
// we dont want that worker.start();
} else {
super.endpointActivation(endpointFactory, activationSpec);
}
if (current != null) {
addJMxControl(current, current.getProperties().getProperty("MdbJMXControl"));
}
}
use of javax.resource.ResourceException in project Payara by payara.
the class ConnectionManagerImpl method validateResourceAndPool.
private void validateResourceAndPool() throws ResourceException {
ResourceInfo resourceInfo = this.resourceInfo;
ResourcesUtil resourcesUtil = ResourcesUtil.createInstance();
ConnectorRuntime runtime = ConnectorRuntime.getRuntime();
ConnectorRegistry registry = ConnectorRegistry.getInstance();
// are disabled.
if (!registry.isResourceDeployed(resourceInfo)) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "resourceInfo not found in connector-registry : " + resourceInfo);
}
boolean isDefaultResource = false;
boolean isSunRAResource = false;
ConnectorDescriptor descriptor = registry.getDescriptor(rarName);
if (descriptor != null) {
isDefaultResource = descriptor.getDefaultResourcesNames().contains(resourceInfo.getName());
if (descriptor.getSunDescriptor() != null) {
com.sun.enterprise.deployment.runtime.connector.ResourceAdapter rar = descriptor.getSunDescriptor().getResourceAdapter();
if (rar != null) {
String sunRAJndiName = (String) rar.getValue(com.sun.enterprise.deployment.runtime.connector.ResourceAdapter.JNDI_NAME);
isSunRAResource = resourceInfo.getName().equals(sunRAJndiName);
}
}
}
if ((runtime.isServer() || runtime.isEmbedded()) && (!resourceInfo.getName().contains(ConnectorConstants.DATASOURCE_DEFINITION_JNDINAME_PREFIX) && (!isDefaultResource) && (!isSunRAResource))) {
// resources config bean each time.
if (resourceConfiguration == null) {
resourceConfiguration = (BindableResource) resourcesUtil.getResource(resourceInfo, BindableResource.class);
if (resourceConfiguration == null) {
String suffix = ConnectorsUtil.getValidSuffix(resourceInfo.getName());
// check for the enabled status and existence using non-prefixed resource-name
if (suffix != null) {
String nonPrefixedName = resourceInfo.getName().substring(0, resourceInfo.getName().lastIndexOf(suffix));
resourceInfo = new ResourceInfo(nonPrefixedName, resourceInfo.getApplicationName(), resourceInfo.getModuleName());
resourceConfiguration = (BindableResource) resourcesUtil.getResource(resourceInfo, BindableResource.class);
}
}
} else {
// we cache the resourceConfiguration for performance optimization.
// make sure that appropriate (actual) resourceInfo is used for validation.
String suffix = ConnectorsUtil.getValidSuffix(resourceInfo.getName());
// check for the enabled status and existence using non-prefixed resource-name
if (suffix != null) {
String nonPrefixedName = resourceInfo.getName().substring(0, resourceInfo.getName().lastIndexOf(suffix));
resourceInfo = new ResourceInfo(nonPrefixedName, resourceInfo.getApplicationName(), resourceInfo.getModuleName());
}
}
if (resourceConfiguration == null) {
throw new ResourceException("No such resource : " + resourceInfo);
}
if (!resourcesUtil.isEnabled(resourceConfiguration, resourceInfo)) {
throw new ResourceException(resourceInfo + " is not enabled");
}
}
}
if (registry.getPoolMetaData(poolInfo) == null) {
String msg = getLocalStrings().getString("con_mgr.no_pool_meta_data", poolInfo);
throw new ResourceException(poolInfo + ": " + msg);
}
}
use of javax.resource.ResourceException in project Payara by payara.
the class ConnectorConnectionPoolAdminServiceImpl method getConnection.
/**
* Get a sql connection from the DataSource specified by the jdbcJndiName.
* This API is intended to be used in the DAS. The motivation for having this
* API is to provide the CMP backend a means of acquiring a connection during
* the codegen phase. If a user is trying to deploy an app on a remote server,
* without this API, a resource reference has to be present both in the DAS
* and the server instance. This makes the deployment more complex for the
* user since a resource needs to be forcibly created in the DAS Too.
* This API will mitigate this need.
*
* @param resourceInfo the jndi name of the resource being used to get Connection from
* This resource can either be a pmf resource or a jdbc resource
* @param user the user used to authenticate this request
* @param password the password used to authenticate this request
* @return a java.sql.Connection
* @throws java.sql.SQLException in case of errors
*/
public Connection getConnection(ResourceInfo resourceInfo, String user, String password) throws SQLException {
java.sql.Connection con = null;
try {
// DASResourcesUtil.setAdminConfigContext();
PoolInfo poolInfo = getPoolNameFromResourceJndiName(resourceInfo);
if (poolInfo == null) {
throw new SQLException("No pool by name exists ");
}
if (_logger.isLoggable(Level.FINE)) {
_logger.fine("ConnectorRuntime.getConnection :: poolName : " + poolInfo);
}
// Maintain consitency with the ConnectionManagerImpl change to be checked in later
String passwd = (password == null) ? "" : password;
// From what we have seen so far, the user cannot be null
// but password can be
// if user is null we will use default authentication
// TODO: Discuss if this is the right thing to do
ResourcePrincipal prin = (user == null) ? null : new ResourcePrincipal(user, passwd);
con = (java.sql.Connection) getUnpooledConnection(poolInfo, prin, true);
if (con == null) {
String i18nMsg = localStrings.getString("ccp_adm.null_unpooled_connection");
throw new SQLException(i18nMsg);
}
} catch (ResourceException re) {
SQLException sqle = new SQLException(re.getMessage());
sqle.initCause(re);
_logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
if (_logger.isLoggable(Level.FINE)) {
_logger.fine(" getConnection in ConnectorRuntime failed : " + re);
}
throw sqle;
} catch (Exception ex) {
SQLException sqle = new SQLException(ex.getMessage());
sqle.initCause(ex);
_logger.log(Level.WARNING, "jdbc.exc_get_conn", ex.getMessage());
if (_logger.isLoggable(Level.FINE)) {
_logger.fine(" getConnection in ConnectorRuntime failed : " + ex);
}
throw sqle;
}
return con;
}
use of javax.resource.ResourceException 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