@Before
  public void setUp() throws InterruptedException {
    jdbcDataFetcher =
        new JdbcDataFetcher(
            derbyConnectorRule.getMetadataConnectorConfig(),
            "tableName",
            "keyColumn",
            "valueColumn",
            100);

    handle = derbyConnectorRule.getConnector().getDBI().open();
    Assert.assertEquals(
        0,
        handle
            .createStatement(
                String.format(
                    "CREATE TABLE %s (%s VARCHAR(64), %s VARCHAR(64))",
                    tableName, keyColumn, valueColumn))
            .setQueryTimeout(1)
            .execute());
    handle
        .createStatement(String.format("TRUNCATE TABLE %s", tableName))
        .setQueryTimeout(1)
        .execute();

    for (Map.Entry<String, String> entry : lookupMap.entrySet()) {
      insertValues(entry.getKey(), entry.getValue(), handle);
    }
    handle.commit();
  }
 private void insertValues(final String key, final String val, Handle handle) {
   final String query;
   handle
       .createStatement(String.format("DELETE FROM %s WHERE %s='%s'", tableName, keyColumn, key))
       .setQueryTimeout(1)
       .execute();
   query =
       String.format(
           "INSERT INTO %s (%s, %s) VALUES ('%s', '%s')",
           tableName, keyColumn, valueColumn, key, val);
   Assert.assertEquals(1, handle.createStatement(query).setQueryTimeout(1).execute());
   handle.commit();
 }
  public void testJustJdbiTransactions() throws Exception {
    Handle h1 = dbi.open();
    Handle h2 = dbi.open();

    h1.execute("insert into something (id, name) values (8, 'Mike')");

    h1.begin();
    h1.execute("update something set name = 'Miker' where id = 8");

    assertEquals(
        "Mike",
        h2.createQuery("select name from something where id = 8").map(StringMapper.FIRST).first());
    h1.commit();
    h1.close();
    h2.close();
  }
Exemple #4
0
  public Team insertWithTxHandle(final Team team) {
    // in this case we use an explicit handle, and attach the dao's to the same handle (connection)
    try (Handle handle = jdbiHelper.getTxHandle()) {
      handle.begin();
      TeamDao teamDao = handle.attach(TeamDao.class);
      TeamPersonDao teamPersonDao =
          handle.attach(TeamPersonDao.class); // team->person mapping table

      long teamId;
      if (team.getPointOfContact() != null) {
        teamId = teamDao.insertWithPoC(team);
      } else {
        teamId = teamDao.insertWithoutPoC(team);
      }
      for (Person p : team.getMembers()) {
        // update the team->person mapping table
        teamPersonDao.insert(new TeamPerson(teamId, p.getId()));
      }
      // add test code for checking that TX is handled correctly
      checkIfTxShouldBeRolledBack(team);
      handle.commit();
      return get(teamId);
    }
  }