Example usage for org.apache.commons.dbcp.managed TransactionContext getSharedConnection

List of usage examples for org.apache.commons.dbcp.managed TransactionContext getSharedConnection

Introduction

In this page you can find the example usage for org.apache.commons.dbcp.managed TransactionContext getSharedConnection.

Prototype

public Connection getSharedConnection() 

Source Link

Document

Gets the connection shared by all ManagedConnections in the transaction.

Usage

From source file:org.ofbiz.tenant.jdbc.TenantJdbcConnectionHandler.java

/**
 * delete database/* w ww  .  j av  a2s .  c om*/
 * @return
 */
public void deleteDatabase() throws GenericEntityException, SQLException {
    Delegator delegator = tenantDataSource.getDelegator();
    GenericHelperInfo helperInfo = delegator.getGroupHelperInfo(this.getEntityGroupName());
    helperInfo.setTenantId(this.getTenantId());
    Datasource datasource = EntityConfigUtil.getDatasource(helperInfo.getHelperBaseName());
    datasource.inlineJdbc.setJdbcUri(this.getJdbcUri());

    // get pool and shared connection
    DBCPConnectionFactory managedConnectionFactory = (DBCPConnectionFactory) ConnectionFactory
            .getManagedConnectionFactory();
    GenericObjectPool pool = managedConnectionFactory.getGenericObjectPool(helperInfo);
    XAConnectionFactory xacf = managedConnectionFactory.getXAConnectionFactory(helperInfo);

    // return shared connection
    if (UtilValidate.isNotEmpty(xacf)) {
        TransactionRegistry transactionRegistry = xacf.getTransactionRegistry();
        TransactionContext transactionContext = transactionRegistry.getActiveTransactionContext();
        if (UtilValidate.isNotEmpty(transactionContext)) {
            PoolableConnection sharedConnection = (PoolableConnection) transactionContext.getSharedConnection();

            try {
                pool.returnObject(sharedConnection);
                pool.clear();
            } catch (Exception e) {
                Debug.logError(e, module);
            }
        }
    }

    // do delete database
    doDeleteDatabase(helperInfo);

    // remove delegator
    String tenantDelegatorName = delegator.getDelegatorBaseName() + "#" + this.getTenantId();
    DelegatorFactory.removeDelegator(tenantDelegatorName);

    // remove connection
    managedConnectionFactory.removeConnection(helperInfo);
}