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

exec(String) throws SQLException: ResultSet already requested #414

Closed
jeiea opened this issue Oct 26, 2018 · 8 comments
Closed

exec(String) throws SQLException: ResultSet already requested #414

jeiea opened this issue Oct 26, 2018 · 8 comments
Assignees

Comments

@jeiea
Copy link

jeiea commented Oct 26, 2018

I wrote the following code and was stuck in exec(query). The backend was sqlite.

private fun Transaction.getTables(): MutableList<String> {
  val sqliteQuery = "SELECT name FROM sqlite_master WHERE type='table'"
  val postgreQuery = "SELECT table_name FROM information_schema.tables WHERE table_schema='public';"
  val query = if (isSqlite) sqliteQuery else postgreQuery
  return execStringColumn(query)
}

private fun Transaction.execStringColumn(query: String): MutableList<String> {
  val fields = mutableListOf<String>()
  exec(query) { cursor ->
    while (cursor.next()) {
      fields.add(cursor.getString(0))
    }
  }
  return fields
}

The above code throws the following exception.

INFO Exposed - Transaction attempt #2 failed: java.sql.SQLException: ResultSet already requested. Statement(s): SELECT name FROM sqlite_master WHERE type='table'
org.jetbrains.exposed.exceptions.ExposedSQLException: java.sql.SQLException: ResultSet already requested
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed(Statement.kt:61)
...

And followings are stacktraces of each call to getResultSet.

getResultSet:151, JDBC3Statement (org.sqlite.jdbc3)
executeQuery:85, JDBC3PreparedStatement (org.sqlite.jdbc3)
executeQuery:52, ProxyPreparedStatement (com.zaxxer.hikari.pool)
executeQuery:-1, HikariProxyPreparedStatement (com.zaxxer.hikari.pool)
executeInternal:104, Transaction$exec$2 (org.jetbrains.exposed.sql)
executeIn$exposed:59, Statement (org.jetbrains.exposed.sql.statements)
exec:128, Transaction (org.jetbrains.exposed.sql)
exec:122, Transaction (org.jetbrains.exposed.sql)
exec:101, Transaction (org.jetbrains.exposed.sql)
getResultSet:133, JDBC3Statement (org.sqlite.jdbc3)
getResultSet:214, ProxyStatement (com.zaxxer.hikari.pool)
getResultSet:-1, HikariProxyPreparedStatement (com.zaxxer.hikari.pool)
executeInternal:107, Transaction$exec$2 (org.jetbrains.exposed.sql)
executeIn$exposed:59, Statement (org.jetbrains.exposed.sql.statements)
exec:128, Transaction (org.jetbrains.exposed.sql)
exec:122, Transaction (org.jetbrains.exposed.sql)
exec:101, Transaction (org.jetbrains.exposed.sql)

Did I have any mistake on executing raw SQL?

@Tapac
Copy link
Contributor

Tapac commented Oct 26, 2018

The one thing that comes in mind is that you share transaction between threads and access connection concurrently. Or maybe you run SQLite in single-thread mode (https://www.sqlite.org/threadsafe.html) ?

Does the same exception happen on PSQL?

@jeiea
Copy link
Author

jeiea commented Oct 26, 2018

I don't know how to check SQLite threading mode, but I set TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE and other DSL/DAO api works well.
With postgres other kind of exception happens, maybe due to incorrect implementation. In other words, not happens.

@Tapac
Copy link
Contributor

Tapac commented Dec 13, 2018

@jeiea , could you provide a reproducible sample for that issue?

@Tapac Tapac added waiting for reply Additional information required sqlite labels Dec 13, 2018
@jeiea
Copy link
Author

jeiea commented Dec 13, 2018

exposed-sqlite.zip
@Tapac Here it is.

@Tapac
Copy link
Contributor

Tapac commented Jan 10, 2019

Thank you, your sample helps a lot. Fixed in a master branch.

@Tapac Tapac closed this as completed Jan 10, 2019
@Tapac Tapac added bug and removed waiting for reply Additional information required labels Jan 10, 2019
@Tapac Tapac self-assigned this Jan 10, 2019
@riyadparvez
Copy link

I am seeing the same issue in postgres 9.6.

@Tapac
Copy link
Contributor

Tapac commented Feb 28, 2019

@riyadparvez , on the latest Exposed version?

@riyadparvez
Copy link

Yes I am still seeing this exception version 0.12.2. Here's the full stack-trace

[main] WARN Exposed - Transaction attempt #2 failed: This ResultSet is closed.. Statement(s): null
org.postgresql.util.PSQLException: This ResultSet is closed.
        at org.postgresql.jdbc.PgResultSet.checkClosed(PgResultSet.java:2741)
        at org.postgresql.jdbc.PgResultSet.next(PgResultSet.java:1829)
        at petals.util.ResultIterator.hasNext(ExposedUtils.kt:19)
        at kotlin.sequences.TransformingSequence$iterator$1.hasNext(Sequences.kt:176)
        at kotlin.sequences.SequencesKt___SequencesKt.toCollection(_Sequences.kt:698)
        at kotlin.sequences.SequencesKt___SequencesKt.toMutableList(_Sequences.kt:728)
        at kotlin.sequences.SequencesKt___SequencesKt.toList(_Sequences.kt:719)
        at petals.FieldWeatherLookup$fieldWeatherLookup$weatherReadings$1.invoke(FieldWeatherLookup.kt:145)
        at petals.FieldWeatherLookup$fieldWeatherLookup$weatherReadings$1.invoke(FieldWeatherLookup.kt:86)
        at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:104)
        at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:75)
        at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:58)
        at petals.FieldWeatherLookup.fieldWeatherLookup(FieldWeatherLookup.kt:97)
        at petals.PetalsKt.main(petals.kt:51)
Exception in thread "main" org.postgresql.util.PSQLException: This ResultSet is closed.
        at org.postgresql.jdbc.PgResultSet.checkClosed(PgResultSet.java:2741)
        at org.postgresql.jdbc.PgResultSet.next(PgResultSet.java:1829)
        at petals.util.ResultIterator.hasNext(ExposedUtils.kt:19)
        at kotlin.sequences.TransformingSequence$iterator$1.hasNext(Sequences.kt:176)
        at kotlin.sequences.SequencesKt___SequencesKt.toCollection(_Sequences.kt:698)
        at kotlin.sequences.SequencesKt___SequencesKt.toMutableList(_Sequences.kt:728)
        at kotlin.sequences.SequencesKt___SequencesKt.toList(_Sequences.kt:719)
        at petals.FieldWeatherLookup$fieldWeatherLookup$weatherReadings$1.invoke(FieldWeatherLookup.kt:145)
        at petals.FieldWeatherLookup$fieldWeatherLookup$weatherReadings$1.invoke(FieldWeatherLookup.kt:86)
        at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:104)
        at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:75)
        at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:58)
        at petals.FieldWeatherLookup.fieldWeatherLookup(FieldWeatherLookup.kt:97)

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

No branches or pull requests

3 participants