Topic: 请教:关于数据库锁的问题

  Print this page

1.请教:关于数据库锁的问题 Copy to clipboard
Posted by: dingligang
Posted on: 2003-12-22 02:39

现在手头有一个需求:
数据库中某个表的每条记录的一个字段表示状态位,值为0或者1。规则是:
1。读取此状态(0或者1,期间可能被别的程序修改)
2。点击一个提交按钮(一段时间后),要求判断这个状态位,如果是1,则置为0,否则提示状态已经被更改。

请问2中的2个操作(检查、赋值),仅仅用一个jdbc中的

setAutoCommit(false);
//select (check) ...
//update (do) ...
conn.commit();
setAutoCommit(true);

行么?或者用别的方法?

2.Re:请教:关于数据库锁的问题 [Re: dingligang] Copy to clipboard
Posted by: floater
Posted on: 2003-12-22 03:09

I don't think this is going to work.

I think someone asked this before, you may search on the keyword database lock.

There should be a setting on the db side with locks, check your db docs and see whether you can do it on the db side for threads.

If all your db operations are java based, you may create some classes/layer so all threads use this and you set restrict in there.

If you have multithread, use synchronization.

The transaction tool is just to make sure it will run all or none successfully, that's all. So if an app does a select and right after that, another app changes the value of what you select, then your update would set the wrong value.

3.Re:请教:关于数据库锁的问题 [Re: dingligang] Copy to clipboard
Posted by: dingligang
Posted on: 2003-12-23 11:41

在程序的线程这一级别是比较好控制的。
但是现在有个问题,就是如果程序两个进程修改的是两条不同的记录的状态位,那么就没有必要去同步他们。
也就是对于一条记录,必须进行同步。当然可以用一些代码来实现这种机制,不过用数据库的特性好像可行性比较好一点Clown

4.Re:请教:关于数据库锁的问题 [Re: dingligang] Copy to clipboard
Posted by: floater
Posted on: 2003-12-23 14:36

当然可以用一些代码来实现这种机制,不过用数据库的特性好像可行性比较好一点


agreed, after all it's on db side, besides you may have another JVM or even code in other languages accessing db.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923