Пример #1
0
  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();
  }
Пример #2
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);
    }
  }