• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I do not implement SingleThreadModel and I have a servlet that looks like this:

[ Edited by Dave to add code tags ]
[ October 14, 2003: Message edited by: David O'Meara ]
 
Joshua Doerring
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joshua Doerring
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joshua Doerring
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic