简体   繁体   English

当@Transactional注释方法被多个实例并行命中时会发生什么?

[英]What happens when a @Transactional annotated method is hit in parallel by multiple instances?

Please correct me if I am wrong somewhere. 如果我在某个地方不对,请纠正我。

I am having an issue where my transaction are not being saved to the data base and some sort of racing is occurring which screws up the data. 我遇到一个问题,即我的交易未保存到数据库中,正在发生某种竞争,使数据搞砸了。 The app is hit in parallel by multiple instances. 该应用程序被多个实例并行命中。 I have used @Transactional, which I know is to do a transaction with database and the transaction is committed when the method returns. 我使用了@Transactional,我知道它是与数据库进行事务处理的,并且该方法返回时将提交事务。

The question is, does hitting it through multiple instance still maintain this one transaction per hit thing, or it does not handle the situation and data will screw up because of racing? 问题是,通过多次实例击中它是否仍会保持每个击中事物的这一笔交易,还是无法处理这种情况并且数据会因为竞速而搞砸?

Can a solution be suggested for the given condition? 可以针对给定条件提出解决方案吗?

The @Transactional is not related to synchronization. @Transactional与同步无关。 It just makes sure that your flow either succeeds or fails. 它只是确保您的流程成功或失败。 Each hit has its own flow and its own success or failure. 每次点击都有自己的流程,也有自己的成功或失败。

I guess what you're experiencing is due to the use of shared data. 我想您遇到的是由于使用共享数据。

For example. 例如。 If you have a class Foo that looks like this: 如果您有一个Foo类,如下所示:

public class Foo {
    private static boolean flag = true;

    @Transactional
    public void doSomething() {
        flag = false;
    }
}

In this case it doesn't matter that you have many Foo instances because they all use the same flag . 在这种情况下,有很多Foo实例都没关系,因为它们都使用相同的flag

Another scenario would be if you have one instance of Foo (very common if you use something like Spring ) and you have data that is changed for this instance. 另一种情况是,如果您有一个Foo实例(如果使用像Spring这样的实例,则很常见),并且您有针对该实例更改的数据。 You can look at the same Foo example and just remove the static from flag : 您可以查看相同的Foo示例,只需从flag删除static

public class Foo {
    private boolean flag = true;

    @Transactional
    public void doSomething() {
        flag = false;
    }
}

In either of those cases you need to synchronize the data changes somehow. 在这两种情况下,您都需要以某种方式同步数据更改。 It has nothing to do with @Transactional . 它与@Transactional无关。

That transactions are database transactions and behavior is database engine dependant but it usually works this way: 事务是数据库事务,行为取决于数据库引擎,但通常以这种方式工作:

  1. A thread enter the method. 线程进入方法。
  2. A thread enter the same or any other transactional method. 线程输入相同或任何其他事务方法。 It does not block as @Transactional is not about synchronization . 它不会阻塞,因为@Transactional 与同步无关
  3. One thread execute any query that blocks a database resource. 一个线程执行任何阻塞数据库资源的查询。 Eg. 例如。 SELECT * FROM MYTABLE FOR UPDATE; .
  4. Another thread try to execute anything that needs the same database resource . 另一个线程尝试执行任何需要相同数据库资源的操作 Eg. 例如。 UPDATE MYTABLE SET A = A + 1; And it blocks . 并且它阻止了
  5. The thread that acquired the lock on step 3 completes the transactional method successfully making an implicit commit or fails making an implicit rollback . 在步骤3上获得锁的线程成功完成了事务性方法的隐式提交,或者失败了隐式的回滚
  6. The blocked thread wakes up and continues as it can now get the resource that was locked. 被阻塞的线程将唤醒并继续运行,因为它现在可以获取已锁定的资源。

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

相关问题 在@async带注释的函数中发生多个请求时会发生什么? - What happens when multiple request occurs in a @async annotated function? 了解Spring事务 - 当事务方法调用另一个事务方法时会发生什么? - Understanding Spring transactions - What happens when a transactional method calls another transactional method? 当@Transactional(readonly=true) 方法调用@Transactional(readonly=false) 方法时会发生什么,反之亦然? - What happens when @Transactional(readonly=true) method calls @Transactional(readonly=false) method and vice versa? 回滚 @Transactional 注释的方法 - Rollback a @Transactional annotated method 将Java类注释为@WebService时会发生什么? - What happens when a java class is annotated @WebService? 创建多个静态类实例时会发生什么? - What happens when multiple instances of static class are created? 从另一个@Transactional注释方法调用@Transactional注释方法 - Call @Transactional annotated method from another @Transactional annotated method 与方法注解者@Transactional的通信 - Communication to caller of method annotated @Transactional @Transactional中的会话发生了什么 - What happens to the Session in @Transactional Spring 没有为 @service 注释的 class 创建 bean,当它的方法之一用 @transactional 注释时 - Spring is not creating the bean for @service annotated class when one of its method is annotated with @transactional
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM