java - Locking database edit by key name -


I need to stop editing the database fields together. I am performing a push operation on a structured data field. , So I want to index an operation, do not ignore one edit and take another.

Basically I want to

  synchronize (key name) {Push value on database field}  

and synchronized items So that there will be only one operation on "key name" at a time. (Note: I am simplified, it is not always a simple push).

A raw path to this would be global synchronization, but it completely disrupts the barriers. Whatever I want to do, he writes two simultaneous with the same key, which is a rare but annoying event.

This is a web based Java application, written by Spring (and using JPA / MySQL). Operation is started by a user's web service call. (The root cause is that when the user sends two simultaneous HTTP requests with the same key).

I have watched Doug Lee / Josh Bloch / At Al-Conquency in Action, but a clear solution, however, it seems quite easy I think that there must be a great way to do this.

It is a simple way for your database to take care of this when it comes to database I am clearly weak about knowledge. In return for that, there is an approach in which each key name needs to be made for a personal lock. There is a single repository that manages the construction / destruction of individual locks, for which a full-application lock is required, but it only keeps that lock while the personal key-name lock is being found, Or being destroyed. The lock placed for the actual database operation is unique to the key name used in that operation.

The keylock class is used to stop the database operation on a single key name simultaneously.

pre> package keylocks; Import java.util.concurrent.locks.Lock; Public class keylock {private finale keylock manager key lock manager; Private last string keyName; Private finals lock lock; KeyLock (KeyLockManager keyLockManager, string keyname, lock lock) {this.keyLockManager = keyLockManager; This.keyName = keyName; This.lock = lock; } @ Override Protect Finalized Zero () {Release (;); } Public Zero Release () {keyLockManager.releaseLock (keyName); } Public Zero Lock () {lock.lock (); } Public Zero Unlock () {lock.unlock (); }}

The KeyLock Manager Class is a repository that is responsible for the life time of the key lock.

  PackageKeyloux; Import java.util.HashMap; Import java.util.Map; Import java.util.concurrent.locks.Lock; Import java.util.concurrent.locks.ReentrantLock; Public Class KeyLockManager {Private Class LockEntry {int acquisitionCount = 0; Last lock lock = new retract lock (); } Private Last Maps & lt; String, lock enterory & gt; Locks = new Hashmop & lt; String, lockentry & gt; (); Private last object mutex = new object (); Public Kunjilak getLock (string key name) {synchronize (mute x) {lock entry lock entry = lock.at (keyname); If (lockEntry == zero) {lockEntry = New LockEntry (); Locks.put (keyName, lockEntry); } LockEntry.acquisitionCount ++; Return the new keylock (this, key name, lockkey. Lock); }} Zero release lock (string key name) {synchronize (mute x) {lock entry lock entry = lock.at (keyname); LockEntry.acquisitionCount--; If (lockEntry.acquisitionCount == 0) {locks.remove (keyName); }}}}  

Here is a sample how you will use key lock.

  Package testing; Import keylock Kellock; Import Kills Kellok Manager; Public Class Main {Private Static Last String KEY_NAME = "TEST_KEY"; Public Static Zero Main (string [] Args) {Last Key Lock Manager Key Locker Manager = New KeyLockManager (); Keylock kunjylock = null; Try {KeyLock = keyLockManager.getLock (KEY_NAME); KeyLock.lock (); Try {// operations on databases on data with specified key names} Finally {keyLock.unlock (); }} Finally {if (keyLock! = Null) {keyLock.release (); }}}}  

Comments