• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronize class for JSP client  RSS feed

 
P Gupta
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I would like to synchronize a method String : Valdate( string ) in SyncValidate class.
The JSP client calls Validate() method in the SyncValidate class
Now I have tried putting synchronized keyword in the method
public synchronized String Validate( String para){…}
But it doesn’t work.
Where am I going wrong, and how do I synchronize Validate(); dose one need to create separate thread for each request from the JSP for synchronized method to work,

Prabhat Gupta
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "it doesn't work", what exactly do you mean? What did you expect would happen?
- Peter
 
P Gupta
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter
The problem was that even when I put synchronized keyword in the method my program crashed as it would allow multiple threads in , when I sent in multiple Request at one time.
I then made all the classes level Variable private static and declared my method as static …and this worked
Surprisingly I hadn’t come across this fact in any of the books I have read…
I figured this out in an article in Java World
Thanks
Prabhat Gupta
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't sound as if it would make any difference unless you had marked your jsp as not threadsafe (isThreadSafe="false"). In any case you might be setting yourself up for threading and scaling problems!
If you feel you still don't have full grip on the design then by all means discuss it here in this forum.
- Peter
 
Bill william
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wondering Peter den Haan is still here. anyway here is my question:
I have a servlet, which calls a java class (DAO) to create a new user and do other stuff. before i try to create a new user, i need to check if the user exists in the DB, so the check and create actions have to be atomic to avoid creating the same user twice. instead of making the servelt implement SingleThreadModel or synchronizing the service method, i try to allow only one instace of the DAO class in the JVM (Singleton), and synchronize the method which does check and create (or make the method static as Gupta metioned above).
the drawback is obvious:
1. performance/scalability sucks, because only one request can be processed at one time.
2. does not work under cluster enviroment, in which multiple JVMs exist, so singleton idea no longer works.
Any comments? any better designs>
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does implementing your customer creation class as
stateless session bean reduce your problem?
 
Bill william
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rufus BugleWeed:
Does implementing your customer creation class as
stateless session bean reduce your problem?

Thanks for your reply.
a method in a stateless session bean only guarantee the transactional part, in which the whole process will be rolled back in case of exception.
I still need to synchronize the method inside the session bean, AND in the descriptor file I need tell the container not to create more than one instance in the pool. so it is similar in terms of performance.
Gupta (http://www.coderanch.com/t/150731/java-Architect-SCEA/certification/synchronization-inside-server) mentioned to use table/record level synchronization with stored procs. Maybe it is one way to do it.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bill william1:
wondering Peter den Haan is still here.
Did anyone invoke my name?
I have a servlet, which calls a java class (DAO) to create a new user and do other stuff. before i try to create a new user, i need to check if the user exists in the DB, so the check and create actions have to be atomic to avoid creating the same user twice.
There are a couple of different ways to handle this.[list]Let the database handle it. Assuming you have a uniqueness constraints on user name or whatever determines user identity, then simply which will be performed atomically by any ACID database. If the user already exists, the database won't do the insert because it would violate the uniqueness constraint, and you can detect this condition in your code by checking the result of executeUpdate(). You can add a WHERE NOT EXISTS... clause if you think the above is ugly, but remember that this won't see any uncommitted changes, i.e. a user may have been added but not yet committed so you can still get the uniqueness constraint violation.[/list][list]This won't work in a cluster, but you could address any performance bottleneck by making the code single-threaded on a per-user basis. For example,This will ensure that the body of addIfNecessary() is only ever executed by a single thread for a given user.HTH,
- Peter
 
Bill william
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
The first approach is good, but it could not be used in my application, because there is no unique constraints in my table, AND I have to check another DB (in another server) to see if i need create a new entry.
The 2nd approach seems good. since my class is running inside an app server, it is possible that two instances are created, in which case i can make 'locks' static and as synchronized set. right?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!