Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 787469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:12:24+00:00 2026-05-14T21:12:24+00:00

I need to prevent simultaneous edits to a database field. Users are executing a

  • 0

I need to prevent simultaneous edits to a database field. Users are executing a push operation on a structured data field, so I want to sequence the operations, not simply ignore one edit and take the second.

Essentially I want to do

synchronized(key name)
{
  push value onto the database field
}

and set up the synchronized item so that only one operation on “key name” will occur at a time. (note: I’m simplifying, it’s not always a simple push).

A crude way to do this would be a global synchronization, but that bottlenecks the entire app. All I need to do is sequence two simultaneous writes with the same key, which is rare but annoying occurrence.

This is a web-based java app, written with Spring (and using JPA/MySQL). The operation is triggered by a user web service call. (the root cause is when a user sends two simultaneous http requests with the same key).

I’ve glanced through the Doug Lea/Josh Bloch/et al Concurrency in Action, but don’t see an obvious solution. Still, this seems simple enough I feel there must be an elegant way to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:12:24+00:00Added an answer on May 14, 2026 at 9:12 pm

    There may be a simple way to let your database take care of this for you. I am admittedly weak in knowledge when it comes to databases. In lieu of that, here is an approach that involves creating an individual lock for each key name. There is a single repository that manages the creation/destruction of the individual locks that requires a one-for-the-entire-application lock, but it only holds that lock while the individual key-name lock is being found, created, or destroyed. The lock that is held for the actual database operation is exclusive to the key name being used in that operation.

    The KeyLock class is used to prevent simultaneous database operations on a single key name.

    package KeyLocks;
    
    import java.util.concurrent.locks.Lock;
    
    public class KeyLock
    {
        private final KeyLockManager keyLockManager;
        private final String keyName;
        private final Lock lock;
    
        KeyLock(KeyLockManager keyLockManager, String keyName, Lock lock)
        {
            this.keyLockManager = keyLockManager;
            this.keyName = keyName;
            this.lock = lock;
        }
    
        @Override
        protected void finalize()
        {
            release();
        }
    
        public void release()
        {
            keyLockManager.releaseLock(keyName);
        }
    
        public void lock()
        {
            lock.lock();
        }
    
        public void unlock()
        {
            lock.unlock();
        }
    }
    

    The KeyLockManager class is the repository that is responsible for the lifetimes of the key locks.

    package KeyLocks;
    
    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;
            final Lock lock = new ReentrantLock();
        }
    
        private final Map<String, LockEntry> locks = new HashMap<String, LockEntry>();
        private final Object mutex = new Object();
    
        public KeyLock getLock(String keyName)
        {
            synchronized (mutex)
            {
                LockEntry lockEntry = locks.get(keyName);
                if (lockEntry == null)
                {
                    lockEntry = new LockEntry();
                    locks.put(keyName, lockEntry);
                }
                lockEntry.acquisitionCount++;
                return new KeyLock(this, keyName, lockEntry.lock);
            }
        }
    
        void releaseLock(String keyName)
        {
            synchronized (mutex)
            {
                LockEntry lockEntry = locks.get(keyName);
                lockEntry.acquisitionCount--;
                if (lockEntry.acquisitionCount == 0)
                {
                    locks.remove(keyName);
                }
            }
        }
    }
    

    Here is a sample of how you would use a key lock.

    package test;
    
    import KeyLocks.KeyLock;
    import KeyLocks.KeyLockManager;
    
    public class Main
    {
        private static final String KEY_NAME = "TEST_KEY";
    
        public static void main(String[] args)
        {
            final KeyLockManager keyLockManager = new KeyLockManager();
            KeyLock keyLock = null;
            try
            {
                keyLock = keyLockManager.getLock(KEY_NAME);
                keyLock.lock();
                try
                {
                    // Do database operation on the data with the specified key name
                }
                finally
                {
                    keyLock.unlock();
                }
            }
            finally
            {
                if (keyLock != null)
                {
                    keyLock.release();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to put the if/else in the loop, and… May 14, 2026 at 11:35 pm
  • Editorial Team
    Editorial Team added an answer Check out the subprocess module. It should help with output… May 14, 2026 at 11:35 pm
  • Editorial Team
    Editorial Team added an answer This is what you are after: http://msdn.microsoft.com/en-us/library/ms972958.aspx It is specifically… May 14, 2026 at 11:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.