简体   繁体   English

"Jdbi3 是否保证每种异常类型的事务回滚?"

[英]Does Jdbi3 guarantee rollback on transaction for every type of Exception?

I have a code like this one:我有这样的代码:

jdbi.inTransaction(h -> {
            
            Dao1 dao1 = h.attach(Dao1.class);
            
            if(!dao1.somequery()) {
                
                throw new CustomException("foobar");
            }

            // ... other statements
}

Can I be sure that if CustomException is thrown jdbi will rollback the transaction or this happen only with SQLException or Jdbi related Exceptions ?我可以确定如果CustomException被抛出jdbi将回滚事务,或者这只发生在SQLException或 Jdbi 相关的异常中吗?

Introduction介绍

Let's consider Jdbi 3.27.0.让我们考虑一下 Jdbi 3.27.0。

Documentation文档

Please, see the Javadoc for the org.jdbi.v3.core.Jdbi#inTransaction(org.jdbi.v3.core.HandleCallback<R,X>) method:请参阅org.jdbi.v3.core.Jdbi#inTransaction(org.jdbi.v3.core.HandleCallback<R,X>)方法的 Javadoc

inTransaction交易中

public <R, X extends Exception > R inTransaction ( HandleCallback <R,X> callback) throws X public <R, X extends Exception > R inTransaction ( HandleCallback <R,X> callback) throws X

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.当回调被调用时,句柄将在一个事务中,如果回调正常完成,则该事务将被提交,如果回调引发异常,则该事务将回滚。

Type Parameters:类型参数:

R - type returned by the callback R - 回调返回的类型

X - exception type thrown by the callback, if any. X - 回调抛出的异常类型,如果有的话。

Parameters:参数:

callback - A callback which will receive an open Handle, in a transaction callback - 在事务中接收打开句柄的回调

Returns:回报:

the value returned by callback回调返回的值

Throws:抛出:

X - any exception thrown by the callback X - 回调抛出的任何异常

Please, note the text marked in bold, which should answer your question:请注意以粗体标记的文本,它应该回答您的问题:

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 .当回调被调用时,句柄将在一个事务中,如果回调正常完成,则该事务将被提交,如果回调引发异常,则该事务将回滚

Throws:抛出:

X - any exception thrown by the callback X -回调抛出的任何异常

As we can see, there is no statement claiming that the exception handling behavior depends on the exception type.正如我们所看到的,没有声明声称异常处理行为取决于异常类型。

Source code源代码

Please, consider the information provided in this section as a draft: it may be rough (imprecise).请将本节中提供的信息视为草稿:它可能很粗略(不精确)。

There are tests that cover the transaction-related functionality.有一些测试涵盖了与交易相关的功能。
The tests are represented by the TestTransactions class: jdbi/TestTransactions.java at v3.27.0 · jdbi/jdbi .测试由TestTransactions类表示: jdbi/TestTransactions.java at v3.27.0 · jdbi/jdbi

Please, take a look at the entire test class.请看一下整个测试班。

Then, please, note the following exception-related test methods:那么,请注意以下与异常相关的测试方法:

  1. testExceptionAbortsTransaction() . testExceptionAbortsTransaction()
  2. testExceptionAbortsUseTransaction() . testExceptionAbortsUseTransaction()

As we can examine from the implementations of these test methods :我们可以从这些测试方法的实现中检查:

  1. The implementations use the java.io.IOException exception type.实现使用java.io.IOException异常类型。
  2. The implementations does not mention that the exception handling behavior depends on the exception type.实现没有提到异常处理行为取决于异常类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM