【发布时间】:2016-05-12 07:22:53
【问题描述】:
由于并发更新问题,我正在为我的应用程序尝试不同的隔离级别。我通过 DataSource 设置隔离级别,从中获取数据库连接
BasicDataSource ds = new BasicDataSource();
ds.setDefaultTransactionIsolation(
Connection.TRANSACTION_REPEATABLE_READ)
/* more config ... */
ds.getConnection()
对于 postgres 9.3 数据库,行的并发更新会触发错误,这是预期的,并且根据Standard SQL Transaction Isolation Levels上的 postgres 文档@ERROR: could not serialize access due to read/write dependencies among transactions
将隔离级别设置为Connection.TRANSACTION_READ_COMMITTED时,此错误按预期消失
尝试使用内存数据库中的 H2 重现此行为,隔离级别似乎不会改变我的单元测试的结果。即使完全停用隔离并使用LOCK_MODE=0 和/或Connection.TRANSACTION_READ_UNCOMMITTED 设置数据库url,我在并发更新时遇到错误org.h2.jdbc.JdbcSQLException: Concurrent update in table "MyTable": another transaction has updated or deleted the same row
我的问题是:如何配置 H2 连接以使用特定的隔离级别,我对锁定模式或连接的任何更改都不能帮助我摆脱 JdbcSQLException。
【问题讨论】:
标签: java postgresql transactions h2