Sorry I hit the submit button by accident. I use an Applet to communicate to the servlet and the servlet looks something like this: public class TestServlet extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException { try { // Receive input data ObjectInputStream input = new ObjectInputStream(request.getInputStream()); Vector inData = (Vector) input.readObject(); input.close(); // Extract the action String action = (String) inData.elementAt(0); Vector outData = null; // Perform the requested action if (action.equalsIgnoreCase("GET_STUDENT_INFO")) { outData = this.getStudentInfo(inData); } else if (action.equalsIgnoreCase("GET_TEACHER_INFO")) { outData = this.getTeacherInfo(inData); } // Send output data response.setContentType("application/x-java-serialized-object"); ObjectOutputStream output = new ObjectOutputStream(response.getOutputStream()); output.writeObject(outData); output.flush(); output.close(); } catch (Throwable exception) { throw new ServletException(exception.getMessage()); } } private Vector getStudentInfo(Vector inData) { Vector studentInfo = new Vector(10); // Query Student Info //... return studentInfo; } private Vector getTeacherInfo(Vector inData) { Vector teacherInfo = new Vector(10); // Query Teacher Info //... return teacherInfo; } } I know that for every call to the doPost method a new Thread is created to handle the request. But do all threads share the getStudentInfo and getTeacherInfo methods or does each thread have its own copy? I'm going to have multiple people hitting this servlet and I would not be good for performance if the methods were shared and I had to synchronize them. Any info would be helpful. Thanks.
William Brogden
,
Author and all-around good cowpoke
I sense some confusion here. You should not be asking about copies of methods but about copies of objects. Since your Vectors outData and inData are local variables in the doPost method, they are unique to a particular request and Thread. As long as none of these methods uses instance variables to manipulate data unique to a particular request, you are ok. It does not matter how many Threads are executing a method in a particular object, as long as only local variables are used, there will be no mixing of data. Bill
I see what you're getting at. Yes I do have an instance variable of type DataSource and I use it to get a connection in the getStudentInfo() and getTeacherInfo() methods. Example: //... Connection connection = this.dataSource.getConnection("GSDQRSHARE", "GSDQRSHARE");
// Deactivate commitment control connection.setAutoCommit(true); //... So I should actually synchronize the use of this resource this this: //... Connection connection = null; synchronized(this.dataSource) { connection = this.dataSource.getConnection("GSDQRSHARE", "GSDQRSHARE"); } // Deactivate commitment control connection.setAutoCommit(true); //... Is this correct?
William Brogden
,
Author and all-around good cowpoke
If DataSource is an instance variable in your servlet, then you will need to synchronize access to it, yes. Having each request get its own connection is not very efficient, since opening a connection can be a slow process. You might want to investigate connection pooling. There are a number of implementations. Bill
I'm using WebSphere Application Server Express 5.0 on an IBM AS/400 ISeries server. It appers that when you setup a data source in WebSphere that it automatically has connection pooling enabled for that data source. So what I do is something like the following public void init(ServletConfig config) throws ServletException { super.init(config); try { Context ctx = new InitialContext(); this.dataSource = (DataSource) ctx.lookup("jdbc/QRDataSource"); } catch (Throwable t) { handleException(t); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection connection = null; try { //... synchronized (this.dataSource) { connection = this.dataSource.getConnection(user, password); } // Deactivate commitment control connection.setAutoCommit(true); //... } catch (Throwable t) { handleException(t); } finally { if (connection != null) { try { connection.close(); connection = null; } catch (Throwable t) { handleException(t); } } } I'm not sure if this behavior works on other webservers.
Post by:autobot
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, the permaculture playing cards