Hi Jonathan,
The init method of a servlet is called only once, when it is created. This
is a perfect spot to put your code for creating a connection to the
database.(to close the connection, put the appropriate code in the delete()
method)
As I review the mailing list, I believe we're all in the same spot - we're
not exactly sure of what performance issues will arise regardless of
approach. One thing is for sure, you really need to have that connection
take place in the init method of your servlet. I.e. I saw response saying
use a connection pool.
See O'Reilly's book by Jason Hunter on this approach - it is well
documented.
Mark Mynatt
-----Original Message-----
From: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of
Jonathan Silvergran, SM3OJR
Sent: Friday, January 05, 2001 6:11 AM
To: [EMAIL PROTECTED]
Subject: Connections bound to the session - comments wanted
Greetings,
In a web ap consisting of a number of servlets, I have a "WelcomeServlet"
which is the first servlet called for each session. In this servlet, I
create a JDBC connection that I store in the session.
I have created a method for executing SELECT queries that looks like this:
// Set autoCommit to false in the query call for manual transactions
public ResultSet runQuery(boolean autoCommit, String qString) {
Connection con = (Connection)session.getValue("connection"); // Get the
conn from session
try {
if ( con != null ) {
if ( !autoCommit )
con.setAutoCommit(false);
else
con.setAutoCommit(true);
Statement statement = con.createStatement();
rs = statement.executeQuery(qString);
session.putValue("connection", con); // Write the conn back to the
session in
// case the AutoCommit status
has changed
}
catch... etc
return rs // returns the ResultSet
}
First, is this way of handling connections recommendable? My application
will not be used by that many, especially not at the same time. I know that
a ConnectionPool is more appropriate, but I don't have the time to learn how
to do it. (The Hunter's servlet book said it was fairly complicated.. :)
Second, I have a problem with the AutoCommit setting. If I don't touch it at
all (ie remarking the if clause in the code above) it works great. On the
other hand, if I put setAutoCommit(true) and try to execute a query, an
empty ResultSet is returned. Why? I can't see the differenct between turning
AutoCommit on and doing nothing at all, which, AFAIK, actually IS AutoCommit
on...
Any comments appreciated. Thanks!
/Jon
Jon, sm3ojr
European distributor of the following products:
* TR Logging Program (by N6TR)
* WJ2O Master QSO Logging Program (by WJ2O)
* RTTY (by WF1B)
---
Jonit Software, Box 178, SE-83122 OSTERSUND, Sweden
Phone: +46-63-57 21 21
Fax / 24h order: +46-63-57 21 22
Mobile: +46-70-569 21 21
E-mail: [EMAIL PROTECTED]
URL: http://www.jonit.com
ICQ: 7319834
---
___________________________________________________________________________
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