Java Code Examples for org.apache.commons.dbcp.BasicDataSource#close()

The following examples show how to use org.apache.commons.dbcp.BasicDataSource#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultJdbcComponentTest.java    From apiman with Apache License 2.0 6 votes vote down vote up
@Test
public void testDataSource() throws Throwable {
    DefaultJdbcComponent component = new DefaultJdbcComponent();

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa");
    ds.setPassword("");
    ds.setUrl("jdbc:h2:mem:testDataSource;DB_CLOSE_DELAY=-1");

    try {
        IJdbcClient client = component.create(ds);
        doAllTests(client);
    } finally {
        try {
            ds.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
 
Example 2
Source File: CommonsDBCPTest.java    From herddb with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:herddb:server:localhost:7000?");
        dataSource.setDriverClassName(Driver.class.getName());
        try (Connection connection = dataSource.getConnection();
             Statement statement = connection.createStatement();
             ResultSet rs = statement.executeQuery("SELECT * FROM SYSTABLES")) {
            int count = 0;
            while (rs.next()) {
                System.out.println("table: " + rs.getString(1));
                count++;
            }
            assertTrue(count > 0);
        }
        dataSource.close();

    }
}
 
Example 3
Source File: DataStoreBaseTest.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
protected void closeH2DB(String databaseName) throws Exception {

        BasicDataSource dataSource = dataSourceMap.get(databaseName);
        if (dataSource != null) {
            dataSource.close();
        }
    }
 
Example 4
Source File: DynamicDBUtil.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 关闭数据库连接池
 *  @param dbKey
 * @return 
 * @return
 */
public static void closeDBkey(final String dbKey){
	BasicDataSource dataSource = getDbSourceBydbKey(dbKey);
	try {
		if(dataSource!=null && !dataSource.isClosed()){
			dataSource.getConnection().commit();
			dataSource.getConnection().close();
			dataSource.close();
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: JdbcStorageUpgradeTest.java    From apicurio-studio with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpgradeFromVersion7toVersion8() throws Exception {
    BasicDataSource ds = createDatasourceFrom("upgrade-test.v7to8.mv.sql");
    try {
        HubConfiguration config = new HubConfiguration();
        H2SqlStatements sqlStatements = new H2SqlStatements(config);
        JdbcStorage storage = new JdbcStorage();
        TestUtil.setPrivateField(storage, "config", config);
        TestUtil.setPrivateField(storage, "dataSource", ds);
        TestUtil.setPrivateField(storage, "sqlStatements", sqlStatements);
        
        // Run the postConstruct method which will upgrade the DB to version 8
        storage.postConstruct();
        
        // List the API designs for eric - they should all have a "type" set!
        Collection<ApiDesign> designs = storage.listApiDesigns("[email protected]");
        Iterator<ApiDesign> diter = designs.iterator();
        ApiDesign design1 = diter.next();
        Assert.assertNotNull(design1.getType());
        Assert.assertEquals(design1.getType(), ApiDesignType.OpenAPI20);
        ApiDesign design2 = diter.next();
        Assert.assertNotNull(design2.getType());
        Assert.assertEquals(design2.getType(), ApiDesignType.OpenAPI30);
    } finally {
        ds.close();
    }
}
 
Example 6
Source File: TestUtils.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public static void closeH2Base() throws Exception {

        BasicDataSource dataSource = dataSourceMap.get(DB_NAME);
        if (dataSource != null) {
            dataSource.close();
        }
    }
 
Example 7
Source File: TestUtils.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public static void closeH2Base() throws Exception {

        BasicDataSource dataSource = dataSourceMap.get(DB_NAME);
        if (dataSource != null) {
            dataSource.close();
        }
    }
 
Example 8
Source File: TestUtils.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Close the initiated H2 database.
 * @throws Exception
 */
public static void closeH2Base() throws Exception {

    BasicDataSource dataSource = dataSourceMap.get(DB_NAME);
    if (dataSource != null) {
        dataSource.close();
    }
}
 
Example 9
Source File: DynamicDBUtil.java    From teaching with Apache License 2.0 5 votes vote down vote up
/**
 * 关闭数据库连接池
 *
 * @param dbKey
 * @return
 */
public static void closeDbKey(final String dbKey) {
    BasicDataSource dataSource = getDbSourceByDbKey(dbKey);
    try {
        if (dataSource != null && !dataSource.isClosed()) {
            dataSource.getConnection().commit();
            dataSource.getConnection().close();
            dataSource.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: DdlUtilsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
static void runImportExportTest_Export(final String clientUrl,
    final String userName, final String password,
    final String schemaFileName, final String dataFileName) throws Throwable {

  // first dump this to XML

  String dbtype = (new PlatformUtils()).determineDatabaseType(
      "com.pivotal.gemfirexd.jdbc.ClientDriver", "jdbc:gemfirexd://" + clientUrl);
  getLogger().info("DB TYPE = " + dbtype);

  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("com.pivotal.gemfirexd.jdbc.ClientDriver");
  dataSource.setUrl("jdbc:gemfirexd://" + clientUrl);
  if (userName != null) {
    dataSource.setUsername(userName);
    dataSource.setPassword(password);
  }

  DatabaseToDdlTask fromDBTask = new DatabaseToDdlTask();
  fromDBTask.addConfiguredDatabase(dataSource);
  fromDBTask.setDatabaseType(dbtype);

  WriteSchemaToFileCommand writeSchemaToFile = new WriteSchemaToFileCommand();
  File schemaFile = new File(schemaFileName);
  writeSchemaToFile.setFailOnError(true);
  writeSchemaToFile.setOutputFile(schemaFile);

  WriteDataToFileCommand writeDataToFile = new WriteDataToFileCommand();
  File dataFile = new File(dataFileName);
  writeDataToFile.setFailOnError(true);
  writeDataToFile.setOutputFile(dataFile);

  fromDBTask.addWriteSchemaToFile(writeSchemaToFile);
  fromDBTask.addWriteDataToFile(writeDataToFile);

  fromDBTask.execute();

  dataSource.close();

  // end dump to XML
}
 
Example 11
Source File: DdlUtilsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
static void runImportExportTest_ImportFail(final Connection netConn,
    final String clientUrl, final String userName, final String password,
    final int rowSize, final String schema, final String schemaFileName,
    final String dataFileName) throws Throwable {

  final String schemaPrefix = schema != null ? (schema + '.') : "";
  final Statement stmt = netConn.createStatement();
  final char[] desc = new char[rowSize];
  final int id;

  // check for failure in loading tables using the XML data with existing
  // duplicate data (atomically for batching)

  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("com.pivotal.gemfirexd.jdbc.ClientDriver");
  dataSource.setUrl("jdbc:gemfirexd://" + clientUrl);
  if (userName != null) {
    dataSource.setUsername(userName);
    dataSource.setPassword(password);
  }

  DdlToDatabaseTask toDBTask = new DdlToDatabaseTask();
  toDBTask.addConfiguredDatabase(dataSource);
  File schemaFile = new File(schemaFileName);
  toDBTask.setSchemaFile(schemaFile);

  WriteSchemaToDatabaseCommand writeSchemaToDB =
      new WriteSchemaToDatabaseCommand();
  writeSchemaToDB.setAlterDatabase(true);
  writeSchemaToDB.setFailOnError(true);

  toDBTask.addWriteSchemaToDatabase(writeSchemaToDB);
  toDBTask.execute();

  WriteDataToDatabaseCommand writeDataToDB = new WriteDataToDatabaseCommand();
  File dataFile = new File(dataFileName);
  writeDataToDB.setIsolationLevel(Connection.TRANSACTION_READ_COMMITTED);
  writeDataToDB.setUseBatchMode(true);
  writeDataToDB.setBatchSize(1000);
  writeDataToDB.setDataFile(dataFile);
  writeDataToDB.setFailOnError(true);

  id = 10;
  stmt.execute("insert into " + schemaPrefix + "language values (" + id
      + ", 'lang" + id + "')");
  final PreparedStatement pstmt = netConn.prepareStatement("insert into "
      + schemaPrefix + "film (film_id, title, description, language_id, "
      + "original_language_id) values (?, ?, ?, ?, ?)");
  pstmt.setInt(1, id);
  pstmt.setString(2, "film" + id);
  for (int index = 0; index < desc.length; index++) {
    desc[index] = (char)('<' + (index % 32));
  }
  pstmt.setString(3, new String(desc));
  pstmt.setInt(4, id);
  pstmt.setInt(5, id);
  pstmt.execute();

  toDBTask = new DdlToDatabaseTask();
  toDBTask.addConfiguredDatabase(dataSource);
  toDBTask.setSchemaFile(schemaFile);

  toDBTask.addWriteDataToDatabase(writeDataToDB);
  try {
    toDBTask.execute();
    fail("expected constraint violation");
  } catch (Throwable t) {
    while (t.getCause() != null && !(t instanceof SQLException)) {
      t = t.getCause();
    }
    if (!(t instanceof SQLException)
        || !"23505".equals(((SQLException)t).getSQLState())) {
      throw t;
    }
  }

  assertEquals(1, stmt.executeUpdate("delete from " + schemaPrefix
      + "film where film_id=" + id));
  assertEquals(1, stmt.executeUpdate("delete from " + schemaPrefix
      + "language where language_id=" + id));

  dataSource.close();
}
 
Example 12
Source File: DdlUtilsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
static void runImportExportTest_Import(final Connection netConn,
    final String clientUrl, final String userName, final String password,
    final String schemaFileName, final String dataFileName) throws Throwable {

  // now load the tables using the XML data

  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("com.pivotal.gemfirexd.jdbc.ClientDriver");
  dataSource.setUrl("jdbc:gemfirexd://" + clientUrl);
  if (userName != null) {
    dataSource.setUsername(userName);
    dataSource.setPassword(password);
  }

  DdlToDatabaseTask toDBTask = new DdlToDatabaseTask();
  toDBTask.addConfiguredDatabase(dataSource);
  File schemaFile = new File(schemaFileName);
  toDBTask.setSchemaFile(schemaFile);

  WriteSchemaToDatabaseCommand writeSchemaToDB =
      new WriteSchemaToDatabaseCommand();
  writeSchemaToDB.setAlterDatabase(true);
  writeSchemaToDB.setFailOnError(true);

  toDBTask.addWriteSchemaToDatabase(writeSchemaToDB);
  toDBTask.execute();

  WriteDataToDatabaseCommand writeDataToDB = new WriteDataToDatabaseCommand();
  File dataFile = new File(dataFileName);
  writeDataToDB.setIsolationLevel(Connection.TRANSACTION_READ_COMMITTED);
  writeDataToDB.setUseBatchMode(true);
  writeDataToDB.setBatchSize(1000);
  writeDataToDB.setDataFile(dataFile);
  writeDataToDB.setFailOnError(true);

  // next check for the success case
  toDBTask = new DdlToDatabaseTask();
  toDBTask.addConfiguredDatabase(dataSource);
  toDBTask.setSchemaFile(schemaFile);

  toDBTask.addWriteDataToDatabase(writeDataToDB);
  toDBTask.execute();

  dataSource.close();

  // end load the tables using the XML data
}
 
Example 13
Source File: GreenplumDataSourceFactoryBean.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
@Override
protected void destroyInstance(BasicDataSource instance) throws Exception {
	instance.close();
}
 
Example 14
Source File: DdlUtilsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
static void runImportExportTest_Export(final String clientUrl,
    final String userName, final String password,
    final String schemaFileName, final String dataFileName) throws Throwable {

  // first dump this to XML

  String dbtype = (new PlatformUtils()).determineDatabaseType(
      "com.pivotal.gemfirexd.jdbc.ClientDriver", "jdbc:gemfirexd://" + clientUrl);
  getLogger().info("DB TYPE = " + dbtype);

  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("com.pivotal.gemfirexd.jdbc.ClientDriver");
  dataSource.setUrl("jdbc:gemfirexd://" + clientUrl);
  if (userName != null) {
    dataSource.setUsername(userName);
    dataSource.setPassword(password);
  }

  DatabaseToDdlTask fromDBTask = new DatabaseToDdlTask();
  fromDBTask.addConfiguredDatabase(dataSource);
  fromDBTask.setDatabaseType(dbtype);

  WriteSchemaToFileCommand writeSchemaToFile = new WriteSchemaToFileCommand();
  File schemaFile = new File(schemaFileName);
  writeSchemaToFile.setFailOnError(true);
  writeSchemaToFile.setOutputFile(schemaFile);

  WriteDataToFileCommand writeDataToFile = new WriteDataToFileCommand();
  File dataFile = new File(dataFileName);
  writeDataToFile.setFailOnError(true);
  writeDataToFile.setOutputFile(dataFile);

  fromDBTask.addWriteSchemaToFile(writeSchemaToFile);
  fromDBTask.addWriteDataToFile(writeDataToFile);

  fromDBTask.execute();

  dataSource.close();

  // end dump to XML
}
 
Example 15
Source File: DdlUtilsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
static void runImportExportTest_ImportFail(final Connection netConn,
    final String clientUrl, final String userName, final String password,
    final int rowSize, final String schema, final String schemaFileName,
    final String dataFileName) throws Throwable {

  final String schemaPrefix = schema != null ? (schema + '.') : "";
  final Statement stmt = netConn.createStatement();
  final char[] desc = new char[rowSize];
  final int id;

  // check for failure in loading tables using the XML data with existing
  // duplicate data (atomically for batching)

  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("com.pivotal.gemfirexd.jdbc.ClientDriver");
  dataSource.setUrl("jdbc:gemfirexd://" + clientUrl);
  if (userName != null) {
    dataSource.setUsername(userName);
    dataSource.setPassword(password);
  }

  DdlToDatabaseTask toDBTask = new DdlToDatabaseTask();
  toDBTask.addConfiguredDatabase(dataSource);
  File schemaFile = new File(schemaFileName);
  toDBTask.setSchemaFile(schemaFile);

  WriteSchemaToDatabaseCommand writeSchemaToDB =
      new WriteSchemaToDatabaseCommand();
  writeSchemaToDB.setAlterDatabase(true);
  writeSchemaToDB.setFailOnError(true);

  toDBTask.addWriteSchemaToDatabase(writeSchemaToDB);
  toDBTask.execute();

  WriteDataToDatabaseCommand writeDataToDB = new WriteDataToDatabaseCommand();
  File dataFile = new File(dataFileName);
  writeDataToDB.setIsolationLevel(Connection.TRANSACTION_READ_COMMITTED);
  writeDataToDB.setUseBatchMode(true);
  writeDataToDB.setBatchSize(1000);
  writeDataToDB.setDataFile(dataFile);
  writeDataToDB.setFailOnError(true);

  id = 10;
  stmt.execute("insert into " + schemaPrefix + "language values (" + id
      + ", 'lang" + id + "')");
  final PreparedStatement pstmt = netConn.prepareStatement("insert into "
      + schemaPrefix + "film (film_id, title, description, language_id, "
      + "original_language_id) values (?, ?, ?, ?, ?)");
  pstmt.setInt(1, id);
  pstmt.setString(2, "film" + id);
  for (int index = 0; index < desc.length; index++) {
    desc[index] = (char)('<' + (index % 32));
  }
  pstmt.setString(3, new String(desc));
  pstmt.setInt(4, id);
  pstmt.setInt(5, id);
  pstmt.execute();

  toDBTask = new DdlToDatabaseTask();
  toDBTask.addConfiguredDatabase(dataSource);
  toDBTask.setSchemaFile(schemaFile);

  toDBTask.addWriteDataToDatabase(writeDataToDB);
  try {
    toDBTask.execute();
    fail("expected constraint violation");
  } catch (Throwable t) {
    while (t.getCause() != null && !(t instanceof SQLException)) {
      t = t.getCause();
    }
    if (!(t instanceof SQLException)
        || !"23505".equals(((SQLException)t).getSQLState())) {
      throw t;
    }
  }

  assertEquals(1, stmt.executeUpdate("delete from " + schemaPrefix
      + "film where film_id=" + id));
  assertEquals(1, stmt.executeUpdate("delete from " + schemaPrefix
      + "language where language_id=" + id));

  dataSource.close();
}
 
Example 16
Source File: DdlUtilsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
static void runImportExportTest_Import(final Connection netConn,
    final String clientUrl, final String userName, final String password,
    final String schemaFileName, final String dataFileName) throws Throwable {

  // now load the tables using the XML data

  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("com.pivotal.gemfirexd.jdbc.ClientDriver");
  dataSource.setUrl("jdbc:gemfirexd://" + clientUrl);
  if (userName != null) {
    dataSource.setUsername(userName);
    dataSource.setPassword(password);
  }

  DdlToDatabaseTask toDBTask = new DdlToDatabaseTask();
  toDBTask.addConfiguredDatabase(dataSource);
  File schemaFile = new File(schemaFileName);
  toDBTask.setSchemaFile(schemaFile);

  WriteSchemaToDatabaseCommand writeSchemaToDB =
      new WriteSchemaToDatabaseCommand();
  writeSchemaToDB.setAlterDatabase(true);
  writeSchemaToDB.setFailOnError(true);

  toDBTask.addWriteSchemaToDatabase(writeSchemaToDB);
  toDBTask.execute();

  WriteDataToDatabaseCommand writeDataToDB = new WriteDataToDatabaseCommand();
  File dataFile = new File(dataFileName);
  writeDataToDB.setIsolationLevel(Connection.TRANSACTION_READ_COMMITTED);
  writeDataToDB.setUseBatchMode(true);
  writeDataToDB.setBatchSize(1000);
  writeDataToDB.setDataFile(dataFile);
  writeDataToDB.setFailOnError(true);

  // next check for the success case
  toDBTask = new DdlToDatabaseTask();
  toDBTask.addConfiguredDatabase(dataSource);
  toDBTask.setSchemaFile(schemaFile);

  toDBTask.addWriteDataToDatabase(writeDataToDB);
  toDBTask.execute();

  dataSource.close();

  // end load the tables using the XML data
}
 
Example 17
Source File: DbcpBean.java    From DistributedCrawler with Apache License 2.0 4 votes vote down vote up
/** 关闭数据源 */
protected static void shutdownDataSource() throws SQLException {
	BasicDataSource bds = (BasicDataSource) DS;
	bds.close();
	LOG.info("数据源正常关闭");
}