问题描述
在关于Spring / Hibernate事务的演讲中,我提出了一个意见,即同步关键字在一个方法上,而 @Transactional $ c $在逻辑上有许多相似之处。果然它们是完全不同的野兽,但它们都作为方面应用于方法,并通过某种共享监视器控制对某些资源的访问(例如,以db为单位的记录)。
有些人立即反对并声称我的比较是致命的错误。我不记得具体的论点,但我也可以在这里看到一些观点。例如, synchronized 从一开始就适用于整个方法,事务只有在达到访问数据库的语句时才有效。 Plus synchronized 不提供任何读/写锁定模式。
有点更新。
<显然我的问题听起来像是比较数据库事务与在Java中输入 synchronized 方法。事实并非如此。我的想法更多的是比较 @Transactional 和 synchronized 的语义中的相似性。
我提出的原因之一也是为了说明传播行为。例如,如果 @Transactional 为PROPAGATION_REQUIRED,它将与输入 synchronized 块有很多相似之处。对于交易:如果交易存在,我们继续使用它,如果不存在,我们将创建一个交易。对于 synchronized ,如果我们已经有监视器,我们继续处理它,如果没有,我们将尝试获取它。当然,对于 @Transactional 我们不会锁定方法边界。 如果我们看 @Transactional 一个数据库资源(因为它在事务中使用) - 那么比较是有道理的。
然而,这是他们所有的共同点。同步在关键字使用时已知的对象监视器上定义(并且仅保护它),而事务可以锁定多个资源(在事务开始时不知道),或者可能不锁定任何资源(乐观锁定,只读事务)。
因此,最终 - 不要使用这种比较,它们有很多不同的东西比它们有所不同共同点。
At one of presentations about Spring/Hibernate transactions I brought up an opinion that synchronized keyword on a method and @Transactional logically have many similarities. Sure enough they are totally different beasts but yet they both applied as aspects to the method and both control access to some resources via some kind of shared monitor (record in db, for example).
There were couple of people in the crowd who immediately opposed and claimed that my comparison is fatally wrong. I don't remember specific arguments but I can see some point here as well. For example, synchronized works for the entire method from the beginning and transaction will only have effect when statement to access DB is reached. Plus synchronized does not offer any read/write locking pattern.
So the question is, is my comparison totally wrong and I should never ever use it, or, with proper wording, would it make sense to present it to experienced engineers who know well how synchronized works but yet trying to learn about AOP transactions? What this wording should be?
A bit of update.
Apparently my question sounded like comparing DB transactions vs entering synchronized method in Java. That's not the case. My idea is more about comparing similarities in semantics of @Transactional and synchronized.
One of the reason I brought it up also was to illustrate propagation behavior. For example, if @Transactional is PROPAGATION_REQUIRED it will have many similarities to entering synchronized block. For transaction: if transaction is present we just continue using it and if not, we will create one. For synchronized, if we already have monitor we proceed with it and if not we will attempt to acquire it. Of course for @Transactional we are not going to lock on method boundary.
If we look at @Transactional as denoting a method that locks a database resource (because it is used in the transaction) - then the comparison makes some sense.
However this is all they have in common. synchronized is defined on an object monitor (and protects only it), which is known at the time of usage of the keyword, while a transaction may lock multiple resources (that are not known when the transaction starts), or may not lock any resources at all (optimistic locking, read-only transactions).
So ultimately - don't use that comparison, there are a lot more things that they differ in than they have in common.
这篇关于Java同步关键字和Spring @Transactional注释的逻辑比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!