"No operations allowed after connection closed." When using with ktor Server
See original GitHub issueI am trying to develop a small ktor server that receives a post request with a CSV file as multipart and inserts all rows into a MySQL database. Everything works fine except that the second request to the server always crashes with the following stack trace:
org.jetbrains.exposed.exceptions.ExposedSQLException: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed(Statement.kt:48)
at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:130)
at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:124)
at org.jetbrains.exposed.sql.Query.iterator(Query.kt:309)
this is the part of the stack trace after the insert is called by exposed.
at first, I created my SQL connection using URL and password, but now I decided to go with javax.sql.DataSource implementation where I just implemented all getConnection overloads.
after some debugging and searching around in the exposed library I found the following piece of code:
private fun doConnect(getNewConnection: () -> Connection, setupConnection: (Connection) -> Unit = {}, manager: (Database) -> TransactionManager = { ThreadLocalTransactionManager(it, DEFAULT_ISOLATION_LEVEL, DEFAULT_REPETITION_ATTEMPTS) } ): Database {
return Database {
getNewConnection().apply { setupConnection(this) }
}.apply {
TransactionManager.registerManager(this, manager(this))
}
}
Here getNewConnection is only called once and not passed to the transaction manager.
is it possible, that this prevents opening a new connection to MySQL when a previous got closed?
Connection creation:
val connection: Database = Database.connect(MySqlDataSource())
MySqlDataSource:
override fun getConnection(): Connection {
return getConnection("root", "")
}
override fun getConnection(username: String?, password: String?): Connection {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", username, password)
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
Unfortunately, I am not allowed to publish the code for the project, but I will try to create an example to reproduce this issue and publish this one.
Thank you for your help. Updating to the newest version did indeed help. just really strange why gradle didn’t suggest updating to a newer version.