Example usage for org.apache.commons.dbcp.managed TransactionRegistry getActiveTransactionContext

List of usage examples for org.apache.commons.dbcp.managed TransactionRegistry getActiveTransactionContext

Introduction

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

Prototype

public TransactionContext getActiveTransactionContext() throws SQLException 

Source Link

Document

Gets the active TransactionContext or null if not Transaction is active.

Usage

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

/**
 * delete database//  ww w .j  av  a 2  s .  c  o m
 * @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);
}