How to lock a table row use JDBC?

icey 2004-01-30 11:00:43
I want to lock a record when I update or use it.
How to use JDBC to do this?
Thanks a lot!
...全文
109 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
icey 2004-02-01
  • 打赏
  • 举报
回复
Who know this?
youthy_yy 2004-01-30
  • 打赏
  • 举报
回复
you can try this:
SELECT * FROM table_name FOR UPDATE;
icey 2004-01-30
  • 打赏
  • 举报
回复
you tell me to lock whole table, I don't want to do in this way.
I just want to lock the records that I selected.

Because between I select this record and modify its value on page and update this, other user couldn't modify same record.
bdsc 2004-01-30
  • 打赏
  • 举报
回复
It looks like you could not master your whole life.
try this, you send some vendor specific sql query to DB and lock the tables, then do what ever you want, then unlock them,

MySQL:
String sql = "xxxxxxxxxxxxxxx";
stat.execute("lock tables tableName WRITE"); //shold check the result
rs = stat.executeQuery(sql);
while(rs.next()) {
String sql_in = "update tableName set value=xxx";
another_stat.execute(sql_in);
}
stat.execute("unlock tables");
hiking 2004-01-30
  • 打赏
  • 举报
回复
Why do you want to lock the record when you use it?

In ejb, the container will automaticly reload the record wherever you want to read it.

<isolation-level> got lots types. you may try other type
icey 2004-01-30
  • 打赏
  • 举报
回复
We use <isolation-level>Required</isolation-level>
If I use TransactionSerializable, it will some error occured.
I have not investigate this problem.
bdsc 2004-01-30
  • 打赏
  • 举报
回复
Ok then, where is your ejb deployer?

<transaction-isolation>
<isolation-level>TransactionSerializable</isolation-level>
<method>
<description>...</description>
<ejb-name>...</ejb-name>
<method-intf>...</method-intf>
<method-name>...</method-name>
<method-params>...</method-params>
</method>
</transaction-isolation>

Deployment file: weblogic-ejb-jar.xml
icey 2004-01-30
  • 打赏
  • 举报
回复
But I am use weblogic's connection pool,
and my jdbc invoke by ejb, its join ejb's transaction, I think it couldn't set transaction level for self connection.

Add "for update" at the end of that sql can lock that record? if lock how to open that lock? if update fail in later, will it rollback and auto open this lock?
bdsc 2004-01-30
  • 打赏
  • 举报
回复
Using the setTransactionIsolation() Method

TRANSACTION_SERIALIZABLE
specifies that dirty reads, nonrepeatable reads,
and phantom reads are prevented.

This is the compatible way.
gong1 2004-01-30
  • 打赏
  • 举报
回复
agree with upstairs.
^_^

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