java - Concurrent access on same table from two servers -
i accessing sql server 2005 table java j2ee using preparedstatements. jars deployed on 2 servers , run in parallel. process follows:
- select event table new user events
- process them (send emails users , other)
- delete processed events event table (if step 2 wasn't executed ok no delete should done)
in cases, users receives 2 emails (one each server) because 2 selects executed simultaneously before delete statement. don't have admin rights on table, access java application.
how can lock table @ first select , unlock after delete? see solution this?
if pick work in transactional way, 1 server can pick up:
set transaction isolation level repeatable read update top 1 tbl set processingonserver = host_name() yourworktable tbl processingonserver null , done = 0
now can select details, knowing work item safely assigned you:
select * yourworktable tbl processingonserver = host_name() , done = 0
the function host_name()
returns client name, if think it's safer can pass in hostname client application.
we add timestamp, can check servers took long process item.
Comments
Post a Comment