Example usage for org.apache.commons.dbcp.managed ManagedConnection getDelegate

List of usage examples for org.apache.commons.dbcp.managed ManagedConnection getDelegate

Introduction

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

Prototype

public Connection getDelegate() 

Source Link

Usage

From source file:org.ofbiz.tenant.util.TenantUtil.java

/**
 * is connection available/*from  ww  w .  java 2s.c  om*/
 * @param tenantId
 * @param delegator
 * @return
 */
public static boolean isConnectionAvailable(String tenantId, Delegator delegator) {
    try {
        List<GenericValue> tenantDataSources = delegator.findByAnd("TenantDataSource",
                UtilMisc.toMap("tenantId", tenantId), null, false);
        for (GenericValue tenantDataSource : tenantDataSources) {
            String entityGroupName = tenantDataSource.getString("entityGroupName");
            String jdbcUri = tenantDataSource.getString("jdbcUri");
            String jdbcUsername = tenantDataSource.getString("jdbcUsername");
            String jdbcPassword = tenantDataSource.getString("jdbcPassword");

            GenericHelperInfo helperInfo = delegator.getGroupHelperInfo(entityGroupName);
            Connection connection = ConnectionFactory.getConnection(jdbcUri, jdbcUsername, jdbcPassword);
            ManagedConnection managedConn = (ManagedConnection) ConnectionFactory.getConnection(helperInfo);
            PoolableConnection poolConn = (PoolableConnection) managedConn.getDelegate();
            Connection innermostDelegate = poolConn.getInnermostDelegate();
            if (UtilValidate.isNotEmpty(connection)) {
                if (!innermostDelegate.getClass().getName().equals(connection.getClass().getName())) {
                    return false;
                }
            } else {
                return false;
            }
        }
    } catch (Exception e) {
        Debug.logWarning(e, module);
        return false;
    }
    return true;
}