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 6106673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:06:18+00:00 2026-05-23T14:06:18+00:00

I have a web application and I am using Oracle database and I have

  • 0

I have a web application and I am using Oracle database and I have a method basically like this:

public static void saveSomethingImportantToDataBase(Object theObjectIwantToSave) {
      if (!methodThatChecksThatObjectAlreadyExists) {
         storemyObject() //pseudo code
     }
     // Have to do a lot other saving stuff, because it either saves everything or nothing
     commit() // pseudo code to actually commit all my changes to the database.
}

Right now there is no synchronization of any kind so n threads can of course access this method freely, the problem arises when 2 threads enter this method both check and of course there is nothing just yet, and then they can both commit the transaction, creating a duplicate object.

I do not want to solve this with a unique key identifier in my Database, because I don’t think I should be catching that SQLException.

I also cannot check right before the commit, because there are several checks not only 1, which would take a considerable amount of time.

My experience with locks and threads is limited, but my idea is basically to lock this code on the object that it is receiving. I don’t know if for example say I receive an Integer Object, and I lock on my Integer with value 1, would that only prevent threads with another Integer with value 1 from entering, and all the other threads with value != 1 can enter freely?, is this how it works?.

Also if this is how it works, how is the lock object compared? how is it determined that they are in fact the same object?. A good article on this would also be appreciated.

How would you solve 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-23T14:06:19+00:00Added an answer on May 23, 2026 at 2:06 pm

    Your idea is a good one. This is the simplistic/naive version, but it’s unlikely to work:

    public static void saveSomethingImportantToDataBase(Object theObjectIwantToSave) {
        synchronized (theObjectIwantToSave) {
            if (!methodThatChecksThatObjectAlreadyExists) {
                storemyObject() //pseudo code
            }
            // Have to do a lot other saving stuff, because it either saves everything or nothing
            commit() // pseudo code to actually commit all my changes to the database.
        }
    }
    

    This code uses the object itself as the lock. But it has to be the same object (ie objectInThreadA == objectInThreadB) if it’s to work. If two threads are operating on an object that is a copy of each other – ie has the same “id” for example, then you’ll need to either synchronize the whole method:

        public static synchronized void saveSomethingImportantToDataBase(Object theObjectIwantToSave) ...
    

    which will of course greatly reduce concurrency (throughput will drop to one thread at a time using the method – to be avoided).

    Or find a way to get the same lock object based on the save object, like this approach:

    private static final ConcurrentHashMap<Object, Object> LOCKS = new ConcurrentHashMap<Object, Object>();
    public static void saveSomethingImportantToDataBase(Object theObjectIwantToSave) {
        synchronized (LOCKS.putIfAbsent(theObjectIwantToSave.getId(), new Object())) {
            ....    
        }
        LOCKS.remove(theObjectIwantToSave.getId()); // Clean up lock object to stop memory leak
    }
    

    This last version it the recommended one: It will ensure that two save objects that share the same “id” are locked with the same lock object – the method ConcurrentHashMap.putIfAbsent() is threadsafe, so “this will work”, and it requires only that objectInThreadA.getId().equals(objectInThreadB.getId()) to work properly. Also, the datatype of getId() can be anything, including primitives (eg int) due to java’s autoboxing.

    If you override equals() and hashcode() for your object, then you could use the object itself instead of object.getId(), and that would be an improvement (Thanks @TheCapn for pointing this out)

    This solution will only work with in one JVM. If your servers are clustered, that a whole different ball game and java’s locking mechanism will not help you. You’ll have to use a clustered locking solution, which is beyond the scope of this answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have developed a Web Application using grails, groovy and oracle as database with
I have a web application using JPA and JTA with Spring. I would like
I have this web application using Spring Web Flow framework. In my main page
I have a Java web application connecting to an Oracle database running on another
We have an application written in C language which interacts with Oracle database. This
I have a web application written in gwt, and I'm using a PostgreSQL database
I have a web application using ASP.NET 2.0 and I want to know if
I have a web application using the .Net 2.0 framework. The whole website is
I have a web application written using CherryPy, which is run locally on 127.0.0.1:4321
I have created a web application using Visualstudio 2008 using C# on my computer.

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.