You absolutely do not want to share database connections like this.  Each request should have its own database connection.  Wether the connection is created from scratch each time or it is checked out from a pool doesn't matter in terms of correctness.  You will run into issues with two or more pieces of code simultaneously utilizing a Connection.

mark

At 06:35 PM 10/16/02 +0530, karthikeyan wrote:
HI,
 
  Have a bean class.  And there in the constructor part of the class establish a database connection.  Do put a condition to check if connection is open already if yes then dont do anything. This worked for me.  Looking forward for some comments on it.
 
karthikeyan.
----- Original Message -----
From: Anoop Kumar V
To: [EMAIL PROTECTED]
Sent: Wednesday, October 16, 2002 1:28 PM
Subject: Connection to database from jsp.

We have a system consisting of a group of jsps - which have links to one another. There is no controller servlet as it is a simple system. What i want is to access the database from the jsps. The problem is that I do not want a new connection to be established everytime a request is made. What is the solution for this?
I had initially thought of having a utility class and in that I can have a getConnection method which returns the connection to a static connection reference in the same utility class. This can then be assigned to the jsps (by making a call from the jsps to some method - returnConn, in the utilily class). Is it true that a new connection will be established everytime. This has confused me a bit.
I am infact confused now about the very concept of static references. How do you think this problem can be solved? Will a servlet be of any use? Actually in a servlet, if we have a static connection reference as a class variable, and the initialization is done ( acquiring a connection) in the init method ( as init is executed only once in the lifetime of a servlet), the problem may be solved. In a servlet, the init method is executed only once and the servlet does not go out of memory till the server comes down. So I presume the connection will also be the same. Is this correct? Even if it is, I want to avoid the servlet, as it will be a lot of rework if the servlet has to be fit in. The application would have to be changed. Suggestions invited.

Anoop Kumar V.

Reply via email to