@Test
  public void testTxActuallyCommits() throws Exception {
    Handle h2 = dbi.open();
    Dao one = handle.attach(Dao.class);
    Dao two = h2.attach(Dao.class);

    // insert in @Transaction method
    Something inserted = one.insertAndFetch(1, "Brian");

    // fetch from another connection
    Something fetched = two.findById(1);
    assertThat(fetched, equalTo(inserted));
  }
 @Test
 public void testTransmogrifiable() throws Exception {
   Hobbsian h = handle.attach(Hobbsian.class);
   h.insert(2, "Cora");
   Something s = h.become(TransactionStuff.class).byId(2);
   assertThat(s, equalTo(new Something(2, "Cora")));
 }
  @Test
  public void testNestedTransactions() throws Exception {
    Dao dao = handle.attach(Dao.class);

    Something s = dao.insertAndFetchWithNestedTransaction(1, "Ian");
    assertThat(s, equalTo(new Something(1, "Ian")));
  }
 @Test
 public void testSimpleCreate() throws Exception {
   Foo foo = handle.attach(Foo.class);
   foo.insert(1, "Stephane");
   Something s = foo.createBar().findById(1);
   assertThat(s, equalTo(new Something(1, "Stephane")));
 }
  @BeforeMethod
  public void setup() throws Exception {
    H2EmbeddedDataSourceConfig dataSourceConfig =
        new H2EmbeddedDataSourceConfig().setFilename("mem:");
    DataSource dataSource = new H2EmbeddedDataSource(dataSourceConfig);
    DBI h2Dbi = new DBI(dataSource);
    handle = h2Dbi.open();
    dao = handle.attach(ShardManagerDao.class);

    ShardManagerDao.Utils.createShardTablesWithRetry(dao);
  }
  @Test
  public void testTxFail() throws Exception {
    Dao dao = handle.attach(Dao.class);

    try {
      dao.failed(1, "Ian");
      fail("should have raised exception");
    } catch (TransactionFailedException e) {
      assertThat(e.getCause().getMessage(), equalTo("woof"));
    }
    assertThat(dao.findById(1), nullValue());
  }
  public boolean insert(Template template) {
    if (template == null) {
      return false;
    }

    Handle handle = dbi.open();
    try {
      TemplateDao db = handle.attach(TemplateDao.class);
      return db.insertBean(template) > 0;
    } finally {
      handle.close();
    }
  }
 /**
  * @param entitySqlDaoTransactionWrapper transaction to execute
  * @param <ReturnType> object type to return from the transaction
  * @return result from the transaction fo type ReturnType
  */
 public <ReturnType> ReturnType execute(
     final EntitySqlDaoTransactionWrapper<ReturnType> entitySqlDaoTransactionWrapper) {
   final Handle handle = dbi.open();
   try {
     final EntitySqlDao<EntityModelDao<Entity>, Entity> entitySqlDao =
         handle.attach(InitialEntitySqlDao.class);
     return entitySqlDao.inTransaction(
         TransactionIsolationLevel.READ_COMMITTED,
         new JdbiTransaction<ReturnType, EntityModelDao<Entity>, Entity>(
             handle, entitySqlDaoTransactionWrapper));
   } finally {
     handle.close();
   }
 }
Exemple #9
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);
    }
  }
 public DatabaseProjectControlStore(Handle handle, int siteId) {
   this.handle = handle;
   this.siteId = siteId;
   this.dao = handle.attach(Dao.class);
 }
 @Test
 public void testInsertAndFind() throws Exception {
   Foo foo = handle.attach(Foo.class);
   Something s = foo.insertAndFind(1, "Stephane");
   assertThat(s, equalTo(new Something(1, "Stephane")));
 }