Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to ensure concurrency control in servlets?

Posted on 2001-08-24
10
Medium Priority
?
276 Views
Last Modified: 2013-11-24
Hi, may I know how to handle multiple requests to the same servlet that will update the database with the correct implementation of concurrency control?

Let's say we get the number of items in the database table. "Select count(*) from Item" returns 100.

Next we add a new Item using 100 + 1 = 101 as the item_id

Now how can I ensure that the item_id is unique, no two requests starting at the same time has added the item_id, 101 into the database?

Thanks.
0
Comment
Question by:Zuzzi2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6421873
Hi,

I would use stored procedure with proper transaction control/isolation level and let db server handle concurrency issues.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
0
 
LVL 3

Expert Comment

by:sghosh092199
ID: 6422073
You can also make the particular method as 'synchronized'.

synchronized public int updateCount(int value)
{
value += 1;
return value;

}

When you are doing a SELECT to get the current value, make the SELECT and the UPDATE as an atomic operation.So, the above method can have this variation:

synchronized public void updateCount()
{
//SELECT
item_id += 1;
//INSERT
}
0
 
LVL 3

Expert Comment

by:sghosh092199
ID: 6422110
You can also make the particular method as 'synchronized'.

synchronized public int updateCount(int value)
{
value += 1;
return value;

}

When you are doing a SELECT to get the current value, make the SELECT and the UPDATE as an atomic operation.So, the above method can have this variation:

synchronized public void updateCount()
{
//SELECT
item_id += 1;
//INSERT
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:sdussinger
ID: 6423326
I would agree with bazarny on using the database for concurrency control. However if this is not possible then a synchronized version much like the one that sghosh suggested would be the correct approach.

Given that, though, there is one slight problem with sghosh's approach.  Since servlets are created and destroyed when connections are accepted, using synchronized in this way will not work. The synchronized keyword as used above will synchronize based on the current instance of an object, but the servlet container will have created multiple instances to support multiple connections. Each of those instances running in their own thread have a monitor which they can use to block access, but since each instance has a different monitor, you can't block access to the database from multiple instances in this way.

What is needed instead is a monitor that all instances of your servlet can share.  For this, we can create a static Object in the servlet class and synchronize on that, instead. The code would look something like:

class DbUpdate
{

  // This is our monitor.
  static Object lock = new Object ();

  .
  .
  .
  public void incrementCounter ()
  {
    synchronized (lock)
    {
      .
      .
      // Do your update here...
      .
      .
    }
  }

}

HTH

--Steve
0
 
LVL 4

Expert Comment

by:sdussinger
ID: 6423334
Actually that DbUpdate class is your servlet. I forgot to add the "extends HttpServlet" to the class definition.

Sorry for any confusion...

--Steve
0
 
LVL 3

Expert Comment

by:sghosh092199
ID: 6423483
> servlets are created
and destroyed when connections are accepted,

> multiple instances to support multiple connections

I think, this is where Servlets are distinguished from CGI.
Servlets are not created in multiple instance to handle multiple connections.

0
 
LVL 4

Expert Comment

by:sdussinger
ID: 6423530
Well, from practical experience, I can safely say that at least one servlet container I've used does create individual instances. I ran into this exact problem several months ago and it turns out that this was the solution.  

While from a specification standpoint, this may not be part of the spec, but there are servlet vendors out there who use multiple instances as a performance improvement, so you can't count on the fact that there will only be one servlet instance...

By using a static lock, you can safely ignore the multiple instance problem, if it exists, and this code will still work when a single instance is all that's being used...


--Steve
0
 
LVL 4

Accepted Solution

by:
sdussinger earned 400 total points
ID: 6423854
After a little more research, I discovered that the servlet specification prior to 2.2 did not specify how multiple instances would be handled. That was left up to the container implementer to decide. In the 2.2 specification, it was tightened to specify that if a Servlet did not implement SingleThreadModel it would only be created once. Any Servlet which does implement SingleThreadModel will be created for each invocation of the servlet.

So servlet containers prior to the 2.2 spec are free to create as many instances as they like, to handle the incoming requests. Servlet containers which are 2.2 compliant should not create multiple instances, unless specifically requested by implementing the SingleThreadModel interface...

--Stevce

0
 
LVL 5

Expert Comment

by:vemul
ID: 7773734
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to sdussinger

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7861667
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question