0

I've been dealing with an issue that involves only allowing a single user to access certain data at a time. The reason for this is so that more than one user can't be editing the data at once.

My current solution is as follows: a user opens the data in a GUI (java) -> a value in the mysql table is set to "currently being viewed" for the data. This way, if another user tries to view the data, it will know there is someone currently editing it.

The problem arises when the user is done with editing the data. Normally, they would click "Done" and the mysql value would change back to "available". However, what if the program is forced shut, or if the user loses power?

How should I notify the program or the mysql table in this case that the data should be available again?

I currently use a WindowClosing event but it doesn't work if the program is forced closed.

user123446
  • 53
  • 1
  • 5
  • https://stackoverflow.com/questions/17431338/optimistic-locking-in-mysql – OldProgrammer Jun 11 '18 at 20:34
  • 1
    Depending on how you're "locking" the data, you could also place a "time/date" stamp against, allowing for a reasonable amount "timeout", reseting it when ever the user updates/accesses said data - when you check the "lock" status of the data, you'd also check the time, if the last "update" exceeds the timeout, you unlock the data. You could also look at "shut down hooks" - but this assumes a graceful shutdown of the JVM – MadProgrammer Jun 11 '18 at 20:35

2 Answers2

1

To get past your problem of one of the users not freeing the data after they're finished, you could implement a heartbeat.

https://en.wikipedia.org/wiki/Heartbeat_(computing)

Continually heartbeat the user using the data, and if the heartbeats stop then you can set it back to available.

Moffen
  • 1,817
  • 1
  • 14
  • 34
0

I thought about keeping a date when the editing began(as @MadProgrammer commented) but this still maybe inapplicable, because you can't define a reasonable limit since a editing session can last long, potentially.

What I would do is use the session with which the user is logged. That is, when a particular user starts editing a record you associate their sessionID to the record as the current owner. After this you can check with a scheduled method (or when there is an other write request from a different user) for this session, if it is dead, and the record is still in edit mode you can restore the free mode as the session is no longer alive.

NiVeR
  • 9,644
  • 4
  • 30
  • 35