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.javacreed.examples.app.Example1.java

public static void main(final String[] args) throws SQLException {
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:target/db");
    ds.setUsername("sa");
    ds.setPassword("");

    try (Connection connection = ds.getConnection()) {
        System.out.println("Connected");
    } finally {//from  w  w  w . j a  v  a2  s. c om
        ds.close();
    }
}

From source file:com.javacreed.examples.app.Example2.java

public static void main(final String[] args) throws SQLException {
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:target/db");
    ds.setUsername("sa");
    ds.setPassword("");

    try {//from   ww w  .ja  v a2s. c  o  m
        final Flyway flyway = new Flyway();
        flyway.setDataSource(ds);
        flyway.migrate();

        try (Connection connection = ds.getConnection(); Statement stmt = connection.createStatement()) {
            stmt.executeUpdate(
                    "INSERT INTO contacts(name, contacts) VALUES ('Albert Attard', 'albert@javacreed.com')");

            System.out.println("Contacts");
            try (ResultSet rs = stmt.executeQuery("SELECT * FROM contacts")) {
                while (rs.next()) {
                    System.out.printf("  >> [%d] %s (%s)%n", rs.getInt("id"), rs.getString("name"),
                            rs.getString("contacts"));
                }
            }
        }
    } finally {
        ds.close();
    }
}

From source file:com.javacreed.examples.app.Main.java

public static void main(final String[] args) {

    Main.LOGGER.debug("Creating the data source");
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:target/db");
    ds.setUsername("sa");
    ds.setPassword("");

    try {//from www .  j  a  va 2s  .co m
        Main.LOGGER.debug("Executing Flyway (database migration)");
        final Flyway flyway = new Flyway();
        flyway.setDataSource(ds);
        flyway.migrate();

        try (Connection connection = ds.getConnection(); Statement stmt = connection.createStatement()) {
            stmt.executeUpdate(
                    "INSERT INTO contacts(name, contacts) VALUES ('Albert Attard', 'albert@javacreed.com')");

            Main.LOGGER.debug("Contacts");
            try (ResultSet rs = stmt.executeQuery("SELECT * FROM contacts")) {
                while (rs.next()) {
                    Main.LOGGER.debug("  >> [{}] {} ({})", rs.getInt("id"), rs.getString("name"),
                            rs.getString("contacts"));
                }
            }
        } catch (final SQLException e) {
            Main.LOGGER.error("Failed", e);
        }
    } finally {
        try {
            ds.close();
        } catch (final SQLException e) {
            Main.LOGGER.error("Failed to close the data source", e);
        }
    }
}

From source file:BasicDataSourceExample.java

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

From source file:com.stratelia.silverpeas.silverStatisticsPeas.control.PerfVolumeTest.java

@AfterClass
public static void teardownDataSource() throws NamingException, SQLException {
    InitialContext ic = new InitialContext();
    BasicDataSource ds = (BasicDataSource) ic.lookup("java:/datasources/silverpeas-jdbc");
    ds.close();
    SimpleMemoryContextFactory.tearDownAsInitialContext();
}

From source file:de.mendelson.comm.as2.database.DBDriverManager.java

/**
 * shutdown the connection pool//from w w  w.  j a  v  a2s .  co  m
 */
public static void shutdownConnectionPool() throws SQLException {
    if (dataSourceConfig != null) {
        BasicDataSource bdsConfig = (BasicDataSource) dataSourceConfig;
        bdsConfig.close();
    }
    if (dataSourceRuntime != null) {
        BasicDataSource bdsRuntime = (BasicDataSource) dataSourceRuntime;
        bdsRuntime.close();
    }
}

From source file:com.weibo.datasys.service.DBManager.java

/**
 * /*from   w  w w. j a v a 2  s  .c o m*/
 * ?????????????
 * 
 * @param dsname
 */
public static void removeDataSource(String dsname) {
    try {
        BasicDataSource ds = dsMap.remove(dsname);
        if (ds != null) {
            ds.close();
            logger.info("[RemoveDataSourceOK] - dsname={}", dsname);
        }
    } catch (Exception e) {
        logger.error("[RemoveDataSourceError] - ", e);
    }
}

From source file:com.weibo.datasys.common.db.DBManager.java

/**
 * // w  w w .j  a  v a2s.  com
 * ???????{@code DBManager.initDataSource(String)}
 * ?????
 * 
 * @param dsname
 */
public static void closeDataSource(String dsname) {
    try {
        BasicDataSource ds = dsMap.get(dsname);
        if (ds != null) {
            ds.close();
            logger.info("[CloseDataSourceOK] - dsname={}", dsname);
        }
    } catch (Exception e) {
        logger.error("[CloseDataSourceError] - ", e);
    }
}

From source file:com.weibo.datasys.common.db.DBManager.java

/**
 * /* w  w  w  .ja v a 2s .com*/
 * ?????????????
 * 
 * @param dsname
 */
public static void removeDataSource(String dsname) {
    try {
        dynDSConfigMap.remove(dsname);
        BasicDataSource ds = dsMap.remove(dsname);
        if (ds != null) {
            ds.close();
            logger.info("[RemoveDataSourceOK] - dsname={}", dsname);
        }
    } catch (Exception e) {
        logger.error("[RemoveDataSourceError] - ", e);
    }
}

From source file:com.cws.esolutions.core.listeners.CoreServiceInitializer.java

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

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

    final Map<String, DataSource> datasources = CoreServiceInitializer.appBean.getDataSources();

    try {
        if ((datasources != null) && (datasources.size() != 0)) {
            for (String key : datasources.keySet()) {
                if (DEBUG) {
                    DEBUGGER.debug("Key: {}", key);
                }

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

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

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