Example usage for org.apache.commons.dbcp BasicDataSource close

List of usage examples for org.apache.commons.dbcp BasicDataSource close

Introduction

In this page you can find the example usage for org.apache.commons.dbcp BasicDataSource close.

Prototype

public synchronized void close() throws SQLException 

Source Link

Document

Close and release all connections that are currently stored in the connection pool associated with our data source.

Usage

From source file:com.cws.esolutions.security.listeners.SecurityServiceInitializer.java

/**
 * Shuts down the running security service process.
 *//* w w w  . j  av a  2s. c  o  m*/
public static void shutdown() {
    final String methodName = SecurityServiceInitializer.CNAME + "#shutdown()";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
    }

    final SecurityConfigurationData config = SecurityServiceInitializer.svcBean.getConfigData();
    Map<String, DataSource> dsMap = SecurityServiceInitializer.svcBean.getDataSources();

    if (DEBUG) {
        DEBUGGER.debug("SecurityConfigurationData: {}", config);
    }

    try {
        DAOInitializer.closeAuthConnection(
                new FileInputStream(FileUtils.getFile(config.getSecurityConfig().getAuthConfig())), false,
                svcBean);

        if (dsMap != null) {
            for (String key : dsMap.keySet()) {
                if (DEBUG) {
                    DEBUGGER.debug("Key: {}", key);
                }

                BasicDataSource dataSource = (BasicDataSource) dsMap.get(key);

                if (DEBUG) {
                    DEBUGGER.debug("BasicDataSource: {}", dataSource);
                }

                if ((dataSource != null) && (!(dataSource.isClosed()))) {
                    dataSource.close();
                }
            }
        }
    } catch (SQLException sqx) {
        ERROR_RECORDER.error(sqx.getMessage(), sqx);
    } catch (FileNotFoundException fnfx) {
        ERROR_RECORDER.error(fnfx.getMessage(), fnfx);
    }
}

From source file:com.cws.esolutions.security.utils.DAOInitializer.java

/**
 * @param properties - The <code>AuthRepo</code> object containing connection information
 * @param isContainer - A <code>boolean</code> flag indicating if this is in a container
 * @param bean - The {@link com.cws.esolutions.security.SecurityServiceBean} <code>SecurityServiceBean</code> that holds the connection
 */// w  w w.  java2 s  .  c om
public synchronized static void closeAuthConnection(final InputStream properties, final boolean isContainer,
        final SecurityServiceBean bean) {
    String methodName = DAOInitializer.CNAME
            + "#closeAuthConnection(final InputStream properties, final boolean isContainer, final SecurityServiceBean bean)";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("InputStream: {}", properties);
        DEBUGGER.debug("isContainer: {}", isContainer);
        DEBUGGER.debug("SecurityServiceBean: {}", bean);
    }

    try {
        Properties connProps = new Properties();
        connProps.load(properties);

        if (DEBUG) {
            DEBUGGER.debug("Properties: {}", connProps);
        }

        AuthRepositoryType repoType = AuthRepositoryType
                .valueOf(connProps.getProperty(DAOInitializer.REPO_TYPE));

        if (DEBUG) {
            DEBUGGER.debug("AuthRepositoryType: {}", repoType);
        }

        switch (repoType) {
        case LDAP:
            LDAPConnectionPool ldapPool = (LDAPConnectionPool) bean.getAuthDataSource();

            if (DEBUG) {
                DEBUGGER.debug("LDAPConnectionPool: {}", ldapPool);
            }

            if ((ldapPool != null) && (!(ldapPool.isClosed()))) {
                ldapPool.close();
            }

            break;
        case SQL:
            // the isContainer only matters here
            if (!(isContainer)) {
                BasicDataSource dataSource = (BasicDataSource) bean.getAuthDataSource();

                if (DEBUG) {
                    DEBUGGER.debug("BasicDataSource: {}", dataSource);
                }

                if ((dataSource != null) && (!(dataSource.isClosed()))) {
                    dataSource.close();
                }
            }

            break;
        case NONE:
            return;
        default:
            return;
        }
    } catch (SQLException sqx) {
        ERROR_RECORDER.error(sqx.getMessage(), sqx);
    } catch (FileNotFoundException fnfx) {
        ERROR_RECORDER.error(fnfx.getMessage(), fnfx);
    } catch (IOException iox) {
        ERROR_RECORDER.error(iox.getMessage(), iox);
    }
}

From source file:io.pivotal.spring.xd.jdbcgpfdist.support.GreenplumDataSourceFactoryBean.java

@Override
protected void destroyInstance(BasicDataSource instance) throws Exception {
    instance.close();
}

From source file:database.DataSourceBase.java

public void shutdown() throws SQLException {
    BasicDataSource bs = (BasicDataSource) datasource;
    bs.close();
}

From source file:eu.artofcoding.beetlejuice.spring.SpringContextHelper.java

private void closeDataSources() {
    Map<String, BasicDataSource> map2 = applicationContext.getBeansOfType(BasicDataSource.class);
    for (String key : map2.keySet()) {
        BasicDataSource dataSource = map2.get(key);
        try {/*from ww  w .j a  v a 2s . c o m*/
            dataSource.close();
        } catch (SQLException e) {
            logger.error(
                    String.format("Could not close dataSource %s: %s", dataSource, e.getLocalizedMessage()), e);
        }
    }
}

From source file:net.firejack.platform.model.config.listener.ConfigContextLoaderListener.java

@Override
public void contextDestroyed(ServletContextEvent event) {
    try {/*from  w ww . j av  a  2s .  co m*/
        Map<String, BasicDataSource> dataSources = getCurrentWebApplicationContext()
                .getBeansOfType(BasicDataSource.class);
        for (BasicDataSource dataSource : dataSources.values()) {
            try {
                dataSource.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            try {
                DriverManager.deregisterDriver(drivers.nextElement());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        JAXBContextCache.clearCaches();
        ClassFactory.cleanCache();
        CachedIntrospectionResults.clearClassLoader(getClass().getClassLoader());
    } finally {
        super.contextDestroyed(event);
    }
}

From source file:edu.tamu.tcat.db.core.AbstractDataSourceFactory.java

/**
 * Close all datasources<br>/*from w  ww .  j ava  2s.  c  om*/
 * If the provider is a service, this should be invoked on service deregistration
 * */
public void shutdown() throws DataSourceException {
    DataSourceException exception = new DataSourceException(
            "Error closing " + getClass().getName() + "datasources");
    for (BasicDataSource ds : dataSources.values()) {
        try {
            ds.close();
        } catch (Exception e) {
            exception.addSuppressed(exception);
        }
    }
    if (exception.getSuppressed().length != 0)
        throw exception;
}

From source file:net.jetrix.DataSourceManager.java

/**
 * Configure a datasource./*from   ww  w.  ja  v a2  s  .c  om*/
 *
 * @param config      the configuration of the datasource
 * @param environment the environment of the datasource
 */
public void setDataSource(DataSourceConfig config, String environment) {
    try {
        Class.forName(config.getDriver());
    } catch (ClassNotFoundException e) {
        log.warning("Unable to find the database driver (" + config.getDriver()
                + "), put the related jar in the lib directory");
        return;
    }

    try {
        // close the previous datasource if necessary
        if (datasources.containsKey(environment)) {
            BasicDataSource datasource = (BasicDataSource) datasources.get(environment);
            datasource.close();
        }

        BasicDataSource datasource = new BasicDataSource();
        datasource.setDefaultAutoCommit(false);

        datasource.setDriverClassName(config.getDriver());
        datasource.setUrl(config.getUrl());
        datasource.setUsername(config.getUsername());
        datasource.setPassword(config.getPassword());
        datasource.setMinIdle(config.getMinIdle() != 0 ? config.getMinIdle() : DEFAULT_MIN_IDLE);
        datasource.setMaxActive(config.getMaxActive() != 0 ? config.getMaxActive() : DEFAULT_MAX_ACTIVE);

        // attempts to open the connection
        datasource.getConnection().close();

        datasources.put(environment, datasource);
    } catch (Exception e) {
        log.log(Level.SEVERE, "Unable to configure the datasource '" + environment + "'", e);
    }
}

From source file:com.alibaba.otter.manager.biz.common.DataSourceCreator.java

public void destroyDataSource(DataSource dataSource) {
    try {//from  w w w. j av  a 2 s.  c o m
        // for filter to destroy custom datasource
        if (letHandlerDestroyIfSupport(0L, dataSource)) {
            return;
        }

        if (dataSource == null) {
            return;
        }

        BasicDataSource basicDataSource = (BasicDataSource) dataSource;
        basicDataSource.close();
    } catch (SQLException e) {
        logger.error("ERROR ## close the datasource has an error", e);
    }
}

From source file:com.agiletec.ConfigTestUtils.java

/**
 * Effettua la chiusura dei datasource definiti nel contesto.
 * @param applicationContext Il contesto dell'applicazione.
 * @throws Exception In caso di errore nel recupero o chiusura dei DataSource.
 *///  w  w w.java 2  s.co m
public void closeDataSources(ApplicationContext applicationContext) throws Exception {
    String[] dataSourceNames = applicationContext.getBeanNamesForType(BasicDataSource.class);
    for (int i = 0; i < dataSourceNames.length; i++) {
        BasicDataSource dataSource = (BasicDataSource) applicationContext.getBean(dataSourceNames[i]);
        if (null != dataSource) {
            dataSource.close();
        }
    }
}