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:

  1. select event table new user events
  2. process them (send emails users , other)
  3. 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

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -