Java Code Examples for org.skife.jdbi.v2.Handle#close()

The following examples show how to use org.skife.jdbi.v2.Handle#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: ResultSetMapperFactoryTest.java    From SimpleFlatMapper with MIT License 6 votes vote down vote up
@Test
public void testMapToDbObject() throws Exception {
    DBI dbi = new DBI(DbHelper.getHsqlDataSource());
    dbi.registerMapper(new SfmResultSetMapperFactory());
    Handle handle = dbi.open();
    try {
        DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).first();
        DbHelper.assertDbObjectMapping(dbObject);

        SfmBindTest.SfmBindExample attach = handle.attach(SfmBindTest.SfmBindExample.class);
        attach.insert(DbObject.newInstance());
        assertTrue(handle.createQuery("select * from TEST_DB_OBJECT").mapTo(DbObject.class).list().size() > 1);
    } finally {
        handle.close();
    }
}
 
Example 2
Source File: JdbiITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {
  Driver.load();
  final DBI dbi = new DBI("jdbc:h2:mem:dbi", "sa", "");
  final Handle handle = dbi.open();
  handle.execute("CREATE TABLE employer (id INTEGER)");
  handle.close();

  TestUtil.checkSpan(new ComponentSpanCount("java-jdbc", 2));
}
 
Example 3
Source File: PostgresStorage.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<RepairRun> deleteRepairRun(UUID id) {
  RepairRun result = null;
  Handle handle = null;
  try {
    handle = jdbi.open();
    handle.begin();
    IStoragePostgreSql pg = getPostgresStorage(handle);
    RepairRun runToDelete = pg.getRepairRun(UuidUtil.toSequenceId(id));
    if (runToDelete != null) {
      int segmentsRunning
          = pg.getSegmentAmountForRepairRunWithState(UuidUtil.toSequenceId(id), RepairSegment.State.RUNNING);
      if (segmentsRunning == 0) {
        pg.deleteRepairSegmentsForRun(UuidUtil.toSequenceId(runToDelete.getId()));
        pg.deleteRepairRun(UuidUtil.toSequenceId(id));
        result = runToDelete.with()
            .runState(RepairRun.RunState.DELETED)
            .endTime(DateTime.now())
            .build(id);
      } else {
        LOG.warn("not deleting RepairRun \"{}\" as it has segments running: {}", id, segmentsRunning);
      }
    }
    handle.commit();
  } catch (DBIException ex) {
    LOG.warn("DELETE failed", ex);
    if (handle != null) {
      handle.rollback();
    }
  } finally {
    if (handle != null) {
      handle.close();
    }
  }
  return Optional.ofNullable(result);
}
 
Example 4
Source File: AlarmDAOImpl.java    From monasca-thresh with Apache License 2.0 5 votes vote down vote up
@Override
public void addAlarmedMetric(String alarmId, MetricDefinitionAndTenantId metricDefinition) {
  Handle h = db.open();
  try {
    h.begin();
    createAlarmedMetric(h, metricDefinition, alarmId);
    h.commit();
  } catch (RuntimeException e) {
    h.rollback();
    throw e;
  } finally {
    h.close();
  }
}
 
Example 5
Source File: AlarmDAOImpl.java    From monasca-thresh with Apache License 2.0 5 votes vote down vote up
@Override
public void createAlarm(Alarm alarm) {
  Handle h = db.open();
  try {
    String timestamp = formatDateFromMillis(System.currentTimeMillis());
    h.begin();
    h.insert(
        "insert into alarm (id, alarm_definition_id, state, state_updated_at, created_at, updated_at) values (?, ?, ?, ?, ?, ?)",
        alarm.getId(), alarm.getAlarmDefinitionId(), alarm.getState().toString(), timestamp,
            timestamp, timestamp);

    for (final SubAlarm subAlarm : alarm.getSubAlarms()) {
      h.insert(
          "insert into sub_alarm (id, alarm_id, sub_expression_id, expression, state, created_at, updated_at) values (?, ?, ?, ?, ?, ?, ?)",
          subAlarm.getId(), subAlarm.getAlarmId(), subAlarm.getAlarmSubExpressionId(), subAlarm
              .getExpression().getExpression(), subAlarm.getState().toString(), timestamp, timestamp);
    }
    for (final MetricDefinitionAndTenantId md : alarm.getAlarmedMetrics()) {
      createAlarmedMetric(h, md, alarm.getId());
    }
    h.commit();
  } catch (RuntimeException e) {
    h.rollback();
    throw e;
  } finally {
    h.close();
  }
}
 
Example 6
Source File: SfmBindTest.java    From SimpleFlatMapper with MIT License 3 votes vote down vote up
@Test
public void testBindDbObject() throws Exception {
    DBI dbi = new DBI(DbHelper.getHsqlDataSource());
    //dbi.registerMapper(new SfmResultSetMapperFactory());

    Handle handle = dbi.open();

    try {
        SfmBindExample attach = handle.attach(SfmBindExample.class);

        DbObject dbObject1 = DbObject.newInstance();
        DbObject dbObject2 = DbObject.newInstance();
        checkObjectNotThere(handle, dbObject1);
        checkObjectNotThere(handle, dbObject2);

        attach.insert(dbObject1);

        checkObjectInserted(attach, handle, dbObject1);

        attach.insert(dbObject2);

        checkObjectInserted(attach, handle, dbObject2);


    } finally {
        handle.close();
    }
}