H2数据库-并发连接策略
我有几个独立运行的相同应用程序。每个从数据库读取一行(基于某些条件),执行一系列操作并最终更新该行。 因此,我想确保应用程序开始处理的行不会被另一个应用程序处理。换句话说,我希望应用程序选择下一个可用行。 我怎样才能实现这个?
我尝试了使用“选择...更新”、MVCC、不同类型的事务隔离的不同策略,但暂时没有运气。
I have several identical applications running independently. Each read a row from the database (based on some criteria), perform a series of operations and finally updating that row.
Therefore I want to make sure that a row started to be processed by an application, will not be processed by another. In other words, i want that an application to select the next available row.
How can I implement this?
I try'it different strategies using "select ... for update", MVCC, different type of transaction isolation, but no luck for moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
常见的解决方案是使用“状态”列:
处理行时,将状态设置为 1。处理完成后,将其设置为 2。这适用于所有数据库。
如果您想防止行陷入“正在处理”状态(例如,因为会话/连接已关闭),那么您可以添加一列“processing_session”并用当前会话填充它(函数
SESSION_ID( )
) 处理时。要查明会话是否仍处于活动状态,请使用表INFORMATION_SCHEMA.SESSIONS
。A common solution is to use a 'state' column:
When processing the row, set the state to 1. Once it's processed, set it to 2. This will work in all databases.
If you want to protect against rows being stuck in the 'processing' state (for example because the session/connection was closed), then you could add a column 'processing_session' and fill it with the current session (function
SESSION_ID()
) when processing. To find out if a session is still alive, use tableINFORMATION_SCHEMA.SESSIONS
.