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

  • Home
  • SEARCH
  • 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 4027112
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:03:22+00:00 2026-05-20T11:03:22+00:00

current situation: I have a service with several typical service methods like create(), remove(),

  • 0

current situation: I have a service with several typical service methods like create(), remove(), findXy(), .. etc. Now I want to prevent two different service calls at the same time, meaning I want to lock my servicecall, after entering it the first time, so the second client (or same client again) will only enter the method as soon as first client is done.

For locking purposes we normally use ReentrantLocks. They work perfectly fine. But now i am doubting on how to implement these ReentrantLocks, since I dont want to insert them in every single method. Moreover our servicelayers define their Transaction context already on their own, which means, when I would integrate them in the method itself, a transaction is already started before the lock has happen.

public class WorkerServiceServerImpl implements WorkerServiceRemote
{
  private static final ReentrantLock workerLock = new ReentrantLock( true );

  @Autowired
  private WorkerDao                 workerDao;

  @Override
  @Transactional
  public int createWorker( int projectId ) throws RemoteException
  {
    workerLock.lock();//NO GOOD! Transaction already started!

    try
    {
      workerDao.create( projectId );

      //Do some more stuff...
    }
    finally
    {
      workerLock.unlock();//Unlock, next request can be handled!
    }
  }

  ...
}

Of course I could create a ServiceLayer above this WorkerServiceImpl class and just forward the calls to the original service, but since I have like 30 services I wonder, if there are any global solutions to solve this issue. Or is it possible to create an own annotation which fires before a transaction is started?

Any ideas are welcome, thanks in advance!
ymene

EDIT: I choose CRUD methods for simplicity. Of course those service calls are far more advanced and can call different CRUD-Methods within one call.

  • 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-20T11:03:22+00:00Added an answer on May 20, 2026 at 11:03 am

    There are several points that can be made regarding this design:

    • The need to apply locks to service methods looks strange. If you have some problems with concurrent execution of service methods, perhaps it would be better to solve them by proper configuration of transactions when accessing an underlying database.

    • unlock() must be called inside a finally block.

    • If your service is singleton, your use of ReentrantLock is equivalent to marking your methods synchronized.

    • You can create an aspect to apply synchronization to your methods. See 7. Aspect Oriented Programming with Spring, especially 7.2.4.7 Advice ordering.

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

Sidebar

Related Questions

The current situation is as follows: We have an production .net 3.5 WCF service,
this is the current state/situation: I have an Activity which binds a Service which
I have a fairly standard inheritance situation in my current LINQ-to-SQL project. I have
Here is my situation. I have written a WCF service which calls into one
I have a situation where I am running in a WCF service that has
The situation: I need to convert our current development environment from Windows XP 32-bit to
I am curious how others would handle this situation. I have a domain layer
I have my app engine project myproject.appspot.com hosted at myprojectsdomain.com. I want to permanently
Current, I've got a stored procedure that has a main goal of doing a
My current development project has two aspects to it. First, there is a public

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.