org.jdbi.v3.core.Handle.begin()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(6.9k)|赞(0)|评价(0)|浏览(125)

本文整理了Java中org.jdbi.v3.core.Handle.begin()方法的一些代码示例,展示了Handle.begin()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Handle.begin()方法的具体详情如下:
包路径:org.jdbi.v3.core.Handle
类名称:Handle
方法名:begin

Handle.begin介绍

[英]Start a transaction.
[中]开始一项交易。

代码示例

代码示例来源:origin: jdbi/jdbi

/**
 * Begins a transaction.
 *
 * @throws TransactionException if called on an on-demand Transactional instance.
 */
default void begin() {
  getHandle().begin();
}

代码示例来源:origin: jdbi/jdbi

@Override
public <R, X extends Exception> R inTransaction(Handle handle,
                        HandleCallback<R, X> callback) throws X {
  if (isInTransaction(handle)) {
    throw new IllegalStateException("Already in transaction");
  }
  didTxnRollback.set(false);
  final R returnValue;
  try {
    handle.begin();
    returnValue = callback.withHandle(handle);
    if (!didTxnRollback.get()) {
      handle.commit();
    }
  } catch (Throwable e) {
    try {
      handle.rollback();
    } catch (Exception rollback) {
      e.addSuppressed(rollback);
    }
    throw e;
  }
  didTxnRollback.remove();
  return returnValue;
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testCloseWithOpenTransaction() {
  h.begin();
  assertThatThrownBy(h::close).isInstanceOf(TransactionException.class);
  assertThat(h.isClosed()).isTrue();
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testDoubleOpen() throws Exception {
  assertThat(h.getConnection().getAutoCommit()).isTrue();
  h.begin();
  h.begin();
  assertThat(h.getConnection().getAutoCommit()).isFalse();
  h.commit();
  assertThat(h.getConnection().getAutoCommit()).isTrue();
}

代码示例来源:origin: jooby-project/jooby

@Override public void handle(final Request req, final Response rsp, final Route.Chain chain)
   throws Throwable {
  logger.debug("open handle");
  Handle handle = jdbi.open();
  Optional.ofNullable(trx.configurer).ifPresent(c -> c.accept(handle));
  req.set(trx.handle(), handle);
  logger.debug("new transaction: {}", handle);
  handle.begin();
  rsp.after(new CommitTransaction(handle));
  rsp.complete(new RollbackTransaction(handle));
  rsp.complete(new CloseHandle(handle));
  chain.next(req, rsp);
 }
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testNestedTransactionsCollapseIntoSingleTransaction() {
  Handle handle = Mockito.spy(dbRule.getSharedHandle());
  Dao dao = handle.attach(Dao.class);
  dao.threeNestedTransactions();
  verify(handle, times(1)).begin();
  verify(handle, times(1)).commit();
  dao.twoNestedTransactions();
  verify(handle, times(2)).begin();
  verify(handle, times(2)).commit();
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testSqlUpdateWithTransaction() {
  Handle handle = Mockito.spy(dbRule.getSharedHandle());
  Dao dao = handle.attach(Dao.class);
  dao.insert(1, "foo");
  verify(handle, never()).begin();
  assertThat(dao.findById(1)).isEqualTo(new Something(1, "foo"));
  assertThat(dao.insertTransactional(2, "bar")).isEqualTo(1);
  verify(handle, times(1)).begin();
  assertThat(dao.findById(2)).isEqualTo(new Something(2, "bar"));
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testNestedTransactionWithSameIsolation() {
  Handle handle = Mockito.spy(dbRule.getSharedHandle());
  Dao dao = handle.attach(Dao.class);
  dao.nestedTransactionWithSameIsolation();
  verify(handle, times(1)).begin();
  verify(handle, times(1)).commit();
}

代码示例来源:origin: jdbi/jdbi

h.begin();
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
  h.execute(SAMPLE_SQL, 1L, "Tom");

代码示例来源:origin: jdbi/jdbi

@Test
public void testCloseWithOpenTransactionCheckDisabled() {
  h.getConfig(Handles.class).setForceEndTransactions(false);
  h.begin();
  h.close();
  assertThat(h.isClosed()).isTrue();
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testSavepoint() {
  h.begin();
  h.execute("insert into something (id, name) values (?, ?)", 1, "Tom");
  h.savepoint("first");
  h.execute("insert into something (id, name) values (?, ?)", 2, "Martin");
  assertThat(h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly())
      .isEqualTo(Integer.valueOf(2));
  h.rollbackToSavepoint("first");
  assertThat(h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly())
      .isEqualTo(Integer.valueOf(1));
  h.commit();
  assertThat(h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly())
      .isEqualTo(Integer.valueOf(1));
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testJustJdbiTransactions() {
  try (Handle h1 = db.open();
     Handle h2 = db.open()) {
    h1.execute("insert into something (id, name) values (8, 'Mike')");
    h1.begin();
    h1.execute("update something set name = 'Miker' where id = 8");
    assertThat(h2.createQuery("select name from something where id = 8")
        .mapTo(String.class)
        .findOnly())
        .isEqualTo("Mike");
    h1.commit();
  }
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testReleaseSavepoint() {
  h.begin();
  h.savepoint("first");
  h.execute("insert into something (id, name) values (?, ?)", 1, "Martin");
  h.release("first");
  assertThatExceptionOfType(TransactionException.class)
      .isThrownBy(() -> h.rollbackToSavepoint("first"));
  h.rollback();
}

代码示例来源:origin: org.jdbi/jdbi3

@SuppressWarnings("unchecked")
@Override
public <R, X extends Exception> R inTransaction(Handle handle,
                        HandleCallback<R, X> callback) throws X
{
  if (isInTransaction(handle)) {
    throw new IllegalStateException("Already in transaction");
  }
  didTxnRollback.set(false);
  final R returnValue;
  try {
    handle.begin();
    returnValue = callback.withHandle(handle);
    if (!didTxnRollback.get()) {
      handle.commit();
    }
  }
  catch (Exception e) {
    try {
      handle.rollback();
    } catch (Exception rollback) {
      e.addSuppressed(rollback);
    }
    throw (X) e;
  }
  didTxnRollback.remove();
  return returnValue;
}

代码示例来源:origin: org.jdbi/jdbi3-sqlobject

@Test
public void testNestedTransactionsCollapseIntoSingleTransaction() {
  Handle handle = Mockito.spy(dbRule.getSharedHandle());
  Dao dao = handle.attach(Dao.class);
  dao.threeNestedTransactions();
  verify(handle, times(1)).begin();
  verify(handle, times(1)).commit();
  dao.twoNestedTransactions();
  verify(handle, times(2)).begin();
  verify(handle, times(2)).commit();
}

代码示例来源:origin: org.jdbi/jdbi3-sqlobject

@Test
public void testSqlUpdateWithTransaction() {
  Handle handle = Mockito.spy(dbRule.getSharedHandle());
  Dao dao = handle.attach(Dao.class);
  dao.insert(1, "foo");
  verify(handle, never()).begin();
  assertThat(dao.findById(1)).isEqualTo(new Something(1, "foo"));
  assertThat(dao.insertTransactional(2, "bar")).isEqualTo(1);
  verify(handle, times(1)).begin();
  assertThat(dao.findById(2)).isEqualTo(new Something(2, "bar"));
}

代码示例来源:origin: org.jdbi/jdbi3-sqlobject

@Test
public void testNestedTransactionWithSameIsolation() {
  Handle handle = Mockito.spy(dbRule.getSharedHandle());
  Dao dao = handle.attach(Dao.class);
  dao.nestedTransactionWithSameIsolation();
  verify(handle, times(1)).begin();
  verify(handle, times(1)).commit();
}

代码示例来源:origin: org.jdbi/jdbi3-sqlobject

@Test
public void testJustJdbiTransactions() {
  try (Handle h1 = db.open();
     Handle h2 = db.open()) {
    h1.execute("insert into something (id, name) values (8, 'Mike')");
    h1.begin();
    h1.execute("update something set name = 'Miker' where id = 8");
    assertThat(h2.createQuery("select name from something where id = 8")
        .mapTo(String.class)
        .findOnly())
        .isEqualTo("Mike");
    h1.commit();
  }
}

相关文章