java - How to lock a mesage in a webapp to prevent more than 1 user from editing it -
i have web application written in java allows users send , reply messages. these messages stored in sql server database. messages belong user id, in cases multiple users have access same id (think 1 user multiple email addresses, shared other users) potentially seek reply same message @ same time.
solutions have found handle such scenario checking if message modified before saving it. not feasible here because a) reply saved new record , b) want prevent sec user origin reply don't spend 30 minutes composing reply find out else replied message. each message can have 1 reply.
so message needs locked somehow. when user starts reply message lock placed on message (say set flag in database record) , when user b tries reply same message informed user answering message.
that's fine except i'm dealing web app. if reason user while replying loses session (say browser crashes or whatever) message remain locked.
my current solution follows:
user starts reply message x.
a lock written table msg_id message x, user_id user , session_id user a's session.
user b tries reply message x. lock table checked , b informed can't reply message because user doing (holding lock).
now user a's browser crashes. user logs in , tries access message x again. allowed because 1 holding lock. session_id updated new one.
now user a's browser crashes again. disgusted , goes on 6 week vacation. message x still locked user session expires. httpsessionlistener takes note when session destroyed , removes lock. user b can reply message.
seeing lastly part of scenario is, while possible, unlikely, work out fine.
i'd see if has improve solution though.
your scenario pretty complete. can improve using ajax.
the thought create continous give-and-take between server , client sending, let's each minute, flag client server "the client writting reply message x".
the server have store info in database.
after that, server have utilize date , hr value unlock message if delay greater expected.
in processing that, messages no more locked 6 months, delay fixed timeout.
java sql session locking
No comments:
Post a Comment