I have a question about synchronization and Servlets. I am using jdk 1.1.5 on a Solaris, Sparc station, Apache/1.3.4 and JServ module 0.9.11 . I am serving pages built from an object oriented database implemented in Object Design Inc's PSE Pro 2.0. I want to be able to serve these pages in a multi-threaded manner. However, the PSE portion of my code requires that I operate within transactions. Although it is oversimplifying the situation let's just say that I can only have one transaction open at a time for a given JVM. Thus It would seem that I need to synchronize my code, starting before I open a transaction and ending when I close a transaction. Let me back up a bit and explain how I think multithreading in servlets works and how it can add efficiency. I would appreciate it if anyone could point out any errors in this statement as I am still learning about servlets and Java in general. Basically each time your servlet is called, a new thread is started and the service() method of your servlet is called in that thread. If your code is completely unsynchronized, then multiple threads can run completely independently of each other. This means that each thread is allotted a certain amount of time to run, after it's time is up, the thread stops and another thread is given some time. Thus, if each thread is performing operations which cause it to actually use all of the processor time available to it then no efficiency is gained through using multiple threads. However, often a servlet will not use all of the time available to it, usually because it is waiting on some other device to respond. While it is waiting for something else to happen, an individual thread can sleep thus freeing up processor time for other threads that are not sleeping. It is in this way that using threads can increase the efficiency of multiple servlets. So when I look at the code for my servlet, there is a big chunk that needs to be synchronized as I explained earlier. The question is, should I synchronize the entire service method, or is it worth it to synchronize just that block that requires synchronization. Now the code before and after each transaction is pretty inconsequential and should not require the thread to sit idle, waiting for a response, with one possible exception, closing the output PrintWriter and flushing it's contents. Does it make sense to synchronize only the block which I am required to and close the PrintWriter in the unsynchronized part of the code? Is this actually more efficient? With some connections, it takes some time for the data to be written to the requesting browser. Does this delay affect my servlet or does the servlet just spit all of the output into some buffer and then exit? Please forgive me if these questions reveal a lack of understanding of threading or servlet concepts. I am learning this stuff as I go along. Here is a revised version of our servlet, which synchronizes everything but the closing of the output stream. Is this at all an improvement over synchronizing the whole service method? You can see this servlet in action at: http://www.labmed.umn.edu/servlets/pageservlet?ptype=c&compID=c0526 Any comments are welcome. Thanks, Doug Hershberger [EMAIL PROTECTED] *********************************** package bbd.servlet; import java.util.*; import java.io.*; import COM.odi.*; import COM.odi.util.*; import bbd.obj.*; import bbd.util.*; import javax.servlet.*; import javax.servlet.http.*; public class PageServlet extends HttpServlet { public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); AccessBroker ab = null; Database db = null; Transaction tr = null; Session sess = null; synchronized { try { ab = new ServletAccessBroker(db, "public", out, req); //The ServletAccessBroker is an object //which carries around the Database, //PrintWriter and other things from one //method to another. This was neccessary //in order to use legacy code to //generate the pages. /* * * Generate a page. * */ } catch (Exception e) { if (ab == null) { ab = new ServletAccessBroker(db, "omnipotent", out, req); } ErrorPage.StackTrace( ab, e ); } finally { out.println("This page was generated using " + "enhanced Servlet technology!"); if ( db != null && db.isOpen()) { db.close(); } if ( sess != null && sess.isActive()) { sess.terminate(); } } } //end synchronized block out.close(); } // End Method service public String getServletInfo() { return "UMBBD Page Servlet"; } } ___________________________________________________________________________ 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
