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

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

Introduction

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

Prototype

@Override
public synchronized void close() throws SQLException 

Source Link

Document

Closes and releases all idle connections that are currently stored in the connection pool associated with this data source.

Connections that are checked out to clients when this method is invoked are not affected.

Usage

From source file:BasicDataSourceExample.java

public static void shutdownDataSource(DataSource ds) throws SQLException {
    BasicDataSource bds = (BasicDataSource) ds;
    bds.close();
}

From source file:de.innovationgate.webgate.api.jdbc.pool.DBCPReplicationConnectionProvider.java

public void close() throws HibernateException {
    log.info("Close DBCPReplicationConnectionProvider");
    try {/*from   www .ja  v a 2s.c o m*/
        try {
            if (_masterDS != null) {
                _masterDS.close();
            }
        } catch (SQLException e) {
            log.error("Cannot close masterDS", e);
        }

        Iterator _slaves = _slaveDSList.iterator();
        while (_slaves.hasNext()) {
            try {
                BasicDataSource slaveDS = (BasicDataSource) _slaves.next();
                if (slaveDS != null) {
                    slaveDS.close();
                }
            } catch (SQLException e) {
                log.error("Cannot close slaveDS", e);
            }
        }
        _slaveDSList.clear();

    } catch (Exception e) {
        throw new HibernateException("Could not close DBCPReplicationConnectionProvider", e);
    }

    log.info("Close DBCPReplicationConnectionProvider complete");
}

From source file:com.microsoft.sqlserver.jdbc.connection.PoolingTest.java

/**
 * test connection pool with Apache DBCP
 * /*from   w  w w . jav  a2 s  .c om*/
 * @throws SQLException
 */
@Test
public void testApacheDBCP() throws SQLException {
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(connectionString);

    try {
        connect(ds);
    } finally {
        ds.close();
    }
}

From source file:com.dsf.dbxtract.cdc.AppJournalWindowTest.java

/**
 * Rigourous Test :-)//w  ww  .  ja  v a  2 s  . c  om
 * 
 * @throws Exception
 *             in case of any error
 */
@Test(dependsOnMethods = "setUp", timeOut = 120000)
public void testAppWithJournalWindow() throws Exception {

    final Config config = new Config(configFile);

    BasicDataSource ds = new BasicDataSource();
    Source source = config.getDataSources().getSources().get(0);
    ds.setDriverClassName(source.getDriver());
    ds.setUsername(source.getUser());
    ds.setPassword(source.getPassword());
    ds.setUrl(source.getConnection());

    // prepara os dados
    Connection conn = ds.getConnection();

    conn.createStatement().execute("truncate table test");
    conn.createStatement().execute("truncate table j$test");

    // Carrega os dados de origem
    PreparedStatement ps = conn.prepareStatement("insert into test (key1,key2,data) values (?,?,?)");
    for (int i = 0; i < TEST_SIZE; i++) {
        if ((i % 100) == 0) {
            ps.executeBatch();
        }
        ps.setInt(1, i);
        ps.setInt(2, i);
        ps.setInt(3, (int) Math.random() * 500);
        ps.addBatch();
    }
    ps.executeBatch();
    ps.close();

    // Popula as tabelas de journal
    ps = conn.prepareStatement("insert into j$test (key1,key2) values (?,?)");
    for (int i = 0; i < TEST_SIZE; i++) {
        if ((i % 500) == 0) {
            ps.executeBatch();
        }
        ps.setInt(1, i);
        ps.setInt(2, i);
        ps.addBatch();
    }
    ps.executeBatch();
    ps.close();

    Long maxWindowId = 0L;
    ResultSet rs = conn.createStatement().executeQuery("select max(window_id) from j$test");
    if (rs.next()) {
        maxWindowId = rs.getLong(1);
        System.out.println("maximum window_id loaded: " + maxWindowId);
    }
    rs.close();
    conn.close();
    ds.close();

    // Clear any previous test
    String zkKey = "/dbxtract/cdc/" + source.getName() + "/J$TEST/lastWindowId";
    if (client.checkExists().forPath(zkKey) != null)
        client.delete().forPath(zkKey);

    // starts monitor
    Monitor.getInstance(config);

    // start app
    app = new App(config);
    System.out.println(config.toString());
    app.start();

    Assert.assertEquals(config.getHandlers().iterator().next().getStrategy(), JournalStrategy.WINDOW);

    while (true) {
        TimeUnit.MILLISECONDS.sleep(500);

        try {
            Long lastWindowId = Long.parseLong(new String(client.getData().forPath(zkKey)));
            System.out.println("lastWindowId = " + lastWindowId);
            if (maxWindowId.longValue() == lastWindowId.longValue()) {
                System.out.println("expected window_id reached");
                break;
            }

        } catch (NoNodeException nne) {
            System.out.println("ZooKeeper - no node exception :: " + zkKey);
        }
    }
}

From source file:com.dsf.dbxtract.cdc.AppJournalDeleteTest.java

/**
 * Rigourous Test :-)//from w  w  w . j  a  v  a 2  s . co m
 * 
 * @throws Exception
 *             in case of any error
 */
