选择计数然后插入 [英] select count then insert

查看:76
本文介绍了选择计数然后插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果我这样做


开始交易

Stmt 1:从中选择计数(*) tableA

如果计数为零,则继续执行以下操作

Stmt 2:插入某事 to tableA


Stmt 3:插入某事物 to tableB

结束交易


如果我使用的是DB2默认设置,是否有数据一致性

问题如果有两个用户

进行上述交易。 (即,选择,得到计数= 0,然后

继续插入)


一个独特的索引会有帮助吗?


谢谢

LeWindLetter

解决方案

嗨。


2007-01-16 10:28, le ********** @ gmail .com 写道:


如果我使用DB2默认设置,是否有数据一致性

问题如果有两个用户

进行上述交易。 (即,选择,得到计数= 0,然后

继续插入)



这取决于您的交易隔离级别重新使用。使用

默认设置,游标稳定性,您将遇到问题。

查看可用的不同隔离级别,并且

set transaction isolation"命令。


唯一索引是否有帮助?



不,它不会。


本杰明


< blockquote>这有点切线,但为了防止它有用,你可能需要

来考虑(如果你还没有)编写条件插件

如此:


BEGIN ATOMIC

如果存在(从桌子中选择1)那么

插入表格...

ELSE

...

END IF

END;


或者,也许,使用MERGE


--Jeff

le ********** @ gmail.com 写道:




如果我这样做


开始交易

Stmt 1:从表A中选择计数(*)

如果count为零,然后继续执行以下操作

Stmt 2:插入某事 to tableA


Stmt 3:插入某事物 to tableB

结束交易


如果我使用的是DB2默认设置,是否有数据一致性

问题如果有两个用户

进行上述交易。 (即,选择,得到计数= 0,然后

继续插入)


一个独特的索引会有帮助吗?


谢谢

LeWindLetter


到本杰明,


你呢是指使用可重复读隔离级别?它是否连接

级别?如果我使用的是JDBC连接池,是否会影响其他的

交易?

在这种情况下,会选择count lock整个表吗?

给Jeff,


有没有办法在Java中而不是使用PL SQL?因为我

不想绑定到特定的数据库


先谢谢你们两位


Hi

If I do

Begin transaction
Stmt 1: select count(*) from tableA
If the count is zero, then proceed to the following
Stmt 2: insert "something" to tableA

Stmt 3: insert "something" to tableB
End transaction

If I am using DB2 default setting, would there be data consistency
issue if there are two users
doing the above transaction. (i.e., both select, got count=0, then
proceed to insert)

Would an unique index help?

Thanks
LeWindLetter

解决方案

Hi.

On 2007-01-16 10:28, le**********@gmail.com wrote:

If I am using DB2 default setting, would there be data consistency
issue if there are two users
doing the above transaction. (i.e., both select, got count=0, then
proceed to insert)

That depends on the transaction isolation level you''re using. With the
default setting, "Cursor Stability", you will run into problems.
Have a look at the different isolation levels that are available, and
the "set transaction isolation" command.

Would an unique index help?

No, it wouldn''t.

Benjamin


This is sort of tangential, but just in case it''s helpful, you may want
to consider (if you haven''t already) writing your conditional insert
like so:

BEGIN ATOMIC
IF EXISTS(SELECT 1 FROM TABLEA) THEN
INSERT INTO TABLEA...
ELSE
...
END IF
END;

or, perhaps, use MERGE

--Jeff

le**********@gmail.com wrote:

Hi

If I do

Begin transaction
Stmt 1: select count(*) from tableA
If the count is zero, then proceed to the following
Stmt 2: insert "something" to tableA

Stmt 3: insert "something" to tableB
End transaction

If I am using DB2 default setting, would there be data consistency
issue if there are two users
doing the above transaction. (i.e., both select, got count=0, then
proceed to insert)

Would an unique index help?

Thanks
LeWindLetter


To Benjamin,

Do you mean using Repeatable read isolation level? Is it on connection
level? If I am using a JDBC connection pool, would it affect other
transaction?
In this case, would select count lock the whole table?

To Jeff,

Is there a way to do it in Java rather than using PL SQL? cause I
don''t want to tied to a particular database

Thanks both of you first


这篇关于选择计数然后插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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