11 messages in com.mysql.lists.javaRe: JDBC ResultSet exception
FromSent OnAttachments
Bjoern Wuest29 Apr 2004 23:59 
Rhino30 Apr 2004 07:39 
Bjoern Wuest30 Apr 2004 08:16 
Jae Joo30 Apr 2004 08:28 
Rhino30 Apr 2004 08:53 
Bjoern Wuest30 Apr 2004 09:26 
Bjoern Wuest30 Apr 2004 09:30 
Morten Norby Larsen30 Apr 2004 09:45 
Bjoern Wuest30 Apr 2004 10:11 
Allen Weeks30 Apr 2004 10:19 
Morten Norby Larsen03 May 2004 02:33 
Subject:Re: JDBC ResultSet exception
From:Morten Norby Larsen (mor@magisterludi.com)
Date:05/03/2004 02:33:04 AM
List:com.mysql.lists.java

Right, you definitely need to close the statement after use - this is where for instance ODBC gets very fuzzy if you don't, while Connector/(J is more forgiving. Sorry for my sloppy advice.

Morten

Allen Weeks wrote:

If I am not mistaken, for what you are attempting you must also close the statement varible as well. You could them use the same names again for both Statement and ResultSet varibles.

Hope this helps

-----Original Message----- From: Morten Norby Larsen [mailto:mor@magisterludi.com] Sent: Friday, April 30, 2004 9:46 AM To: bjoe@gmx.net Cc: ja@lists.mysql.com Subject: Re: JDBC ResultSet exception

You use the same Statement instance twice. I am not sure that is allowed by the JDBC spec, but I am talking out of the top of my head, so please do not take it for more than it is.

However, it wouldn't hurt closing the statement object and create a new one for your next query. Probably also more portable (ODBC is fuzzy about these issues).

Hope this helps, Morten

Bjoern Wuest wrote:

Dear all

Due to some discussion I decided to reformulate a previous request of o problem I have with MySQL and JDBC.

My problem:

I work on a small application with database support. For

development I use

the MySQL database which is just doing fine. During testing I

experienced a,

in my opinion, strange bug.

I have the following source code:

Connection conn = ... // get JDBC connection object to MySQL

database from

somewhere try { Statement stmt = null; ResultSet rSet = null; try { // Create statement from connection stmt = conn.createStatement(); // Read some data from database (works perfect) rSet = stmt.executeQuery("select v from config where k='time'"); rSet.next(); String ttl = rSet.getString("v"); rSet.close(); rSet = null; // Read some other data from database rSet = stmt.executeQuery("select v from config where k='location'"); while (rSet.next()) { // Do something in here } rSet.close(); } catch (SQLException Ex) { // Notify about exception } finally { // Do some cleanup work if (rSet != null) { try { rSet.close(); } catch (SQLException Ignore) {} rSet = null; } if (stmt != null) { try { stmt.close(); } catch (SQLException Ignore) {} stmt = null; } } } finally { // Close the JDBC connection to the MySQL database if (conn != null) { try { conn.close(); } catch (SQLException

Ignore) {} }

}

<<<<<<<<<<<<<<<<<<

Now the problem I encounter:

The first query ("select v from config where k='time'") is

executed without

any problems. I can read the returned value and save in 'ttl'.

Then, I close

this result set and leave it to the garbage collector. Next, I

want to use

the same variable to take the result set of another query

("select v from

config where k='location'") which is executed as well. But then, when I execute "while (rSet.next())" I get the following exception:

java.sql.SQLException: Operation not allowed after ResultSet closed at com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:4579) at com.mysql.jdbc.ResultSet.next(ResultSet.java:2423) at

net.sos.web.services.mapping.CMySQLMappingService$1.run(CMySQLMapp ingService

.java:323) at java.lang.Thread.run(Unknown Source)

<<<<<<<<<<<<<<<<<<

"while (rSet.next())" is on line 323 in an anonymous class

implementation of

java.lang.Runnable, but I think this is not the reason for the

exception.

I use Connection/J in version 3.1.1alpha and MySQL 5.0.0alpha (I have to because of some features unique to MySQL 5). The JDK I use is 1.4.2_02

I hope someone can help me since I have really no clue why this is happening. Temporarily I don't close the result set but this

would result in

a lot of memory consumption in my system.

Thanks Bjoern

http://lists.mysql.com/java?unsub=alle@starband.net