简体   繁体   English

如何处理并发事务?

[英]How are concurrent Transactions handled?

I am using Glassfish 3.1 + Hibernate 3.6 + Postgres 9 and i am not fully understanding how i should handle "long" running concurrent transactions. 我正在使用Glassfish 3.1 + Hibernate 3.6 + Postgres 9,但是我还没有完全理解我应该如何处理“长时间”运行的并发事务。

My transactions can take up to one second in some cases. 在某些情况下,我的交易最多可能需要一秒钟。 What happens is that i generate objects out of a template table when i receive an event from a client. 发生的是,当我从客户端收到事件时,我从模板表中生成对象。 It can happen that two different events produce an overlapping set of objects. 可能发生两个不同的事件产生一组重叠的对象的情况。

I am thinking about this scenario: 我正在考虑这种情况:

T1 begin
T1 read objects which may already exist

T2 begin
T2 select templates which have to be processed
T1 inserts (generate objects)
T1 commit

T1 select templates which have to be processed
T1 inserts (generate objects)
T1 commit

How will this be propagated to me? 这将如何传播给我? Any lock exception? 有锁异常吗? Or will i get a constraint exception because of an unique index? 还是由于唯一索引而导致约束异常? How can i handle this situation? 我该如何处理这种情况? Restart an event processing 3 times before stop processing it? 在停止处理之前重新启动事件处理3次?

greetings, m 问候,米

The way that PostgreSQL handles this is called multi-version concurrency control. PostgreSQL处理此问题的方式称为多版本并发控制。 Basically concurrent transactions see a snapshot of the data when they begin the statement (by default) or more seldom when they begin the transaction (depending on transaction level). 基本上,并发事务在开始执行语句时(默认)会看到数据的快照,或者在开始事务时很少见(取决于事务级别)。

In this approach readers never block writers, and writers only block readers when the reader requests it. 在这种方法中,读者永远不会阻止作家,作家只会在读者要求时阻止读者。 For just reading and inserting you are unlikely to get lock exceptions/ 仅用于读取和插入,您不太可能会获得锁异常/

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

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