英文:
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
。如果指定了不支持的级别,它将抛出异常。
参考文献:
- https://www.postgresql.org/docs/current/transaction-iso.html
- https://docs.oracle.com/cd/E25054_01/server.1111/e25789/consist.htm#CNCPT1312
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论