You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MySQLConnection closes unexpectedly in a Swift Command Line Tool that I am writing.
To Reproduce
import Foundation
import MySQLNIO
@main
struct InsertValuesIntoMySQLDB {
static func main() {
do {
try insertRecords()
} catch {
print("There was an error!")
print(error.localizedDescription)
}
}
static func insertRecords() throws {
let addr = try SocketAddress.makeAddressResolvingHost(
"database.domain.com",
port: 3306
)
let eventGroup = MultiThreadedEventLoopGroup(numberOfThreads: 4)
let eventLoop = eventGroup.next()
let conn = try MySQLConnection.connect(to: addr, username: "admin", database: "quora", password: "xyzzy", tlsConfiguration: nil, on: eventLoop).wait()
defer {
do {
try conn.close().wait()
} catch {
print("Error: \(error)")
}
}
let rows = try conn.simpleQuery("SELECT COUNT(*) from questions").wait()
print("rows: \(rows.count)")
// try eventGroup.syncShutdownGracefully()
}
}
Steps to reproduce the behavior:
When I run the code I get:
rows: 0 (should be > 0)
Error: alreadyClosed
The query clearly reaches the database server since a misspelled table name generates an error (MySQL error: Server error: Table 'quora.question' doesn't exist), but when the SQL is valid, I get no response.
If I uncomment the last line (try eventGroup.syncShutdownGracefully), then I get the following error:
ERROR: Cannot schedule tasks on an EventLoop that has already shut down. This will be upgraded to a forced crash in future SwiftNIO versions.
Environment
OS version: macOS Sonoma 14.3
This is my first time trying to use mysql-nio as well as swift-no, therefore I could be totally wrong on how to use these packages, but I have run out of ideas. Thanks in advance for any kind of help you could provide.
The text was updated successfully, but these errors were encountered:
@huibert7 sorry for the delay. There are a few things that you should improve with your code to start debugging this.
First I strongly suggest you start using the async APIs (and using .get() where these aren't available. That will avoid any issues around waiting on event loops. Second, you're creating a new event loop group on every insert which is a bad idea. You should ideally use the singleton event loop group that's managed for you.
Those 2 suggestions might fix your issue, but feel free to post if it doesn't help or produces a different error
Describe the bug
MySQLConnection closes unexpectedly in a Swift Command Line Tool that I am writing.
To Reproduce
Steps to reproduce the behavior:
When I run the code I get:
rows: 0 (should be > 0)
Error: alreadyClosed
The query clearly reaches the database server since a misspelled table name generates an error (MySQL error: Server error: Table 'quora.question' doesn't exist), but when the SQL is valid, I get no response.
If I uncomment the last line (try eventGroup.syncShutdownGracefully), then I get the following error:
ERROR: Cannot schedule tasks on an EventLoop that has already shut down. This will be upgraded to a forced crash in future SwiftNIO versions.
Environment
This is my first time trying to use mysql-nio as well as swift-no, therefore I could be totally wrong on how to use these packages, but I have run out of ideas. Thanks in advance for any kind of help you could provide.
The text was updated successfully, but these errors were encountered: