synchronized block in JSP tag class

Posted by Sudhakar on Stack Overflow See other posts from Stack Overflow or by Sudhakar
Published on 2010-05-31T10:31:04Z Indexed on 2010/05/31 10:32 UTC
Read the original article Hit count: 324

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())

© Stack Overflow or respective owner

Related posts about java

Related posts about web-applications