Example usage for javax.sql XADataSource getXAConnection

List of usage examples for javax.sql XADataSource getXAConnection

Introduction

In this page you can find the example usage for javax.sql XADataSource getXAConnection.

Prototype

XAConnection getXAConnection() throws SQLException;

Source Link

Document

Attempts to establish a physical database connection that can be used in a distributed transaction.

Usage

From source file:org.bytesoft.bytejta.supports.serialize.XAResourceDeserializerImpl.java

private XAResource deserializeResource(String identifier, Object bean) throws Exception {
    if (DataSourceHolder.class.isInstance(bean)) {
        DataSourceHolder holder = (DataSourceHolder) bean;
        RecoveredResource xares = new RecoveredResource();
        xares.setDataSource(holder.getDataSource());
        return xares;
    } else if (javax.sql.DataSource.class.isInstance(bean)) {
        javax.sql.DataSource dataSource = (javax.sql.DataSource) bean;
        RecoveredResource xares = new RecoveredResource();
        xares.setDataSource(dataSource);
        return xares;
    } else if (XADataSource.class.isInstance(bean)) {
        XADataSource xaDataSource = (XADataSource) bean;
        XAConnection xaConnection = null;
        try {/*www. jav a2s.com*/
            xaConnection = xaDataSource.getXAConnection();
            return xaConnection.getXAResource();
        } finally {
            this.closeQuietly(xaConnection);
        }
    } else if (XAConnectionFactory.class.isInstance(bean)) {
        XAConnectionFactory connectionFactory = (XAConnectionFactory) bean;
        javax.jms.XAConnection xaConnection = null;
        XASession xaSession = null;
        try {
            xaConnection = connectionFactory.createXAConnection();
            xaSession = xaConnection.createXASession();
            return xaSession.getXAResource();
        } finally {
            this.closeQuietly(xaSession);
            this.closeQuietly(xaConnection);
        }
    } else if (ManagedConnectionFactory.class.isInstance(bean)) {
        ManagedConnectionFactory connectionFactory = (ManagedConnectionFactory) bean;
        ManagedConnection managedConnection = null;
        try {
            managedConnection = connectionFactory.createManagedConnection(null, null);
            return managedConnection.getXAResource();
        } finally {
            this.closeQuietly(managedConnection);
        }
    } else {
        return null;
    }

}

From source file:org.nuxeo.common.utils.JDBCUtils.java

/**
 * Tries to acquire a {@link XAConnection} through a {@link XADataSource} even when the server is overloaded.
 *
 * @param xaDataSource the XA data source
 * @return a XA connection to the XA data source
 *//*from w  w  w  .  j  av  a2  s.co  m*/
public static XAConnection getXAConnection(final XADataSource xaDataSource) throws SQLException {
    return callWithRetry(new Callable<XAConnection>() {

        @Override
        public XAConnection call() throws SQLException {
            return xaDataSource.getXAConnection();
        }
    });
}

From source file:org.ops4j.pax.jdbc.pool.narayana.impl.DbcpXAPooledDataSourceFactory.java

@Override
public DataSource create(DataSourceFactory dsf, Properties props) throws SQLException {
    try {/* w  w w . j av  a  2s  .c o  m*/
        final XADataSource ds = dsf.createXADataSource(getNonPoolProps(props));
        DataSourceXAConnectionFactory connFactory = new DataSourceXAConnectionFactory(tm, (XADataSource) ds);
        PoolableManagedConnectionFactory pcf = new PoolableManagedConnectionFactory(connFactory, null);
        GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
        BeanConfig.configure(conf, getPoolProps(props));
        BeanConfig.configure(pcf, getPrefixed(props, FACTORY_PREFIX));
        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf, conf);
        pcf.setPool(pool);
        TransactionRegistry transactionRegistry = connFactory.getTransactionRegistry();
        final ServiceRegistration<XAResourceRecovery> registration = bundleContext
                .registerService(XAResourceRecovery.class, new XAResourceRecovery() {
                    @Override
                    public XAResource[] getXAResources() {
                        try {
                            return new XAResource[] { new Wrapper(ds.getXAConnection()) };
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }, null);
        ManagedDataSource<PoolableConnection> mds = new ManagedDataSource<PoolableConnection>(pool,
                transactionRegistry) {
            @Override
            public void close() throws Exception {
                registration.unregister();
                super.close();
            }
        };
        return mds;
    } catch (Throwable e) {
        LOG.error("Error creating pooled datasource" + e.getMessage(), e);
        if (e instanceof SQLException) {
            throw (SQLException) e;
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

From source file:org.ops4j.pax.jdbc.pool.narayana.impl.ds.DbcpXAPooledDataSourceFactory.java

@Override
public DataSource createDataSource(Properties props) throws SQLException {
    try {//  www .j  a  va2 s  . c  o  m
        final XADataSource ds = dsFactory.createXADataSource(getNonPoolProps(props));
        DataSourceXAConnectionFactory connFactory = new DataSourceXAConnectionFactory(tm, (XADataSource) ds);
        PoolableManagedConnectionFactory pcf = new PoolableManagedConnectionFactory(connFactory, null);
        GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
        BeanConfig.configure(conf, getPoolProps(props));
        BeanConfig.configure(pcf, getPrefixed(props, FACTORY_PREFIX));
        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf, conf);
        pcf.setPool(pool);
        TransactionRegistry transactionRegistry = connFactory.getTransactionRegistry();
        final ServiceRegistration<XAResourceRecovery> registration = bundleContext
                .registerService(XAResourceRecovery.class, new XAResourceRecovery() {
                    @Override
                    public XAResource[] getXAResources() {
                        try {
                            return new XAResource[] { new Wrapper(ds.getXAConnection()) };
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }, null);
        ManagedDataSource mds = new ManagedDataSource<PoolableConnection>(pool, transactionRegistry) {
            @Override
            public void close() throws Exception {
                registration.unregister();
                super.close();
            }
        };
        return mds;
    } catch (Throwable e) {
        LOG.error("Error creating pooled datasource" + e.getMessage(), e);
        if (e instanceof SQLException) {
            throw (SQLException) e;
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}