hibernate中的并行更新处理 [英] Concurrent updates handling in hibernate

查看:86
本文介绍了hibernate中的并行更新处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我们在数据层中使用JDBC,并计划用休眠替代它。我是Hibernate的新手,不确定hibernate是如何处理并发的。有人可以解释我,如果我们使用spring来进行事务管理,将如何处理并发更新:通过hibernate(在hibernate的内存自动版本管理中),或者我必须将版本列置于数据库中以手动处理并发更新。 / b>

解决方案 有人可以解释我,如果我们使用spring来进行事务管理,将如何处理并发更新通过hibernate(在hibernate的内存自动版本管理中),或者我必须将版本列放入数据库来手动处理并发更新。


无论您是否使用Spring进行事务管理都不重要,并且在并发管理方面并不相关,这实际上是由Hibernate处理的。 Hibernate可以使用2种策略来处理并发更新:乐观锁定和悲观锁定。



乐观



锁定,你将一个特殊的属性(一个数字,一个时间戳)映射为一个版本(所以你实际上有一个列)。当您在更新期间检索where子句中的实体和包含时,会读取此版本,Hibernate将递增。为了说明这是如何工作的,让我们假设你通过id = 1加载一个Person实体,当前版本为1。保存后,Hibernate会执行如下操作:

  update PERSON set ID = 1,NAME ='NAME 1',VERSION = 2其中ID = 1且VERSION = 1; 

现在,假设您有两个并发事务在运行,每个事务都加载 实体(相同版本号)并更改名称。



假设事务#1首先提交,执行以下查询:

  update PERSON set ID = 1,NAME ='NAME 1',VERSION = 2其中ID = 1且VERSION = 1; 

成功并且版本得到增加。



然后提交事务#2,执行以下查询:

  update PERSON set ID = 1,NAME ='NAME 2',VERSION = 2其中ID = 1且VERSION = 1; 

这个不会更新任何内容,因为where子句不会匹配任何记录。这是你会得到一个乐观的并发异常的地方。



当你不维护连接,并发访问不频繁时,这种策略是合适的,好。所有的东西当然都是由Hibernate为你处理的,只要你映射一个版本属性即可。



悲观



当使用悲观锁定时,Hibernate会锁定一条记录供您独占使用,直到完成它为止(通常使用 SELECT ... FOR UPDATE )。试图访问相同记录的任何其他并发事务将被挂起,直到锁被删除。这种策略以性能价格提供了更好的可预测性,并且不会无限缩小。 参考


Presently we are using JDBC in the data layer and planning to replace it with hibernate. I am new to Hibernate and not sure how hibernate handles concurrency. Can somebody explain me if we use spring for the transaction management, how concurrent updates will be handled: by hibernate (In memory automatic version management of hibernate) or I have to put version column in the database to take care of concurrent updates manually.

解决方案

Can somebody explain me if we use spring for the transaction management, how concurrent updates will be handled by hibernate (In memory automatic version management of hibernate) or I have to put version column in the database to take care of concurrent updates manually.

Whether you're using Spring for transaction management or not doesn't really matter and isn't relevant when it comes to concurrency management, this is actually handled by Hibernate. Hibernate can use 2 strategies to handle concurrent updates: optimistic locking and pessimistic locking.

Optimistic

When using optimistic locking, you map a special attribute (a number, a timestamp) as a version (so you actually have a column for it). This version is read when you retrieve an entity and included in the where clause during an update and incremented by Hibernate.

To illustrate how this works, let's imagine you load a Person entity by id=1 and with a current version=1. After a save, Hibernate will perform something like this:

update PERSON set ID=1, NAME='NAME 1', VERSION=2 where ID=1 and VERSION=1;

So, now, imagine you have two concurrent transactions running, each of them loading the same entity (same version number) and changing the name.

Let's say transaction #1 is committed first, the following query is performed:

update PERSON set ID=1, NAME='NAME 1', VERSION=2 where ID=1 and VERSION=1;

It succeeds and the version gets incremented.

Then transaction #2 is committed, the following query is performed:

update PERSON set ID=1, NAME='NAME 2', VERSION=2 where ID=1 and VERSION=1;

This one won't update anything because the where clause won't match any record. This is where you'll get an optimistic concurrency exception.

This strategy is appropriate when you don't maintain the connection, when concurrent accesses are not frequent, and scales really well. And everything is of course handled transparently by Hibernate for you, as long as you map a version attribute.

Pessimistic

When using pessimistic locking, Hibernate locks a record for your exclusive use until you have finished with it (typically using a SELECT ... FOR UPDATE). Any other concurrent transaction trying to access the same record will get suspended until the lock is removed. This strategy gives better predictability, at the price of performance and doesn't scale indefinitely.

References

这篇关于hibernate中的并行更新处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