在Oracle数据库中持久处理并发请求? [英] Handling the concurrent request while persisting in oracle database?

查看:198
本文介绍了在Oracle数据库中持久处理并发请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况,在航空公司网站上(使用Java)两个独立的客户同时发送两个请求预订同一航空公司的同一个座位
从B $ B到纽约到芝加哥。我正在使用oracle数据库并且读取隔离级别。我的问题是oracle数据库是否提供
处理这种并发场景的任何解决方案?
我知道的是什么时候第一次事务DML语句被触发它将获得对受影响的
行的锁定,并将在事务完成时释放,即在发出回滚或commit时发布。但是一旦提交完成,第二个请求将在
首先进行已完成并将覆盖第一个。所以它没有帮助?

I have this scenario ,on a airline website (using Java) two separate customers send two requests at same time to book a same seat in same airline
from New York to Chicago. I am using the oracle database and isolation level is read committed.My question here is that does oracle database provide any solution to deal with this kind of concurrent scenario? what I know is when first transaction DML statement is fired it will get a lock on affected rows and will release when transaction completes i.e on issuing rollback or commit.But as soon as commit is done and second request will proceed as soon as first is completed and will override the first one.So it does not help?

是的Java我可以处理我的db类作为单例并在正在进行更新的方法上使用synchronized关键字。 但是想知道是否有
无论如何我们可以在数据库级别本身出现这种问题?可序隔离级别可以帮助。但不确定?

Yes in Java I can deal with making my db class as singleton and using synchronized keyword on method which is doing update. But want to know is there anyway we can this kind of issue at database level itself?Probably isolation level as serializable can help. But not sure?

推荐答案

要在网站中处理并发,通常会在每个网站上都有一列记录,允许您检查它,因为你得到它没有更新。上次更新日期或顺序版本号(由触发器自动递增)。

To handle concurrency in a web site a common practice it to have a column on each record that allows you to check it has not been updated since you got it. Either last update date or a sequential version number (auto incremented by a trigger).

通常,您将读取数据(加上并发列)

Typically you will read the data (plus the concurrency column)

SELECT seat,etc,version_no
FROM t1
WHERE column = a_value

然后当用户最终开始预订座位时,除非有更新,否则更新将会有效。

Then when the user eventually gets round to booking the seat the update will work unless there has been an update.

(版本号或更新日期将在每次更新后更改)

(the version number or update date will change after every update)

BEGIN
    UPDATE t1
    SET seatTaken = true
    WHERE seatid = .....
    AND version_no = p_version
    RETURNING version_no INTO p_version;
EXCEPTION WHEN NOT_FOUND THEN
    --Generate a custom exception 
    --concurrency viloation the record has been updated already
END;

自动更新版本号的触发器看起来有点像这样

the trigger to auto update the version number would look a little like this

CREATE OR REPLACE TRIGGER t1_version
AFTER INSERT OR UPDATE ON t1
FOR EACH ROW
BEGIN
    IF :new.version_no IS NULL THEN
       :new.version_no  := 0;
    ELSE
       :new.version_no  := :old.version_no  + 1;
    END IF;
END;

这篇关于在Oracle数据库中持久处理并发请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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