简体   繁体   English

事务隔离级别如何使用多个@Transactional 改变整个事务

[英]How transaction isolation level changes the whole transaction using several @Transactional

@Transactional
public void a() {
  b();
  c();
}

@Transactonal(isolation = Isolation.SERIALIZABLE)
public void b() {
  // some code with update in db
}

public void c() {
  // some code with selects and updates in db
}

What isolation level will be used in method 'c'?方法“c”中将使用什么隔离级别? As far as I understand, it all will be a one transaction.据我了解,这一切都将是一笔交易。 And in method 'b' we just join to current transaction and change its isolation level.在方法“b”中,我们只是加入当前事务并更改其隔离级别。 But will it be changed back to default after method b execution?但是方法b执行后会改回默认值吗?

I ask it because in such situation on big load we got 'PSQLException: ERROR: could not serialize access due to concurrent update' in method 'c', although i want to have a default isolation level in method 'c'.我问它是因为在大负载的这种情况下,我们在方法“c”中得到“PSQLException:错误:由于并发更新而无法序列化访问”,尽管我想在方法“c”中有一个默认的隔离级别。

@Transactional
public void a() {
  b();
  c();
}

@Transactonal(isolation = Isolation.SERIALIZABLE)
public void b() {
  // some code with update in db
}

public void c() {
  // some code with selects and updates in db
}

Calling another method from the same class with @Transactional will have no effect since proxies can not work on invokation from the same class.使用@Transactional从同一个类调用另一个方法将不起作用,因为代理不能从同一个类调用。 It is how proxies behind the scenes, work that does not allow such a thing.它是如何在幕后代理,工作不允许这样的事情。

However if you call another method from another spring bean, that has @Transactional this will be respected.但是,如果您从另一个具有@Transactional spring bean 调用另一个方法,这将受到尊重。

@Service
public SpringClass {

    @Transactional
    public void a() {
      anotherSpringClass.b();
      anotherSpringClass.c();
    }

}

There you can define that you want a nested transaction to be opened with another isolation level.在那里您可以定义要使用另一个隔离级别打开嵌套事务。

@Service
public class AnotherSpringClass {

@Transactional(propagation = Propagation.NESTED, isolation = Isolation.SERIALIZABLE)
public void b() {
  // some code with update in db
}

}

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

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