@Test(timeOut = 120000)
public void testAppWithJournalDelete() throws Exception {

    final Config config = new Config(configFile);

    BasicDataSource ds = new BasicDataSource();
    Source source = config.getDataSources().getSources().get(0);
    ds.setDriverClassName(source.getDriver());
    ds.setUsername(source.getUser());
    ds.setPassword(source.getPassword());
    ds.setUrl(source.getConnection());

    // prepara os dados
    Connection conn = ds.getConnection();

    conn.createStatement().execute("truncate table test");
    conn.createStatement().execute("truncate table j$test");

    // Carrega os dados de origem
    PreparedStatement ps = conn.prepareStatement("insert into test (key1,key2,data) values (?,?,?)");
    for (int i = 0; i < TEST_SIZE; i++) {
        if ((i % 100) == 0) {
            ps.executeBatch();
        }
        ps.setInt(1, i);
        ps.setInt(2, i);
        ps.setInt(3, (int) Math.random() * 500);
        ps.addBatch();
    }
    ps.executeBatch();
    ps.close();

    app = new App(config);
    app.start();

    Assert.assertEquals(config.getHandlers().iterator().next().getStrategy(), JournalStrategy.DELETE);

    // Popula as tabelas de journal
    ps = conn.prepareStatement("insert into j$test (key1,key2) values (?,?)");
    for (int i = 0; i < TEST_SIZE; i++) {
        if ((i % 500) == 0) {
            ps.executeBatch();
        }
        ps.setInt(1, i);
        ps.setInt(2, i);
        ps.addBatch();
    }
    ps.executeBatch();
    ps.close();

    while (true) {
        TimeUnit.MILLISECONDS.sleep(500);

        ResultSet rs = conn.createStatement().executeQuery("select count(*) from j$test");
        if (rs.next()) {
            long count = rs.getLong(1);
            System.out.println("remaining journal rows: " + count);
            rs.close();
            if (count == 0L)
                break;
        }
    }
    conn.close();
    ds.close();
}

From source file:org.apache.jmeter.protocol.jdbc.config.DataSourceElement.java

@Override
public void testEnded() {
    synchronized (this) {
        if (dbcpDataSource != null) {
            try {
                dbcpDataSource.close();//from ww w. j av a  2 s. c o m
            } catch (SQLException ex) {
                log.error("Error closing pool:" + getName(), ex);
            }
        }
        dbcpDataSource = null;
    }
    if (perThreadPoolSet != null) {// in case
        for (BasicDataSource dsc : perThreadPoolSet) {
            log.debug("Closing pool: " + getDataSourceName() + " @" + System.identityHashCode(dsc));
            try {
                dsc.close();
            } catch (SQLException ex) {
                log.error("Error closing pool:" + getName(), ex);
            }
        }
        perThreadPoolSet = null;
    }
}

From source file:org.ofbiz.core.entity.transaction.DBCPConnectionFactory.java

/**
 * Shuts down and removes a datasource, if it exists
 *
 * @param helperName The name of the datasource to remove
 */// w w w .j ava  2s. c o m
public synchronized static void removeDatasource(String helperName) {
    BasicDataSource dataSource = dsCache.get(helperName);
    if (dataSource != null) {
        try {
            dataSource.close();
            unregisterMBeanIfPresent();
        } catch (Exception e) {
            Debug.logError(e, "Error closing connection pool in DBCP");
        }

        dsCache.remove(helperName);
    }
    trackerCache.remove(helperName);
}

From source file:org.ramadda.repository.database.DatabaseManager.java

/**
 * _more_//www  .j  a  v  a 2  s. c om
 *
 * @throws Exception _more_
 */
public void reInitialize() throws Exception {
    if (dataSource != null) {
        BasicDataSource bds = (BasicDataSource) dataSource;
        try {
            bds.close();
        } catch (Exception exc) {
            logError("Closing data source", exc);
        }
        dataSource = doMakeDataSource();
    }
}

From source file:xml.cz.Parser.java

/**
 * Main class in which all data are parsed
 * @param args arguments on main class - not supported
 * @throws IOException/* ww w. j  av  a  2 s .  c  o  m*/
 * @throws SQLException
 * @throws SAXException
 * @throws ParserConfigurationException 
 */
public static void main(String[] args)
        throws IOException, SQLException, SAXException, ParserConfigurationException {
    Properties pro = new Properties();
    pro.load(new FileInputStream("src/main/java/configuration/jdbc.properties"));

    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(pro.getProperty("url"));
    ds.setUsername(pro.getProperty("username"));
    ds.setPassword(pro.getProperty("password"));

    SectorManagerImpl sectorManager = new SectorManagerImpl();
    sectorManager.setDataSource(ds);
    ClassificationManagerImpl classificationManager = new ClassificationManagerImpl();
    classificationManager.setDataSource(ds);
    RegionManagerImpl regionManager = new RegionManagerImpl();
    regionManager.setDataSource(ds);

    Parser parser = new Parser();
    parser.parseSector(sectorManager);
    parser.parseClassification(classificationManager);
    parser.parseRegion(regionManager);

    ds.close();
}

From source file:xml.sk.Parser.java

public static void main(String[] args) throws IOException, SQLException, ParserConfigurationException,
        SAXException, XPathExpressionException, ParseException {
    Properties pro = new Properties();
    pro.load(new FileInputStream("src/main/java/configuration/jdbc.properties"));

    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(pro.getProperty("url"));
    ds.setUsername(pro.getProperty("username"));
    ds.setPassword(pro.getProperty("password"));

    SectorManagerImpl sectorManager = new SectorManagerImpl();
    sectorManager.setDataSource(ds);//from ww  w  .  ja  v  a2 s. c  om
    ClassificationManagerImpl classificationManager = new ClassificationManagerImpl();
    classificationManager.setDataSource(ds);
    EducationManagerImpl educationManager = new EducationManagerImpl();
    educationManager.setDataSource(ds);
    AgeManagerImpl ageManager = new AgeManagerImpl();
    ageManager.setDataSource(ds);

    Parser parser = new Parser();
    parser.parseSectorSk(sectorManager);
    parser.parseClassificationSk(classificationManager);
    parser.parseEducationSk(educationManager);
    parser.parseAgeSk(ageManager);

    ds.close();
}