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:se.unlogic.hierarchy.core.cache.DataSourceCache.java

private DataSource getDataSourceInstance(DataSourceDescriptor dataSourceDescriptor)
        throws SQLException, DataSourceNotFoundInContextException {

    if (dataSourceDescriptor.getType() == DataSourceType.ContainerManaged) {

        try {//from  w w  w.j  a v  a2  s . c  o  m
            return DBUtils.getDataSource(dataSourceDescriptor.getUrl());
        } catch (NamingException e) {
            throw new DataSourceNotFoundInContextException(dataSourceDescriptor, e);
        }

    } else {

        BasicDataSource basicDataSource = DBCPUtils.createConnectionPool(dataSourceDescriptor);

        try {

            //Dummy call to initialize connection pool
            basicDataSource.getLogWriter();

            return basicDataSource;

        } catch (SQLException e) {

            if (basicDataSource != null) {

                try {
                    basicDataSource.close();
                } catch (SQLException e1) {

                    log.error("Error closing data source after failed initialization", e);
                }
            }

            throw e;
        }
    }
}

From source file:se.unlogic.hierarchy.core.cache.DataSourceCache.java

public void update(DataSourceDescriptor dataSourceDescriptor)
        throws SQLException, DataSourceNotFoundInContextException {

    try {/*from  ww  w  . j a  v  a2  s .co  m*/
        w.lock();

        Entry<DataSourceDescriptor, DataSourceWrapper> cachedEntry = getCachedDataSourceEntry(
                dataSourceDescriptor.getDataSourceID());

        if (cachedEntry != null) {

            DataSourceDescriptor oldDescritor = cachedEntry.getKey();
            DataSourceWrapper dataSourceWrapper = cachedEntry.getValue();

            log.info("Updating cached datasource " + oldDescritor);

            BasicDataSource oldDataSourceInstance = null;

            if (oldDescritor.getType() == DataSourceType.SystemManaged
                    && dataSourceWrapper.getDataSource() instanceof BasicDataSource) {

                // Save the old instance so it can be properly closed when the new one has been started
                oldDataSourceInstance = (BasicDataSource) dataSourceWrapper.getDataSource();
            }

            // Create new datasource instance
            DataSource dataSource;

            try {
                dataSource = this.getDataSourceInstance(dataSourceDescriptor);

            } catch (SQLException e) {

                log.error("Error updating data source " + dataSourceDescriptor, e);
                throw e;
            }

            // Switch to new datasource instance in datasource wrapper
            dataSourceWrapper.setDataSource(dataSource);

            // Update key in datasource map
            this.dataSourceMap.remove(dataSourceDescriptor);
            this.dataSourceMap.put(dataSourceDescriptor, dataSourceWrapper);

            if (oldDataSourceInstance != null) {
                try {
                    oldDataSourceInstance.close();
                } catch (SQLException e) {
                    log.error("Error closing old instance datasource " + oldDescritor + " after update");
                }
            }
        }
    } finally {
        w.unlock();
    }
}

From source file:se.unlogic.hierarchy.core.cache.DataSourceCache.java

public void stop(int dataSourceID) {

    try {//w  ww . j a  v  a  2s .c  o  m
        w.lock();

        Entry<DataSourceDescriptor, DataSourceWrapper> cachedEntry = getCachedDataSourceEntry(dataSourceID);

        if (cachedEntry != null) {

            DataSourceDescriptor descriptor = cachedEntry.getKey();
            DataSourceWrapper dataSourceWrapper = cachedEntry.getValue();

            log.info("Stopping datasource " + descriptor);

            BasicDataSource dataSourceInstance = null;

            if (descriptor.getType() == DataSourceType.SystemManaged
                    && dataSourceWrapper.getDataSource() instanceof BasicDataSource) {

                // Save the datasource instance so it can be properly closed after the wrapper has been stopped
                dataSourceInstance = (BasicDataSource) dataSourceWrapper.getDataSource();
            }

            // Stop the datasource
            dataSourceWrapper.stop();

            // Remove key from datasource map
            this.dataSourceMap.remove(descriptor);

            //Add key to stopped datasource map
            this.stoppedDataSourceMap.put(descriptor, dataSourceWrapper);

            if (dataSourceInstance != null) {
                try {
                    //                  String url = dataSourceInstance.getUrl();

                    dataSourceInstance.close();

                    //                  Driver driver = DriverManager.getDriver(url);
                    //
                    //                  if(driver != null){
                    //
                    //                     log.debug("Succesfully unregistered JDBC driver " + driver + " for URL " + url);
                    //                     DriverManager.deregisterDriver(driver);
                    //
                    //                  }else{
                    //
                    //                     log.warn("Unable to unregister JDBC driver " + driver + " for URL " + url);
                    //                  }

                } catch (SQLException e) {
                    log.error("Error closing datasource " + descriptor, e);
                }
            }
        }
    } finally {
        w.unlock();
    }
}

From source file:se.unlogic.hierarchy.core.cache.DataSourceCache.java

public void delete(int dataSourceID) {

    try {/*from   w w w  .  j ava2s  .  co  m*/
        w.lock();

        Entry<DataSourceDescriptor, DataSourceWrapper> cachedEntry = getCachedDataSourceEntry(dataSourceID);

        if (cachedEntry != null) {

            DataSourceDescriptor descriptor = cachedEntry.getKey();
            DataSourceWrapper dataSourceWrapper = cachedEntry.getValue();

            log.info("Deleting datasource " + descriptor);

            BasicDataSource dataSourceInstance = null;

            if (descriptor.getType() == DataSourceType.SystemManaged
                    && dataSourceWrapper.getDataSource() instanceof BasicDataSource) {

                // Save the datasource instance so it can be properly closed after the wrapper has been stopped
                dataSourceInstance = (BasicDataSource) dataSourceWrapper.getDataSource();
            }

            // Stop the datasource
            dataSourceWrapper.delete();

            // Remove key from datasource map
            this.dataSourceMap.remove(descriptor);

            // Remove key from stopped datasource map
            this.stoppedDataSourceMap.remove(descriptor);

            if (dataSourceInstance != null) {
                try {
                    dataSourceInstance.close();
                } catch (SQLException e) {
                    log.error("Error closing datasource " + descriptor);
                }
            }
        }
    } finally {
        w.unlock();
    }
}

From source file:xbird.util.jdbc.DBAccessor.java

/**
 * Closes the DataSource.//  w  w  w  .  j a v a  2s .c om
 */
public final void shutdownDataSource(DataSource ds) throws SQLException {
    // TODO who call me.
    BasicDataSource bds = (BasicDataSource) ds;
    bds.close();
}