Mirko-
The design pattern I find works the best with servlets is MVC. I use the
servlet to be the controller to handle requests and session management. The
view is either a JSP, template, or imbedded HTML in a presentation class.
The model or data portion is handled through separate classes.
There shouldn't be a need to synchronize to handle concurrent data requests.
Also, consider a connection pooling scheme.
Below is an example of how you could handle concurrent requests.
<snippet>
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
hSessionObjects = new Hashtable();
// bundle-up session objects
hSessionObjects.put("MyServlet.request", req);
hSessionObjects.put("MyServlet.response", res);
hSessionObjects.put("MyServlet.out", out);
MyData md = new MyData(hSessionObjects);
String queryType = req.getParameter("queryType");
// based on type of query... execute a different method
if (queryType.equals("query1")){
md.doQuery1();
}else{
md.doQuery2();
}
}
public class Mydata {
private PrintWriter out;
private HttpServletResponse res;
private HttpServletRequest req;
private Hashtable hDataObjects;
MyData(Hashtable hash){
hDataObjects = new Hashtable();
hDataObjects = hash;
this.res =
(HttpServletResponse)hDataObjects.get("MyServlet.response");
this.out = (PrintWriter)hDataObjects.get("MyServlet.out");
this.req = (HttpServletRequest)hDataObjects.get("MyServlet.request");
}
public void doQuery1(){
String queryString = req.getParameter("queryString");
// execute the query
// print the results
out.println(.....);
}
public void doQuery2(){
String queryString = req.getParameter("queryString");
// execute the query
// print the results
out.println(.....);
}
}
<end snippet>
John Kirby
DISC
-----Original Message-----
From: Mirko Wolf [mailto:[EMAIL PROTECTED]]
Sent: Friday, April 09, 1999 9:51 AM
To: [EMAIL PROTECTED]
Subject: a design question
Hi there,
Here is another question how to design functions for a servlet.
IMHO for every request made to a servlet the service function is called
again.
But what happens, when a critical function is called by two or more service
functions at the same time?
For instance a function which handles a database connection and the
belonging queries.
What happens, when a second service function call this method while it
handle's a database querie for the first one?
The first one will have no chance to complete the request, right?
Should the database function synchronized or is it better to implement the
SingleThreadModel?
I think it is easier to implement the SingleThreadModel, because i've a lot
of functions the must be synchronized and
then i'am afraid to have a deadlock in the system.
Any comments or suggestions?
Mirko
___________________________________________________________________________
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
___________________________________________________________________________
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