Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite3 java.lang.IllegalStateException: SQLite JDBC: inconsistent internal state #291

Closed
kc14 opened this issue Apr 25, 2019 · 10 comments
Closed

Comments

@kc14
Copy link

kc14 commented Apr 25, 2019

I am using:

  • sqlline-1.7.0-jar-with-dependencies.jar
  • sqlite-jdbc-3.27.2.jar

I can connect to the database, create a table, select from the table, insert into the table, but the first time I try to read after an insert I get the following exception:

0: jdbc:sqlite:/maxmin> SELECT * FROM maxmind_build_specs ORDER by pdate DESC ;
java.lang.IllegalStateException: SQLite JDBC: inconsistent internal state
at org.sqlite.core.CoreResultSet.checkCol(CoreResultSet.java:88)
at org.sqlite.jdbc3.JDBC3ResultSet.getTableName(JDBC3ResultSet.java:926)
at sqlline.Rows.isPrimaryKey(Rows.java:119)
at sqlline.TableOutputFormat.getOutputString(TableOutputFormat.java:114)
at sqlline.TableOutputFormat.getOutputString(TableOutputFormat.java:98)
at sqlline.TableOutputFormat.print(TableOutputFormat.java:40)
at sqlline.SqlLine.print(SqlLine.java:1626)
at sqlline.Commands.execute(Commands.java:982)
at sqlline.Commands.sql(Commands.java:906)
at sqlline.SqlLine.dispatch(SqlLine.java:740)
at sqlline.SqlLine.begin(SqlLine.java:557)
at sqlline.SqlLine.start(SqlLine.java:270)
at sqlline.SqlLine.main(SqlLine.java:201)
0: jdbc:sqlite:/maxmin>

Is anybody else experiencing the same problem?

I just found this on stackoverflow:

https://stackoverflow.com/questions/48269807/sqlite-jdbc-inconsistent-internal-state-when-attempting-to-getcolumncount-fro

But I don't know what to do.

Any help appreciated.

@julianhyde
Copy link
Owner

I think you should log a bug against SQLite. Please post the URL here. Maybe the SQLite developers can suggest a workaround.

@kc14
Copy link
Author

kc14 commented Apr 26, 2019

I looked a little bit more into the issue and it seems, that the SQLite JDBC driver throws this exception, when you try to access the Metadata of a ResultSet after you have iterated thru the result set.

I found this solution (see Sqlite problem on OsX: inconsistent internal state. Help!):

the loop
while(rs.next()) {
will close/invalidate the md MetaResultset object, so that the subsequent call to md (to retrieve the column names) has sqlite complaining about the request to this invalidated object. One simple fix is to cache the column names before going through the tuples loop:

// cache column names because the last while(rs.next()) { iteration for the tuples below will close the md object:   
Vector<String> columnNames = new Vector<String>(); 
for (int i = 0; i < numAttributes; i++) { 
    columnNames.add(md.getColumnName(i + 1)); 
} 

[here goes the tuples loop]

The suggested solution is to make a copy of the needed metadata before using the resultSet. Could this be an issue here? If you can point me to some lines of code in sqlline that iterate thru a result set, I would check out the source code and try that solution.

The JDBC standard really allows a resultSet to be closed and invalidated when next() returns false.

So this may be a problem here?

@julianhyde
Copy link
Owner

Can you provide evidence that the JDBC specification allows the result set to be closed by calling ‘next’?

This is a bug in the SQLite driver. Let’s be clear about that. And please make sure a bug is logged.

@kc14
Copy link
Author

kc14 commented Apr 26, 2019

I looked at the oracle doc of java:

ResultSet.next

There it says:

When a call to the next method returns false, the cursor is positioned after the last row. Any invocation of a ResultSet method which requires a current row will result in a SQLException being thrown.

Of course there is room for interpretation ...

@julianhyde
Copy link
Owner

It's pretty clear to me. The result set is not closed. You are free (subject to scrollability constraints) to re-position the result set on a valid row (say by calling previous()).

@kc14
Copy link
Author

kc14 commented Apr 27, 2019

I have posted the problem in the sqlite-users mailing list as suggested on their Bug Reports Page. Their bug tracking system is no longer open to the public, so I cannot post an URL. But I have taken the same subject for the post as in this issue.

@kc14
Copy link
Author

kc14 commented Apr 30, 2019

Seems to be a little hard to get a valid response from the sqllite mailing list ... they asked me if I had closed the db correctly ... but I think that is not the problem.

I looked into the source code of the JDBC driver code following the stacktrace. It looks like we have a resultSet but no metainfo for the columns ... the variable colsMeta seems to be null ... strange ...

I posted the following text to the sqllite mailing list, hoping somebody who knows the source code may respond:

I looked into the JDBC driver source code and found the following two code lines mentioned in the stack trace.

Sqlline is trying to get the tablename of the resultSet which results in the invocation of:

https://github.com/xerial/sqlite-jdbc/blob/77ae7d2ddefe679fd0432eb0858bd8aab104b795/src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java#L926

Which in turn calls:

https://github.com/xerial/sqlite-jdbc/blob/14839bae0ceedff805f9cda35f5e52db8c4eea88/src/main/java/org/sqlite/core/CoreResultSet.java#L86

Here we see, that colsMeta == null results in throwing the seen exception.

But how can colsMeta be null in a valid resultSet?

Does anybody have a deeper understanding how this can happen in the sqlite3 code?

Thanks for any help in advance on this matter.

@julianhyde
Copy link
Owner

How about posting an issue to https://github.com/xerial/sqlite-jdbc/issues? That seems to be the place, since this concerns the JDBC driver rather than SQLite itself. I did a quick scan and it does not seem that this issue is already logged.

@kc14
Copy link
Author

kc14 commented May 6, 2019

You are right. Got the same hint from the mailing list:

This has nothing to do with the sqlite3 code itself but judging by the
comments in the jdbc binding, colsMeta being null implies that the
ResultSet has been closed. But I'm not sure where colsMeta is initialised,
it appears some other class is responsible for that.

So I opened an issue in the sqlite JDBC project:

@julianhyde
Copy link
Owner

I believe this is a duplicate of #379. Therefore marking fixed as of e0f9e9a.

If the problem still exists, please re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants