Search in sources :

Example 16 with XAResource

use of javax.transaction.xa.XAResource in project geode by apache.

the class JCAConnectionManagerImpl 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.JCAConnectionManagerImpl_JCACONNECTIONMANAGERIMPLALLOCATECONNECTIONNO_VALID_CONNECTION_AVAILABLE.toLocalizedString());
    }
    ManagedConnection conn = null;
    try {
        conn = (ManagedConnection) mannPoolCache.getPooledConnectionFromPool();
    } catch (PoolException ex) {
        // ex.printStackTrace();
        throw new ResourceException(LocalizedStrings.JCAConnectionManagerImpl_JCACONNECTIONMANAGERIMPL_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);
            // Asif :Add in the Map after successful registration of XAResource
            xaResourcesMap.put(conn, xar);
        // else throw a resource exception
        }
    } catch (RollbackException ex) {
        throw new ResourceException(LocalizedStrings.JCAConnectionManagerImpl_JCACONNECTIONMANAGERIMPL_ALLOCATECONNECTION_IN_TRANSACTION_DUE_TO_0.toLocalizedString(ex.getMessage()), ex);
    } catch (SystemException ex) {
        throw new ResourceException(LocalizedStrings.JCAConnectionManagerImpl_JCACONNECTIONMANAGERIMPL_ALLOCATECONNECTION_SYSTEM_EXCEPTION_DUE_TO_0.toLocalizedString(ex.getMessage()), ex);
    }
    return conn.getConnection(subject, reqInfo);
}
Also used : XAResource(javax.transaction.xa.XAResource) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) ResourceException(javax.resource.ResourceException) ManagedConnection(javax.resource.spi.ManagedConnection) RollbackException(javax.transaction.RollbackException)

Example 17 with XAResource

use of javax.transaction.xa.XAResource in project Payara by payara.

the class LegacyJmsRecoveryResourceHandler method recoverJMSXAResource.

// -------------------------
/* private void recoverJMSXAResources(Context ic,
    Vector xaresList, Vector connList, Set jmsRes) {
        for(Iterator iter = jmsRes.iterator(); iter.hasNext();) {
            J2EEResource next = (J2EEResource)iter.next();
            if (next instanceof ExternalJndiResource) {
                if (!((ExternalJndiResource)next).isJMSConnectionFactory()) {
                    continue;
                }
            }

            String jndiName = next.getName();
            try {
                Object jmsXAConnectionFactory;
                boolean isQueue;
                if (next instanceof ExternalJndiResource) {
                    Object objext = ic.lookup(jndiName);
                    if (!(objext instanceof javax.jms.ConnectionFactory)) {
                        throw new NamingException(localStrings.getLocalString("recovery.unexpected_objtype",
                        "Unexpected object type "+objext.getClass().getName()+" for "+ jndiName,
                        new Object[]{objext.getClass().getName(), jndiName}));
                    }
                    jmsXAConnectionFactory = wrapJMSConnectionFactoryObject(objext);
                    isQueue = (objext instanceof javax.jms.QueueConnectionFactory);
                }
                else {
                    jmsXAConnectionFactory = ic.lookup(getXAConnectionFactoryName(jndiName));
                    isQueue = (jmsXAConnectionFactory instanceof JMSXAQueueConnectionFactory);
                }
                recoverJMSXAResource(xaresList, connList, jmsXAConnectionFactory, isQueue);
            } catch (Exception ex) {
                _logger.log(Level.SEVERE,"datasource.xadatasource_error",jndiName);
                _logger.log(Level.SEVERE,"datasource.xadatasource_error_excp",ex);
            }
        }
    }  */
private void recoverJMSXAResource(List xaresList, List connList, Object jmsXAConnectionFactory, boolean isQueue) throws Exception {
    if (isQueue) {
        // JMSXAQueueConnectionFactory fac =
        // (JMSXAQueueConnectionFactory) obj;
        Object jmsXAQueueConnection = getXAConnection(jmsXAConnectionFactory, "createXAQueueConnection");
        connList.add(jmsXAQueueConnection);
        // con.createXAQueueSession
        XAResource xares = getXAResource(jmsXAQueueConnection, "createXAQueueSession");
        // (true, Session.AUTO_ACKNOWLEDGE).getXAResource();
        xaresList.add(xares);
    } else {
        // XATopicConnectionFactory
        // JMSXATopicConnectionFactory fac =
        // (JMSXATopicConnectionFactory) obj;
        Object jmsXATopicConnection = getXAConnection(jmsXAConnectionFactory, "createXATopicConnection");
        connList.add(jmsXATopicConnection);
        XAResource xares = getXAResource(jmsXATopicConnection, "createXATopicSession");
        // XAResource xares = con.createXATopicSession
        // (true, Session.AUTO_ACKNOWLEDGE).getXAResource();
        xaresList.add(xares);
    }
}
Also used : XAResource(javax.transaction.xa.XAResource)

Example 18 with XAResource

use of javax.transaction.xa.XAResource in project Payara by payara.

the class LegacyJmsRecoveryResourceHandler method getXAResource.

private XAResource getXAResource(Object XAConnection, String methodName) {
    try {
        Class connectionClass = XAConnection.getClass();
        Class[] paramtypes = { boolean.class, int.class };
        Method m = connectionClass.getMethod(methodName, paramtypes);
        // Session.AUTO_ACKNOWLEDGE});
        Object jmsXASession = m.invoke(XAConnection, new Object[] { true, 1 });
        Class xaSessionClass = jmsXASession.getClass();
        Method m1 = xaSessionClass.getMethod("getXAResource", null);
        return (XAResource) m1.invoke(jmsXASession, null);
    } catch (Exception e) {
    // todo: need to handle this better
    }
    return null;
}
Also used : XAResource(javax.transaction.xa.XAResource) Method(java.lang.reflect.Method) NamingException(javax.naming.NamingException)

Example 19 with XAResource

use of javax.transaction.xa.XAResource in project Payara by payara.

the class JdbcRecoveryResourceHandler method loadXAResourcesAndItsConnections.

/**
 * {@inheritDoc}
 */
