spring事务管理_02: 事务管理setting: read-only, read/write, rollback触发条件, 默认配置...

技术文档

16.5.5<tx:advice/> settings

事务:read/write, read-only

First of all, since Spring doesn't do persistence itself, it cannot specify what readOnly should exactly mean. This attribute is only a hint to the provider, the behavior depends on, in this case, Hibernate.
首先,由于Spring本身不执行持久性,因此无法指定readOnly应该具有什么意义。这个属性只是对提供者的一个提示,行为依赖于,在这种情况下,Hibernate。

If you specify readOnly as true, the flush mode will be set as FlushMode.NEVER in the current Hibernate Session preventing the session from committing the transaction.
如果将readOnly指定为true,则刷新模式将在当前Hibernate会话中设置为FlushMode.NEVER,以防止会话提交事务。

Furthermore, setReadOnly(true) will be called on the JDBC Connection, which is also a hint to the underlying database. If your database supports it (most likely it does), this has basically the same effect as FlushMode.NEVER, but it's stronger since you cannot even flush manually.

此外,将在JDBC Connection上调用setReadOnly(true),这也是底层数据库的提示。如果您的数据库支持它(很可能它),它与FlushMode.NEVER具有基本相同的效果,但它更强大,因为您甚至无法手动刷新。

Now let's see how transaction propagation works.
现在让我们看看事务传播是如何工作的。

If you don't explicitly set readOnly to true, you will have read/write transactions. Depending on the transaction attributes (like REQUIRES_NEW), sometimes your transaction is suspended at some point, a new one is started and eventually committed, and after that the first transaction is resumed.
如果未将readOnly显式设置为true,则将具有读/写事务。根据事务属性(如REQUIRES_NEW),有时您的事务会在某个时刻暂停,新的事务将被启动并最终提交,之后将恢复第一个事务。

OK, we're almost there. Let's see what brings readOnly into this scenario.

好的,我们快到了。让我们看看是什么让readOnly进入这个场景。

If a method in a read/write transaction calls a method that requires a readOnly transaction, the first one should be suspended, because otherwise a flush/commit would happen at the end of the second method.
如果读/写事务中的方法调用需要readOnly事务的方法,则应该挂起第一个,否则在第二个方法结束时会发生刷新/提交。

Conversely, if you call a method from within a readOnly transaction that requires read/write, again, the first one will be suspended, since it cannot be flushed/committed, and the second method needs that.

相反,如果从需要读/写的readOnly事务中调用方法,则第一个方法将被挂起,因为它不能被刷新/提交,第二个方法需要它。

In the readOnly-to-readOnly, and the read/write-to-read/write cases the outer transaction doesn't need to be suspended (unless you specify propagation otherwise, obviously).

在readOnly-to-readOnly和read / write-to-read / write情况下,不需要暂停外部事务(除非你明确指定传播)。

总结: read-only read/write

总的来说还是服务于事务的传播行为的。

事务的传播路径:

[1]
read-only -> read-only

向provider (Hibernate, Jdbc)指明这是个纯查询,

决定是否以非事务的方式进行。

[2]
read/write -> read/write

向provider (Hibernate, Jdbc)指明这是个写操作,

要求以事务的方式执行。

[3]
read-only -> read/write

挂起 read-only,
在read/write 事务中刷新数据, 并提交事务。

执行 read-only, 直到结束。

[4]
read/write -> read-only

挂起 read/write 事务,

执行 read-only, 执行完。

继续read/write事务操作,最后提交事务。

会执行rollback的场景。不会执行rollback的场景

16.5.3 Rolling back a declarative transaction

The default <tx:advice/> settings are:

rollback:

Any RuntimeException triggers rollback, and any checked Exception does not.

In its default configuration, the Spring Framework’s transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException. ( Errors will also - by default - result in a rollback). Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration.

在默认配置中,

会被rollback的情况:
RuntimeException,Error,unchecked exceptions

不会被rollback的情况:

Checked exceptions

Checked vs Unchecked Exceptions in Java

  1. Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.

2)Unchecked

In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.

java: exception, error, runtimeException



           | Throwable |
                   +-----------+
                    /         \
           /           \
          +-------+          +-----------+
          | Error |          | Exception |
          +-------+          +-----------+
       /  |  \           / |        \
         \________/   \______/       \
                            +------------------+
    unchecked    checked    | RuntimeException |
                    +------------------+
                      /   |    |      \
                     \_________________/
                       
                       unchecked
                       

你可以选择把事务交给spring管理,你也可以自己管理事务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值