sqlite-jdbc icon indicating copy to clipboard operation
sqlite-jdbc copied to clipboard

call of statement.getMoreResults() breaks the statement

Open sorontur opened this issue 5 years ago • 1 comments

Hi all,

I have a PreparedStatement but when calling getMoreResults() on it, it is closed as a side effect. Maybe we are using it wrong, but the javadoc of the method states: "Moves to this Statement object's next result, returns true if it is a ResultSet object, andimplicitly closes any current ResultSetobject(s) obtained with the method getResultSet. " As far as I see, closing the result set is fine, but closing the statement is not. I checked your code and you call in the org.sqlite.jdbc3.JDBC3Statement.getMoreResults() the close of the statment. I think this is wrong. Please check why getMoreResults always closes the statement and returns false everytime.

Here is a minimum sample to see the error:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Sample {
	public static void main(String[] args) {
		Connection connection = null;
		try {
			connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
			Statement statement = connection.createStatement();
			statement.executeUpdate("drop table if exists person");
			statement.executeUpdate("create table person (id integer, name string)");
			statement.executeUpdate("insert into person values(1, 'leo')");
			statement.executeUpdate("insert into person values(2, 'yui')");
			statement.close();
			PreparedStatement preparedStatement = connection.prepareStatement("select * from person where id=?");
			preparedStatement.clearParameters();
			preparedStatement.setInt(1, 1);
			preparedStatement.execute();
			ResultSet rs = preparedStatement.getResultSet();
			while (rs.next()) {
				System.out.println("name = " + rs.getString("name"));
				System.out.println("id = " + rs.getInt("id"));
			}
			rs.close();
			preparedStatement.getMoreResults();
			preparedStatement.getUpdateCount();
			preparedStatement.clearParameters();
			preparedStatement.setInt(1, 2);
			preparedStatement.execute();
			ResultSet rs2 = preparedStatement.getResultSet();
			while (rs2.next()) {
				System.out.println("name = " + rs2.getString("name"));
				System.out.println("id = " + rs2.getInt("id"));
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
		} finally {
			try {
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				System.err.println(e.getMessage());
			}
		}
	}
}

Thanks for help! Patrick

sorontur avatar Oct 21 '19 12:10 sorontur

~~Is this still happening on the latest version?~~

gotson avatar Jul 28 '22 08:07 gotson

🎉 This issue has been resolved in 3.39.4.0 (Release Notes)

github-actions[bot] avatar Nov 07 '22 10:11 github-actions[bot]

It seems this fix has introduced a new regression: https://github.com/xerial/sqlite-jdbc/issues/809

lukaseder avatar Nov 16 '22 16:11 lukaseder