@Override
public void loadXAResourcesAndItsConnections(List xaresList, List connList) {
    // Done so as to initialize connectors-runtime before loading jdbc-resources. need a better way ?
    ConnectorRuntime crt = connectorRuntimeProvider.get();
    Collection<JdbcResource> jdbcResources = getAllJdbcResources();
    if (jdbcResources == null || jdbcResources.size() == 0) {
        if (_logger.isLoggable(Level.FINEST)) {
            _logger.finest("loadXAResourcesAndItsConnections : no resources");
        }
        return;
    }
    List<JdbcConnectionPool> jdbcPools = new ArrayList<JdbcConnectionPool>();
    for (Resource resource : jdbcResources) {
        JdbcResource jdbcResource = (JdbcResource) resource;
        if (getResourcesUtil().isEnabled(jdbcResource)) {
            ResourceInfo resourceInfo = ConnectorsUtil.getResourceInfo(jdbcResource);
            JdbcConnectionPool pool = JdbcResourcesUtil.createInstance().getJdbcConnectionPoolOfResource(resourceInfo);
            if (pool != null && "javax.sql.XADataSource".equals(pool.getResType())) {
                jdbcPools.add(pool);
            }
            if (_logger.isLoggable(Level.FINE)) {
                _logger.fine("JdbcRecoveryResourceHandler:: loadXAResourcesAndItsConnections :: " + "adding : " + (jdbcResource.getPoolName()));
            }
        }
    }
    loadAllJdbcResources();
    // Read from the transaction-service , if the replacement of
    // Vendor XAResource class with our version required.
    // If yes, put the mapping in the xaresourcewrappers properties.
    Properties XAResourceWrappers = new Properties();
    XAResourceWrappers.put("oracle.jdbc.xa.client.OracleXADataSource", "com.sun.enterprise.transaction.jts.recovery.OracleXAResource");
    Config c = habitat.getService(Config.class, ServerEnvironment.DEFAULT_INSTANCE_NAME);
    txService = c.getExtensionByType(TransactionService.class);
    List<Property> properties = txService.getProperty();
    if (properties != null) {
        for (Property property : properties) {
            String name = property.getName();
            String value = property.getValue();
            if (name.equals("oracle-xa-recovery-workaround")) {
                if ("false".equals(value)) {
                    XAResourceWrappers.remove("oracle.jdbc.xa.client.OracleXADataSource");
                }
            } else if (name.equals("sybase-xa-recovery-workaround")) {
                if (value.equals("true")) {
                    XAResourceWrappers.put("com.sybase.jdbc2.jdbc.SybXADataSource", "com.sun.enterprise.transaction.jts.recovery.SybaseXAResource");
                }
            }
        }
    }
    for (JdbcConnectionPool jdbcConnectionPool : jdbcPools) {
        if (jdbcConnectionPool.getResType() == null || jdbcConnectionPool.getName() == null || !jdbcConnectionPool.getResType().equals("javax.sql.XADataSource")) {
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.finest("skipping pool : " + jdbcConnectionPool.getName());
            }
            continue;
        }
        if (_logger.isLoggable(Level.FINEST)) {
            _logger.finest(" using pool : " + jdbcConnectionPool.getName());
        }
        PoolInfo poolInfo = ConnectorsUtil.getPoolInfo(jdbcConnectionPool);
        try {
            String[] dbUserPassword = getdbUserPasswordOfJdbcConnectionPool(jdbcConnectionPool);
            String dbUser = dbUserPassword[0];
            String dbPassword = dbUserPassword[1];
            if (dbPassword == null) {
                dbPassword = "";
                if (_logger.isLoggable(Level.FINEST)) {
                    _logger.log(Level.FINEST, "datasource.xadatasource_nullpassword_error", poolInfo);
                }
            }
            if (dbUser == null) {
                dbUser = "";
                if (_logger.isLoggable(Level.FINEST)) {
                    _logger.log(Level.FINEST, "datasource.xadatasource_nulluser_error", poolInfo);
                }
            }
            ManagedConnectionFactory fac = crt.obtainManagedConnectionFactory(poolInfo);
            Subject subject = new Subject();
            PasswordCredential pc = new PasswordCredential(dbUser, dbPassword.toCharArray());
            pc.setManagedConnectionFactory(fac);
            Principal prin = new ResourcePrincipal(dbUser, dbPassword);
            subject.getPrincipals().add(prin);
            subject.getPrivateCredentials().add(pc);
            ManagedConnection mc = fac.createManagedConnection(subject, null);
            connList.add(mc);
            try {
                XAResource xares = mc.getXAResource();
                if (xares != null) {
                    // See if a wrapper class for the vendor XADataSource is
                    // specified if yes, replace the XAResouce class of database
                    // vendor with our own version
                    String datasourceClassname = jdbcConnectionPool.getDatasourceClassname();
                    String wrapperclass = (String) XAResourceWrappers.get(datasourceClassname);
                    if (wrapperclass != null) {
                        // need to load wrapper class provided by "transactions" module.
                        // Using connector-class-loader so as to get access to "transaction" module.
                        XAResourceWrapper xaresWrapper = null;
                        xaresWrapper = (XAResourceWrapper) crt.getConnectorClassLoader().loadClass(wrapperclass).newInstance();
                        xaresWrapper.init(mc, subject);
                        if (_logger.isLoggable(Level.FINEST)) {
                            _logger.finest("adding resource " + poolInfo + " -- " + xaresWrapper);
                        }
                        xaresList.add(xaresWrapper);
                    } else {
                        if (_logger.isLoggable(Level.FINEST)) {
                            _logger.finest("adding resource " + poolInfo + " -- " + xares);
                        }
                        xaresList.add(xares);
                    }
                }
            } catch (ResourceException ex) {
                _logger.log(Level.WARNING, "datasource.xadatasource_error", poolInfo);
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.log(Level.FINE, "datasource.xadatasource_error_excp", ex);
                }
            // ignored. Not at XA_TRANSACTION level
            }
        } catch (Exception ex) {
            _logger.log(Level.WARNING, "datasource.xadatasource_error", poolInfo);
            if (_logger.isLoggable(Level.FINE)) {
                _logger.log(Level.FINE, "datasource.xadatasource_error_excp", ex);
            }
        }
    }
}
Also used : JdbcResource(org.glassfish.jdbc.config.JdbcResource) Config(com.sun.enterprise.config.serverbeans.Config) ArrayList(java.util.ArrayList) PasswordCredential(javax.resource.spi.security.PasswordCredential) Properties(java.util.Properties) XAResourceWrapper(com.sun.enterprise.transaction.api.XAResourceWrapper) ManagedConnection(javax.resource.spi.ManagedConnection) ResourceException(javax.resource.ResourceException) Property(org.jvnet.hk2.config.types.Property) ResourceInfo(org.glassfish.resourcebase.resources.api.ResourceInfo) JdbcConnectionPool(org.glassfish.jdbc.config.JdbcConnectionPool) TransactionService(com.sun.enterprise.transaction.config.TransactionService) XAResource(javax.transaction.xa.XAResource) Resource(com.sun.enterprise.config.serverbeans.Resource) JdbcResource(org.glassfish.jdbc.config.JdbcResource) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) Subject(javax.security.auth.Subject) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) ManagedConnectionFactory(javax.resource.spi.ManagedConnectionFactory) XAResource(javax.transaction.xa.XAResource) PoolInfo(org.glassfish.resourcebase.resources.api.PoolInfo) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) Principal(java.security.Principal) ConnectorRuntime(com.sun.appserv.connectors.internal.api.ConnectorRuntime)

