This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes synchronized block in JSP tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronized block in JSP tag" Watch "synchronized block in JSP tag" New topic
Author

synchronized block in JSP tag

Sudhakar Duraiswamy
Ranch Hand

Joined: Dec 20, 2007
Posts: 43
Hi,
I am trying to find answer for the following ,for the past couple of days ,but couldnt find comprehensive answer


Problem Statement

I have a custom JSP tag class which handles a web form submission ,captures data and write it to same file in the filesystem.
As all web applications,this can be triggeredsimultaneosly ,and i fear that multiple threads would be in action handling each of the submission (we all know thats how Servlet works.)

CODE



ISSUE:

- A File object can be opened by one thread for writing and at same time another thread might try to access and fail and throw an exception
So i thought of synchronizing (on the object ) should solve the issue , but read some where that jsp engine would have pool of jsp tag objects, so i am afraid that synchronized (this) wont work and it should be changed to
synchronized (this.getClass()
[/b]



Can anyone please provide any light on this .
Thanks a bunch in advance



Sudhakar
SCJP 5.0 93%| SCWCD (prep. ongoing))
Sudhir Ravindra
Greenhorn

Joined: May 31, 2010
Posts: 16
Else you can perform file system locking. Create a lock file when proceeding to logging action. Other calls will be blocked till the lock is cleared.
When logging is done, release lock. Depends on your scenario, is there any read on these log files from jsp?
Sudhakar Duraiswamy
Ranch Hand

Joined: Dec 20, 2007
Posts: 43
Hi Sudhir,
Appreciate the reply.

As per j2 api,

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.
File-lock objects are safe for use by multiple concurrent threads.


So following are my questions

1. what is the difference between multiple threads and multiple concurrent threads?
2. In the API its mentioned that resultant lock is system dependant.So does this work well in UNIX?
3. Implementing File lock will the threads wait till the they acquire the lock or will they just quit processing when a file is locked ?
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Firstly, writing to a file from a jsp is truly against an MVC architecture (in case you bother about it) and really not advisable.

Coming to your question:

Writing to the same file from different threads essentially means that one of the thread has to wait till the time the other has finished writing. This can be achieved in 2 ways:

1) Make the code in such a way that only one thread writes to the file at a time (Thats what you are trying to do by putting a sync block)
2) Have a different class that writes to the file. All the other (HTTP) threads just queue their requests to this class and does not actually write it to the file in the same HTTP thread. A different thread clears the queue and persist the information to the file.

If you follow 2) and assuming you only have one worker thread per file, you can use FileLock to prevent concurrent access to this file.

Any kind of locking done in code (aka synchronize block) breaks off in a clustered scenario so it is not really the way to go.

Sudhakar Duraiswamy wrote: 3. Implementing File lock will the threads wait till the they acquire the lock or will they just quit processing when a file is locked ?


There is a method "trylock" that does not block as opposed to "lock" which blocks till the lock is available.


apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized block in JSP tag
 
Similar Threads
Will a started thread run to completion in this example?
Exam 9, Q24 Dan Chisholm
Use a singleton or a "purely static" class?
sending jasper report directly to printer
Concurrency Question with Thread.sleep(5000) ?