java - 选择列并从其他选择中隐藏

标签 java sql database jdbc oracle11g

我正在处理一种在 oracle 11.2 表中实现的消息队列。我知道,这是错误的,但我仍然拥有它。

表格由idmessagedatestatus组成。插入所有新消息,状态为 NEW。我正在开发 java (jdbc) 阅读器,它会挑选最旧的新消息:

select * from messages
where status = NEW and rownum <= 1
order by date asc

然后阅读器处理消息并将其状态设置为完成。它运作良好,而我们只有一个读者。多个阅读器的问题在于,他们都选择相同的消息。

我试图通过将状态更新为 WORKING 来解决这个问题。以下伪代码是否正确?

//autocommit is on
id = query(select … for update)
query(update messsages set status = WORKING where id = :id)
…do some processing in reader…
query(update messsages set status = DONE where id = :id)

它适用于多个并发读者吗?读者大部分时间会在锁上等待吗?或者他们只会得到下一个未锁定的行?

最佳答案

除非您在您的select for update 中包含skip locked 子句,否则多个读者将等待。 From the documentation :

By default, the SELECT FOR UPDATE statement waits until the requested row lock is acquired. To change this behavior, use the NOWAIT, WAIT, or SKIP LOCKED clause of the SELECT FOR UPDATE statement.

And :

SKIP LOCKED is an alternative way to handle a contending transaction that is locking some rows of interest. Specify SKIP LOCKED to instruct the database to attempt to lock the rows specified by the WHERE clause and to skip any rows that are found to be already locked by another transaction.

关于java - 选择列并从其他选择中隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25785687/

相关文章:

java - 使用 collect() 而不是 filter() 过滤流

java - 无法在屏幕旋转时保持 RecyclerView 位置

php - 在其他 sql 语句中使用 sql 语句结果

mysql - “名为 MySQL 的 Windows 服务已存在”。如何删除我的电脑中已存在的 MySQL 服务名称

php - 如何存储由用户服务器端(mySQL)创建的 <li>-item

java - Hibernate Spring 连接无法 Autowiring

java - onPrepareOptionsMenu 没有在 Fragments 中被调用

mysql - SQL - 选择关联记录

SQL语法错误

sql - 什么时候不应该使用关系数据库?