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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:19:46+00:00 2026-06-06T15:19:46+00:00

I asked a similar question in Code Review forum but was suggested to ask

  • 0

I asked a similar question in Code Review forum but was suggested to ask that question here.
I am wondering about the synchronized keyword in the web-method below. Since setPerson is called from a thread pool (meaning that different threads can call it), I should syncronize it in someway. The thead pool is used by EclipseLink when my client is doing SOAP calls to the method. My question is if it is a good practice to make a web method synchronized or can I do the synchronization with em.lock(person, WRITE)?

@Override
public synchronized void setPerson(Person person) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersonLibPU");            
    EntityManager em = emf.createEntityManager(); 
    if(!em.getTransaction().isActive()) {
       em.getTransaction().begin();
    }
    try {
         person.setPersonId(getLastInsertedId() + 1); // Get the last inserted ID and increment it by 1
         em.merge(person);
         em.getTransaction().commit();  
         emf.getCache().evict(Person.class);                    
    } catch (Exception ex) {
         if(em.getTransaction().isActive())
            em.getTransaction().rollback();
    } finally {
        em.close();
    }
}

EDIT
I added one more row in the code above where I set the primary key value of the person object. That is what the synchronization is made for, not for any shared java objects. I need to synchronize it so two threads cannot get the same primary key.

  • 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-06-06T15:19:48+00:00Added an answer on June 6, 2026 at 3:19 pm

    You’re possibly asking the wrong question. Synchronization won’t help you here, as the only part that has concurrency concerns is getLastInsertedId(): you wouldn’t want two methods to get hold of the same value and try persisting an instance with that ID.

    Synchronizing setPerson would only solve the problem if it’s the only method that persists a Person.

    If possible use the underlying data-store’s auto-incrementing functionality; failing that does your ORM solution perform context-wide ID allocation? The last resort would be to have a method of getNextId that could be synchronized and returns an incremented value. This may end up with non-contiguous ID sequences in the database (getNextId won’t know if the persist operation succeeded), but this reduces the scope of the locking.

    As JB Nizet points out, the ID-allocation needs to be persisted and made available in some fashion that is available to all clients of this data store. If you had two instances of your application persisting Person instances, you would need to ensure that they share an ID-allocator.

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

Sidebar

Related Questions

I asked a similar question yesterday but I included some code that basically took
I asked a similar question yesterday, but recognize that i need to rephase it
I asked a similar question earlier but I'll ask it again in a different
I asked a similar question earlier but I'll ask it again in a different
I've asked a very similar question before, but this time is not about VB
I asked similar question before and got some nice code, but i'm concerned with
I asked a similar question yesterday that was specific to a technology, but now
Earlier, I asked a similar question, but I've since changed my code. Now the
I asked a similar question here but I changed assemblers and the question almost
I have asked a similar question elsewhere but perhaps I did not ask it

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.