锁定老帖子 主题:基于hibernate多线程操作同一张表
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-07-02
diracstar 写道 这的确是个办法,但仔细想一下,要等到发送线程 将消息发送出去之后 才会修改标志位然后存入log,然后最后从queue中删除, 那么这个过程中,读写数据库的线程仍然会重复操作
那就设置一个中间状态标志,表示正在发送,发送完成了再删除。 |
|
返回顶楼 | |
发表时间:2007-07-02
diracstar 写道 这的确是个办法,但仔细想一下,要等到发送线程 将消息发送出去之后 才会修改标志位然后存入log,然后最后从queue中删除, 那么这个过程中,读写数据库的线程仍然会重复操作
有次序的动作交个线程按次序处理,不存在重复操作的问题 因为你前面那个管理发送记录的线程已经对记录进行了分流。 可以参考webserver的线程模型,每个request都可以操作相同的数据库,你觉得你的情况非常特殊了吗? |
|
返回顶楼 | |
发表时间:2007-07-02
悲观锁可以解决
摘录自《POJOS in action》 Pessimistic locking When optimistic locking won’t work, another way to handle concurrent updates is by using pessimistic locking. As the name suggests, this mechanism assumes that concurrent updates will occur and so incurs an overhead regardless of whether they do. However, this overhead is much less than with fully isolated transactions. A transaction that uses pessimistic locking locks the rows that it reads, which prevents other transactions from reading and updating them. Other transactions will block until the transaction releases those locks by either committing or rolling back. Pessimistic locking prevents lost updates and provides some degree of read consistency because it prevents the read rows from being changed by other transactions. However, because pessimistic locking does not prevent new rows from being inserted, re-executing the same query might return different results. |
|
返回顶楼 | |
发表时间:2007-07-02
我觉得你不需要多线程,单线程会比较好!要知道,不是所有的程序都要用多线程的!有时,单线程也是有优势的!个人认为只要你发送了信息,N个对方收到后,回复事务,就可以把它删除!只需要用JDBC的回滚事务就行了!
|
|
返回顶楼 | |