question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"No operations allowed after connection closed." When using with ktor Server

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
virusbearcommented, Jan 21, 2020

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.

0reactions
virusbearcommented, Jan 29, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No operations allowed after connection closed - Stack Overflow
I have build an application, maintained with session. It uses JDBC & mysql. When the application is deployed on server (Apache Tomcat 6)....
Read more >
KotlinJS Ktor server "Connection closed without response ...
After shuffling the order of operations around so that my static resources declaration came first, I started seeing the following error in the...
Read more >
KB-1132 "No operations allowed after connection closed ...
This may be caused by a separate datasource which opens a large number of idle connections that are not automatically cleaned up. If...
Read more >
Creating and configuring a client - Ktor
Note that the close function prohibits creating new requests but doesn't terminate currently active ones. Resources will only be released after ...
Read more >
Creating a WebSocket chat client - Ktor
The client application will allow users to join a common chat server, send messages to other users, and see messages from other users...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found