The answer to your question depends on where you create the connection. If you put it in the init method, then there is only one because init only gets run the first time the Servlet is activated.  If you put your connection in the doGet or doPost method, then you will have one connection per session.  If you don't have one connection per session then you need synchronize access to the connection object or you'll have problems.
 
If you expect more than a few users to be making use of this connection, then it is a better idea to use a connection pool of some kind.
 
- Mac
-----Original Message-----
From: Gerry Duhig [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, May 08, 2001 12:55 PM
To: [EMAIL PROTECTED]
Subject: Servlets & threads

I have written a Java servlet that seems to work quite well but I am confused about the exact context in which it runs.
 
At initialisation it creates a JDBC connection and uses that to connect to a remote Oracle Database on each activation of the servlet. The connection remains open until the servlet closes down.
 
I can see that JServ and Tomcat (I am experimenting with both) create multiple threads, so presumably my servlet can handle multiple simultaneous requests? But what about that one JDBC connection.
 
Is there one per thread?
 
Is there protection so that only one thread can use the connection at a time?
 
Or should I have created some protection to prevent clashes?
 
Its very unclear and I'd appreciate a few clues as to the real situation.
 
Regards
 
Gerry

Reply via email to