Example 20 with XAResource

use of javax.transaction.xa.XAResource in project Payara by payara.

the class ConnectorsRecoveryResourceHandler method loadXAResourcesAndItsConnections.

/**
 * {@inheritDoc}
 */
public void loadXAResourcesAndItsConnections(List xaresList, List connList) {
    // Done so as to initialize connectors-runtime before loading connector-resources. need a better way ?
    ConnectorRuntime crt = connectorRuntimeProvider.get();
    // ApplicationLoaderService already guarantees that connectors are loaded before any other applications.
    // Recovery will not start sooner than transaction is first needed on EE application startup, therefore
    // it is safe to continue here without waiting for startupProvider, as that ultimately creates a deadlock
    // between ApplicationLoaderService needed a transaction manager, and recovery wainting for applications
    // to finish loading.
    Collection<ConnectorResource> connectorResources = getAllConnectorResources();
    if (connectorResources == null || connectorResources.size() == 0) {
        return;
    }
    List<ConnectorConnectionPool> connPools = new ArrayList<ConnectorConnectionPool>();
    for (Resource resource : connectorResources) {
        ConnectorResource connResource = (ConnectorResource) resource;
        if (getResourcesUtil().isEnabled(connResource)) {
            ResourceInfo resourceInfo = ConnectorsUtil.getResourceInfo(connResource);
            ConnectorConnectionPool pool = ResourcesUtil.createInstance().getConnectorConnectionPoolOfResource(resourceInfo);
            if (pool != null && ConnectorConstants.XA_TRANSACTION_TX_SUPPORT_STRING.equals(getTransactionSupport(pool))) {
                connPools.add(pool);
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.fine("ConnectorsRecoveryResourceHandler loadXAResourcesAndItsConnections :: " + "adding : " + connResource.getPoolName());
                }
            }
        }
    }
    loadAllConnectorResources();
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "Recovering pools : " + connPools.size());
    }
    for (ConnectorConnectionPool connPool : connPools) {
        PoolInfo poolInfo = ConnectorsUtil.getPoolInfo(connPool);
        try {
            String[] dbUserPassword = getdbUserPasswordOfConnectorConnectionPool(connPool);
            if (dbUserPassword == null) {
                continue;
            }
            String dbUser = dbUserPassword[0];
            String dbPassword = dbUserPassword[1];
            Subject subject = new Subject();
            // username [pointbase interprets this as "root"]/password.
            if (dbPassword == null) {
                dbPassword = "";
                if (_logger.isLoggable(Level.FINEST)) {
                    _logger.log(Level.FINEST, "datasource.xadatasource_nullpassword_error", poolInfo);
                }
            }
            if (dbUser == null) {
                dbUser = "";
                if (_logger.isLoggable(Level.FINEST)) {
                    _logger.log(Level.FINEST, "datasource.xadatasource_nulluser_error", poolInfo);
                }
            }
            String rarName = connPool.getResourceAdapterName();
            // TODO V3 JMS-RA ??
            if (ConnectorAdminServiceUtils.isJMSRA(rarName)) {
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.log(Level.FINE, "Performing recovery for JMS RA, poolName  " + poolInfo);
                }
                ManagedConnectionFactory[] mcfs = crt.obtainManagedConnectionFactories(poolInfo);
                _logger.log(Level.INFO, "JMS resource recovery has created CFs = " + mcfs.length);
                for (int i = 0; i < mcfs.length; i++) {
                    PasswordCredential pc = new PasswordCredential(dbUser, dbPassword.toCharArray());
                    pc.setManagedConnectionFactory(mcfs[i]);
                    Principal prin = new ResourcePrincipal(dbUser, dbPassword);
                    subject.getPrincipals().add(prin);
                    subject.getPrivateCredentials().add(pc);
                    ManagedConnection mc = mcfs[i].createManagedConnection(subject, null);
                    connList.add(mc);
                    try {
                        XAResource xares = mc.getXAResource();
                        if (xares != null) {
                            xaresList.add(xares);
                        }
                    } catch (ResourceException ex) {
                    // ignored. Not at XA_TRANSACTION level
                    }
                }
            } else {
                ManagedConnectionFactory mcf = crt.obtainManagedConnectionFactory(poolInfo);
                PasswordCredential pc = new PasswordCredential(dbUser, dbPassword.toCharArray());
                pc.setManagedConnectionFactory(mcf);
                Principal prin = new ResourcePrincipal(dbUser, dbPassword);
                subject.getPrincipals().add(prin);
                subject.getPrivateCredentials().add(pc);
                ManagedConnection mc = mcf.createManagedConnection(subject, null);
                connList.add(mc);
                try {
                    XAResource xares = mc.getXAResource();
                    if (xares != null) {
                        xaresList.add(xares);
                    }
                } catch (ResourceException ex) {
                // ignored. Not at XA_TRANSACTION level
                }
            }
        } catch (Exception ex) {
            _logger.log(Level.WARNING, "datasource.xadatasource_error", poolInfo);
            if (_logger.isLoggable(Level.FINE)) {
                _logger.log(Level.FINE, "datasource.xadatasource_error_excp", ex);
            }
        }
    }
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "Total XAResources identified for recovery is " + xaresList.size());
        _logger.log(Level.FINE, "Total connections identified for recovery is " + connList.size());
    }
}
Also used : ResourceInfo(org.glassfish.resourcebase.resources.api.ResourceInfo) ConnectorConnectionPool(org.glassfish.connectors.config.ConnectorConnectionPool) XAResource(javax.transaction.xa.XAResource) ConnectorResource(org.glassfish.connectors.config.ConnectorResource) PasswordCredential(javax.resource.spi.security.PasswordCredential) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) Subject(javax.security.auth.Subject) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException) ManagedConnectionFactory(javax.resource.spi.ManagedConnectionFactory) XAResource(javax.transaction.xa.XAResource) PoolInfo(org.glassfish.resourcebase.resources.api.PoolInfo) ManagedConnection(javax.resource.spi.ManagedConnection) ResourceException(javax.resource.ResourceException) ConnectorResource(org.glassfish.connectors.config.ConnectorResource) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) Principal(java.security.Principal) ConnectorRuntime(com.sun.enterprise.connectors.ConnectorRuntime)

Aggregations

XAResource (javax.transaction.xa.XAResource)49 Xid (javax.transaction.xa.Xid)14 Test (org.junit.Test)13 SystemException (javax.transaction.SystemException)12 Transaction (javax.transaction.Transaction)11 ResourceException (javax.resource.ResourceException)9 XAException (javax.transaction.xa.XAException)9 XidImpl (org.neo4j.kernel.impl.transaction.XidImpl)9 RollbackException (javax.transaction.RollbackException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 ManagedConnection (javax.resource.spi.ManagedConnection)6 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)6 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)6 SQLException (java.sql.SQLException)5 RelationshipType (org.neo4j.graphdb.RelationshipType)5 Map (java.util.Map)4 XAConnection (javax.sql.XAConnection)4 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)3 PoolingException (com.sun.appserv.connectors.internal.api.PoolingException)3