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
synchronized (this){
final String reportFileName = "testReport.csv";
File reportDir = new File( rootCsDirectory, "reports" );
if(!reportDir.isDirectory())reportDir.mkdir();
File reportFile = new File (reportDir, reportFileName);
logReport(reportFile,reportContent.toString());
}
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())