Synchronization will not kill the performance if you use
a synchronization block and the lock is on the object session.
A wrong design assumption would have place the lock on the
servlet itself (blocking the processing of all clients).
So the following code is okay for me :
HttpSession session = request.getSession(true);
synchronized ( session ) {
ConnectionHolder holder = (ConnectionHolder)
session.getValue("servletapp.connection");
if(holder==null) {
.......
}
}
By the way, it's not a good idea to store JDBC connection
in the session. You may lock this resource for a really long
time. On the other hand, is the JDBC connection a Serialisable
object (persistent session, swapping, clustering Cf WebSphere) ?
You should move to a pool with DataSources
Bruno
Mageshkumar Maruthapillai wrote:
> I hope somebody can help me with a resolution to this issue.
> Basically, if an impatient user happens to submit multiple times to the same
> servlet, then
> obviously threading problems will occur. For example the code snippet below
> from Jason Hunter's book. Multple threads from the same user can arise
> when he presses the submit button multiple times.
> Threading switching occurrs after the execution of the
>
> statement 'if(holder==null)' in the code below, then
>
> there is a possibility for a null pointer exception when holder
>
> is used.
>
> How can this be prevented? ASP , I think only allows
>
> one user thread per asp script. I think using synchronization
>
> blocks within the servlet will be kill performance. How can I get
>
> over this problem.
>
> Any help on this matter is appreciated.
>
> Thanks,
>
> mmp.
>
> ----------------------------------------------------------------------------
> -----
>
> page 271 [ Chapter 9: Database Connectivity - Connections as Part of
> Session]
>
> public void doGet(HttpServletRequest request, HttpServletResponse response)
> throws ServletException, IOException
>
> {
>
> response.setContentType("text/html");
>
> PrintWriter out = response.getWriter();
>
> HttpSession session = request.getSession(true);
>
> ConnectionHolder holder = (ConnectionHolder)
> session.getValue("servletapp.connection");
>
> if(holder==null) {
>
> try
>
> {
>
> holder = new ConnectionHolder
> (DriverManager.getConnection("jdbc:oracle:oci7:ordersdb","user","password"))
> ;
>
> session.putValue("servletapp.connection",holder);
>
> }
>
> catch (SQLException e)
>
> {
>
> getServletContext().log(e, "Could not get db connection");
>
> }
>
> }
>
> Connection con = holder.getConnection();
>
> try
>
> Statement stmt = con.createStatement();
>
> // transactions using con
>
> }
>
> // ...................
>
> }
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html