我刚发现H2不支持使用不同事务隔离级别的并发连接。也就是说,更改一个连接的事务隔离会影响所有其他连接。
Postgresql是否支持对每个连接使用不同的隔离级别?
发布于 2014-09-14 19:00:59
是的,它确实支持每个连接不同的事务隔离级别。您可以为与SET SESSION CHARACTERISTICS的连接设置事务隔离级别(以及事务的只读和可延迟状态):
localhost:5432 postgres postgres # SHOW transaction_isolation;
transaction_isolation
-----------------------
read committed
(1 row)
localhost:5432 postgres postgres # SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SET
localhost:5432 postgres postgres # SHOW transaction_isolation;
transaction_isolation
-----------------------
repeatable read
(1 row)您可以使用SET TRANSACTION ISOLATION LEVEL覆盖每个事务,也可以在使用BEGIN TRANSACTION ISOLATION LEVEL启动事务时覆盖:
localhost:5432 postgres postgres # BEGIN;
BEGIN
Time: 0.227 ms
localhost:5432 postgres postgres * # SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SET
Time: 0.229 ms
localhost:5432 postgres postgres * # SHOW transaction_isolation;
transaction_isolation
-----------------------
serializable
(1 row)
Time: 0.262 ms
localhost:5432 postgres postgres * # COMMIT;
COMMIT
localhost:5432 postgres postgres # SHOW transaction_isolation;
transaction_isolation
-----------------------
repeatable read
(1 row)默认设置为default_transaction_isolation参数:
localhost:5432 postgres postgres # SHOW default_transaction_isolation;
default_transaction_isolation
-------------------------------
repeatable read
(1 row)见http://www.postgresql.org/docs/current/static/sql-set-transaction.html的文档
发布于 2014-09-14 13:04:43
每个事务基础设置事务隔离级别。即使是一个连接也可以在每个事务上具有不同的隔离级别。
当然,不同的连接可以有不同的隔离级别。
下面是一些测试:
postgres=# begin;
BEGIN
postgres=# show transaction_isolation ;
transaction_isolation
-----------------------
read committed
(1 row)
postgres=# set transaction isolation level serializable ;
SET
postgres=# show transaction_isolation ;
transaction_isolation
-----------------------
serializable
(1 row)同时在另一届会议上:
postgres=# begin;
BEGIN
postgres=# show transaction_isolation
;
transaction_isolation
-----------------------
read committed
(1 row)
postgres=# set transaction isolation level read uncommitted ;
SET
postgres=# show transaction_isolation
;
transaction_isolation
-----------------------
read uncommitted
(1 row)提交事务并开始另一个新事务后,将使用在postgresql.conf上设置的默认隔离级别:
#default_transaction_isolation = 'read committed'我在Postgres 9.1上试过这个。
希望能帮上忙,干杯。
https://dba.stackexchange.com/questions/76493
复制相似问题