本文整理了Java中org.jdbi.v3.core.Handle.inTransaction()
方法的一些代码示例,展示了Handle.inTransaction()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Handle.inTransaction()
方法的具体详情如下:
包路径:org.jdbi.v3.core.Handle
类名称:Handle
方法名:inTransaction
[英]Executes callback
in a transaction, and returns the result of the callback.
[中]在事务中执行callback
,并返回回调结果。
代码示例来源:origin: jdbi/jdbi
/**
* Executes <code>callback</code> in a transaction.
*
* <p>
* This form accepts a transaction isolation level which will be applied to the connection
* for the scope of this transaction, after which the original isolation level will be restored.
* </p>
* @param level the transaction isolation level which will be applied to the connection for the scope of this
* transaction, after which the original isolation level will be restored.
* @param callback a callback which will receive an open handle, in a transaction.
* @param <X> exception type thrown by the callback, if any
* @throws X any exception thrown by the callback
*/
public <X extends Exception> void useTransaction(TransactionIsolationLevel level, HandleConsumer<X> callback) throws X {
inTransaction(level, handle -> {
callback.useHandle(handle);
return null;
});
}
代码示例来源:origin: jdbi/jdbi
/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients. The handle will be in a transaction when the callback is invoked, and
* that transaction will be committed if the callback finishes normally, or rolled back if the
* callback raises an exception.
*
* @param callback A callback which will receive an open Handle, in a transaction
* @param <R> type returned by the callback
* @param <X> exception type thrown by the callback, if any.
*
* @return the value returned by callback
*
* @throws X any exception thrown by the callback
*/
public <R, X extends Exception> R inTransaction(final HandleCallback<R, X> callback) throws X {
return withHandle(handle -> handle.<R, X>inTransaction(callback));
}
代码示例来源:origin: jdbi/jdbi
private ResultIterator<?> executeBatch(final Handle handle, final PreparedBatch batch) {
if (!handle.isInTransaction() && sqlBatch.transactional()) {
// it is safe to use same prepared batch as the inTransaction passes in the same
// Handle instance.
return handle.inTransaction(c -> batchIntermediate.apply(batch));
} else {
return batchIntermediate.apply(batch);
}
}
代码示例来源:origin: jdbi/jdbi
/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients. The handle will be in a transaction when the callback is invoked, and
* that transaction will be committed if the callback finishes normally, or rolled back if the
* callback raises an exception.
*
* <p>
* This form accepts a transaction isolation level which will be applied to the connection
* for the scope of this transaction, after which the original isolation level will be restored.
* </p>
*
* @param level the transaction isolation level which will be applied to the connection for the scope of this
* transaction, after which the original isolation level will be restored.
* @param callback A callback which will receive an open Handle, in a transaction
* @param <R> type returned by the callback
* @param <X> exception type thrown by the callback, if any.
*
* @return the value returned by callback
*
* @throws X any exception thrown by the callback
*/
public <R, X extends Exception> R inTransaction(final TransactionIsolationLevel level, final HandleCallback<R, X> callback) throws X {
return withHandle(handle -> handle.inTransaction(level, callback));
}
代码示例来源:origin: jdbi/jdbi
/**
* Executes the given callback within a transaction, returning the value returned by the callback.
*
* @param callback the callback to execute
* @param <R> method return type
* @param <X> exception optionally thrown by the callback.
* @return the value returned by the callback.
* @throws X any exception thrown by the callback.
*/
@SuppressWarnings("unchecked")
default <R, X extends Exception> R inTransaction(TransactionalCallback<R, This, X> callback) throws X {
return getHandle().inTransaction(h -> callback.inTransaction((This) this));
}
代码示例来源:origin: jdbi/jdbi
/**
* Executes the given callback within a transaction, returning the value returned by the callback.
*
* @param isolation the transaction isolation level.
* @param callback the callback to execute
* @param <R> method return type
* @param <X> exception optionally thrown by the callback.
* @return the value returned by the callback.
* @throws X any exception thrown by the callback.
*/
@SuppressWarnings("unchecked")
default <R, X extends Exception> R inTransaction(
TransactionIsolationLevel isolation, TransactionalCallback<R, This, X> callback) throws X {
return getHandle().inTransaction(isolation, h -> callback.inTransaction((This) this));
}
代码示例来源:origin: jdbi/jdbi
@Test
public void testCallback() {
String woot = h.inTransaction(x -> "Woot!");
assertThat(woot).isEqualTo("Woot!");
}
代码示例来源:origin: jdbi/jdbi
@Test
public void testThrowingRuntimeExceptionPercolatesOriginal() {
assertThatThrownBy(() -> h.inTransaction(handle -> {
throw new IllegalArgumentException();
})).isInstanceOf(IllegalArgumentException.class);
}
代码示例来源:origin: jdbi/jdbi
@Test
public void testEventuallySucceeds() throws Exception {
final AtomicInteger remaining = new AtomicInteger(MAX_RETRIES / 2);
Handle handle = dbRule.getJdbi().open();
handle.inTransaction(TransactionIsolationLevel.SERIALIZABLE, conn -> {
if (remaining.decrementAndGet() == 0) {
return null;
}
throw new SQLException("serialization", "40001");
});
assertThat(remaining.get()).isZero();
}
代码示例来源:origin: jdbi/jdbi
@Test
public void testExceptionAbortsTransaction() {
assertThatThrownBy(() ->
h.inTransaction(handle -> {
handle.execute("insert into something (id, name) values (?, ?)", 0, "Keith");
throw new IOException();
}))
.isInstanceOf(IOException.class);
List<Something> r = h.createQuery("select * from something").mapToBean(Something.class).list();
assertThat(r).isEmpty();
}
代码示例来源:origin: jdbi/jdbi
@Test
public void testTemplateEngineThrowsError() {
assertThatThrownBy(() -> h.setTemplateEngine(new BoomEngine()).inTransaction(h2 -> h2.execute("select 1")))
.isOfAnyClassIn(Error.class)
.hasMessage("boom");
assertThat(h.isInTransaction()).isFalse();
}
代码示例来源:origin: jdbi/jdbi
}).when(onSuccess).accept(anyList());
dbRule.getJdbi().open().inTransaction(TransactionIsolationLevel.SERIALIZABLE, conn -> {
if (remainingAttempts.decrementAndGet() == 0) {
return null;
代码示例来源:origin: jdbi/jdbi
@Test
public void testEventuallyFails() {
final AtomicInteger attempts = new AtomicInteger(0);
Handle handle = dbRule.getJdbi().open();
assertThatExceptionOfType(SQLException.class)
.isThrownBy(() -> handle.inTransaction(TransactionIsolationLevel.SERIALIZABLE,
conn -> {
attempts.incrementAndGet();
throw new SQLException("serialization", "40001", attempts.get());
}))
.satisfies(e -> assertThat(e.getSQLState()).isEqualTo("40001"))
.satisfies(e -> assertThat(e.getSuppressed())
.hasSize(MAX_RETRIES)
.describedAs("suppressed are ordered reverse chronologically, like a stack")
.isSortedAccordingTo(Comparator.comparing(ex -> ((SQLException) ex).getErrorCode()).reversed()))
.describedAs("thrown exception is chronologically last")
.satisfies(e -> assertThat(e.getErrorCode()).isEqualTo(((SQLException) e.getSuppressed()[0]).getErrorCode() + 1));
assertThat(attempts.get()).isEqualTo(1 + MAX_RETRIES);
}
代码示例来源:origin: jdbi/jdbi
public Optional<User> findUserById(long id) {
return handle.inTransaction(h ->
h.createQuery("SELECT * FROM users WHERE id=:id")
.bind("id", id)
.mapTo(User.class)
.findFirst());
}
// end::simpleTransaction[]
代码示例来源:origin: jdbi/jdbi
@Test
public void testInTransaction() {
Handle h = dbRule.openHandle();
String value = h.inTransaction(handle -> {
handle.execute("insert into something (id, name) values (1, 'Brian')");
return handle.createQuery("select name from something where id = 1").mapToBean(Something.class).findOnly().getName();
});
assertThat(value).isEqualTo("Brian");
}
代码示例来源:origin: jdbi/jdbi
@Test
public void testSillyNumberOfCallbacks() throws Exception {
try (Handle h = dbRule.openHandle()) {
h.execute("insert into something (id, name) values (1, 'Keith')");
}
// strangely enough, the compiler can't infer this and thinks the throws is redundant
String value = dbRule.getJdbi().<String, Exception>withHandle(handle ->
handle.inTransaction(handle1 ->
handle1.createQuery("select name from something where id = 1").mapTo(String.class).findOnly()));
assertThat(value).isEqualTo("Keith");
}
代码示例来源:origin: jdbi/jdbi
return h.inTransaction(isolation, callback);
} finally {
if (flipReadOnly) {
代码示例来源:origin: org.jdbi/jdbi3
/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients. The handle will be in a transaction when the callback is invoked, and
* that transaction will be committed if the callback finishes normally, or rolled back if the
* callback raises an exception.
*
* @param callback A callback which will receive an open Handle, in a transaction
* @param <R> type returned by the callback
* @param <X> exception type thrown by the callback, if any.
*
* @return the value returned by callback
*
* @throws X any exception thrown by the callback
*/
public <R, X extends Exception> R inTransaction(final HandleCallback<R, X> callback) throws X
{
return withHandle(handle -> handle.<R, X>inTransaction(callback));
}
代码示例来源:origin: org.jdbi/jdbi3
/**
* Executes <code>callback</code> in a transaction.
*
* <p>
* This form accepts a transaction isolation level which will be applied to the connection
* for the scope of this transaction, after which the original isolation level will be restored.
* </p>
* @param level the transaction isolation level which will be applied to the connection for the scope of this
* transaction, after which the original isolation level will be restored.
* @param callback a callback which will receive an open handle, in a transaction.
* @param <X> exception type thrown by the callback, if any
* @throws X any exception thrown by the callback
*/
public <X extends Exception> void useTransaction(TransactionIsolationLevel level, HandleConsumer<X> callback) throws X {
inTransaction(level, handle -> {
callback.useHandle(handle);
return null;
});
}
代码示例来源:origin: org.jdbi/jdbi3
/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients. The handle will be in a transaction when the callback is invoked, and
* that transaction will be committed if the callback finishes normally, or rolled back if the
* callback raises an exception.
*
* <p>
* This form accepts a transaction isolation level which will be applied to the connection
* for the scope of this transaction, after which the original isolation level will be restored.
* </p>
*
* @param level the transaction isolation level which will be applied to the connection for the scope of this
* transaction, after which the original isolation level will be restored.
* @param callback A callback which will receive an open Handle, in a transaction
* @param <R> type returned by the callback
* @param <X> exception type thrown by the callback, if any.
*
* @return the value returned by callback
*
* @throws X any exception thrown by the callback
*/
public <R, X extends Exception> R inTransaction(final TransactionIsolationLevel level, final HandleCallback<R, X> callback) throws X
{
return withHandle(handle -> handle.<R, X>inTransaction(level, callback));
}
内容来源于网络,如有侵权,请联系作者删除!