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

The following examples show how to use org.apache.commons.dbcp2.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: SpringTest.java    From java-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void spring() throws SQLException {
  BasicDataSource dataSource = getDataSource(false);

  JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  jdbcTemplate.execute("CREATE TABLE employee (id INTEGER)");

  dataSource.close();

  List<MockSpan> finishedSpans = mockTracer.finishedSpans();
  assertEquals(DB_CONNECTION_SPAN_COUNT + 1, finishedSpans.size());
  MockSpan mockSpan = finishedSpans.get(DB_CONNECTION_SPAN_COUNT);

  assertEquals(Tags.SPAN_KIND_CLIENT, mockSpan.tags().get(Tags.SPAN_KIND.getKey()));
  assertEquals(JdbcTracingUtils.COMPONENT_NAME, mockSpan.tags().get(Tags.COMPONENT.getKey()));
  assertThat(mockSpan.tags().get(Tags.DB_STATEMENT.getKey()).toString()).isNotEmpty();
  assertEquals("h2", mockSpan.tags().get(Tags.DB_TYPE.getKey()));
  assertEquals("spring", mockSpan.tags().get(Tags.DB_INSTANCE.getKey()));
  assertEquals("localhost:-1", mockSpan.tags().get("peer.address"));

  assertEquals(0, mockSpan.generatedErrors().size());

  assertNull(mockTracer.activeSpan());
  checkNoEmptyTags(finishedSpans);
}
 
Example 2
Source File: SpringTest.java    From java-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void spring_with_parent() throws Exception {
  final MockSpan parent = mockTracer.buildSpan("parent").start();
  try (Scope ignored = mockTracer.activateSpan(parent)) {
    BasicDataSource dataSource = getDataSource(false);

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.execute("CREATE TABLE with_parent_1 (id INTEGER)");
    jdbcTemplate.execute("CREATE TABLE with_parent_2 (id INTEGER)");

    dataSource.close();
  }
  parent.finish();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(DB_CONNECTION_SPAN_COUNT + 3, spans.size());

  checkSameTrace(spans);
  checkNoEmptyTags(spans);
}
 
Example 3
Source File: SpringTest.java    From java-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void spring_with_parent_and_active_span_only() throws Exception {
  final MockSpan parent = mockTracer.buildSpan("parent").start();
  try (Scope ignored = mockTracer.activateSpan(parent)) {
    BasicDataSource dataSource = getDataSource(true);

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.execute("CREATE TABLE with_parent_skip_1 (id INTEGER)");
    jdbcTemplate.execute("CREATE TABLE with_parent_skip_2 (id INTEGER)");

    dataSource.close();
  }
  parent.finish();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(DB_CONNECTION_SPAN_COUNT + 3, spans.size());

  checkSameTrace(spans);
  checkNoEmptyTags(spans);
}
 
Example 4
Source File: SpringTest.java    From java-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void spring_with_ignored_statement() throws Exception {
  BasicDataSource dataSource = getDataSource(false, Arrays.asList(
      "CREATE TABLE ignored (id INTEGER, TEST VARCHAR)",
      "INSERT INTO ignored (id, \\\"TEST\\\") VALUES (1, 'value')"
  ));

  JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  jdbcTemplate.execute("CREATE TABLE ignored (id INTEGER, TEST VARCHAR)");
  jdbcTemplate.execute("INSERT INTO ignored (id, \"TEST\") VALUES (1, 'value')");
  jdbcTemplate.execute("CREATE TABLE not_ignored (id INTEGER)");

  dataSource.close();

  List<MockSpan> finishedSpans = mockTracer.finishedSpans();
  assertEquals(DB_CONNECTION_SPAN_COUNT + 1, finishedSpans.size());
  checkNoEmptyTags(finishedSpans);
}
 
Example 5
Source File: SpringTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void batch() throws SQLException {
  BasicDataSource dataSource = getDataSource(false);

  JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  jdbcTemplate.execute("CREATE TABLE batch (id INTEGER)");

  final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5);
  jdbcTemplate.batchUpdate("INSERT INTO batch (id) VALUES (?)",
      new BatchPreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
          preparedStatement.setInt(1, ids.get(i));
        }

        @Override
        public int getBatchSize() {
          return ids.size();
        }
      }
  );

  dataSource.close();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(DB_CONNECTION_SPAN_COUNT + 2, spans.size());

  for (MockSpan span : spans.subList(DB_CONNECTION_SPAN_COUNT, spans.size() - 1)) {
    assertEquals(Tags.SPAN_KIND_CLIENT, span.tags().get(Tags.SPAN_KIND.getKey()));
    assertEquals(JdbcTracingUtils.COMPONENT_NAME, span.tags().get(Tags.COMPONENT.getKey()));
    assertThat(span.tags().get(Tags.DB_STATEMENT.getKey()).toString()).isNotEmpty();
    assertEquals("h2", span.tags().get(Tags.DB_TYPE.getKey()));
    assertEquals("spring", span.tags().get(Tags.DB_INSTANCE.getKey()));
    assertEquals("localhost:-1", span.tags().get("peer.address"));
    assertEquals(0, span.generatedErrors().size());
  }

  assertNull(mockTracer.activeSpan());
  checkNoEmptyTags(spans);
}
 
Example 6
Source File: SpringTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void spring_active_span_only() throws Exception {
  BasicDataSource dataSource = getDataSource(true);

  JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  jdbcTemplate.execute("CREATE TABLE skip_new_spans (id INTEGER)");

  dataSource.close();

  List<MockSpan> finishedSpans = mockTracer.finishedSpans();
  assertEquals(0, finishedSpans.size());
  checkNoEmptyTags(finishedSpans);
}
 
Example 7
Source File: DataSourceDecorator.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Override
public void afterAll(final TestInvocation invocation) throws Exception {
    final ExecutionContext context = invocation.getContext();
    final BasicDataSource ds = (BasicDataSource) context.getData(Constants.KEY_DATA_SOURCE);
    ds.close();
    context.storeData(Constants.KEY_DATA_SOURCE, null);
}
 
Example 8
Source File: ConfigTestUtils.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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.
 */
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();
        }
    }
}
 
Example 9
Source File: DBCPDataSourceServiceImporter.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
@Override
protected void passivateDataSourcePool( BasicDataSource dataSourcePool )
        throws Exception
{
    dataSourcePool.close();
}
 
Example 10
Source File: BasicDataSourceExample.java    From commons-dbcp with Apache License 2.0 4 votes vote down vote up
public static void shutdownDataSource(DataSource ds) throws SQLException {
    BasicDataSource bds = (BasicDataSource) ds;
    bds.close();
}