Jon,
I'd really look at using a connection pool. I'm using a modified version
from core Servletes and JavaServer Pages (Marty Hall). There are others to
choose from (check the archive). In my experience they are not that
complicated to use.
You mention that you don't expect to deal with multiple concurrent users.
With your current design if two request did come simultaneously the same
connection would
be in use and results would be unpredictable.
Not sure about why your not getting a ResultSet im afraid. Connections are
in
auto-commit by default.
Oh and you don't need to session.putValue() - it's still the same
connection.
hth
Graham
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Jonathan
Silvergran, SM3OJR
Sent: 05 January 2001 14:11
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