当春季事务隔离级别与数据库事务隔离级别发生冲突时会发生什么?

huangapple go评论42阅读模式
英文:

What happens when spring transaction isolation level conflicts with database transaction isolation level?

问题

数据库事务隔离级别是首要的,或者Spring可以覆盖它吗?如果数据库级别具有优先级,那么在哪些情况下使用Spring的隔离配置?

英文:

As I know database transaction isolation level is a prior, or spring can override it?
If database level has priority what are the cases to use spring isolation configuration?

答案1

得分: 4

没有所谓的"数据库事务隔离级别"和"Spring事务隔离级别"之分。一个数据库可能实现了SQL标准定义的隔离级别,而启动事务的客户端可能会为其请求特定的隔离级别。

有一些需要注意的事情,但它们并不相互矛盾:

  • 数据库通常有一个默认的隔离级别,如果客户端不明确请求事务的特定级别,就会使用该默认级别。比如,在PostgreSQL中,默认的隔离级别是Read Committed,而在MySQL中是Repeatable Read。
  • 数据库可能不实现所有的隔离级别,或者在实现中具有一些特殊之处。例如,Oracle数据库不支持Read Uncommitted和Repeatable Read隔离级别,而PostgreSQL的Read Uncommitted模式的行为类似于Read Committed。

在Spring中,当您通过@Transactional(isolation = ...)注解或TransactionTemplate#setIsolationLevel()指定隔离级别时,它会让JDBC驱动程序发出SQL命令,以设置当前会话所需的隔离级别。

例如,对于Read Committed,Oracle JDBC驱动程序会执行ALTER SESSION SET ISOLATION_LEVEL = READ COMMITTED。如果指定了不支持的级别,它将抛出异常。

参考文献:

英文:

There is no such separation as a "database transaction isolation level" and a "spring transaction isolation level".
A DB might implement the isolation levels defined by the SQL standard and a client that starts a transaction might request a specific level of isolation for it.

There are a couple of things to note that however do not present any contradiction:

  • A DB usually has a default isolation level that is used if a client does not explicitly request a specific level for a transaction. Say, in PostgreSQL the default one is Read Committed and in MySQL it's Repeatable Read.
  • A DB might not implement all of the isolation levels or have some specifics in their implementation. E.g. Oracle DB does not support the Read Uncommitted and Repeatable Read isolation levels and PostgreSQL's Read Uncommitted mode behaves like Read Committed.

With Spring, when you specify an isolation level either via the @Transactional(isolation = ...) annotation or TransactionTemplate#setIsolationLevel() it makes the JDBC driver issue an SQL command to set the desired level for the current session.

E.g. Oracle JDBC driver will do ALTER SESSION SET ISOLATION_LEVEL = READ COMMITTED for Read Committed.
If an unsupported level is specified it'll throw an exception.

Refs:

huangapple
  • 本文由 发表于 2023年2月8日 23:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定