I am using Vapor framework form server-side back-end using Swift in Ubuntu. In the 80% project done and this framework not reconnecting database automatically. I just need solution to reconnect database at least manually. In worst time, the database is automatically closed on the client demo. Because of this all my work gone ruin. I tried below but it does not work. I’m really frustrated that without db_reconnect function the whole work and the whole framework is just garbage. I’ve to move for Laravel.
I’ve stored the database in a class which is used as common to query from database on the configure.swift
app.databases.use(.mysql(configuration: .prod), as: .mysql)
do {
try await app.db.withConnection({ database in
Database.initialize(database)
})
} catch {
fatalError("Unable to connect database")
}
However, sometimes query result comes with database disconnect error. So, I try to reconnect the database with same db which is initialized previously. I’ve got Connected printed in the console continuously which means the db is actually not connected. That is why it comes to this function after executing some queries.
private func connect_db(_ completionHandler:@escaping(Error?) -> Void){
print("Reconnecting Database....")
if let db = self.db as? FluentKit.Database {
Task.detached(priority: .userInitiated) {
do {
try await db.withConnection({ _ in
print("Connected")
completionHandler(nil)
})
} catch {
print("Unable to connect!")
completionHandler(error)
}
}
} else {
completionHandler(SERVER_ERROR("Invalid DB", info: "The Database isn't Database object", code: 28))
}
}
Try 2:
private func connect_db(_ completionHandler:@escaping(Error?) -> Void){
print("Reconnecting Database....")
Task.detached(priority: .userInitiated) {
do {
try await kAPI.app.db.withConnection { database in
Database.initialize(database)
print("Connected")
completionHandler(nil)
}
} catch {
completionHandler(error)
}
}
}
I’ve used connect_db
on this function
func query(_ query: String, with continuation: CheckedContinuation<[MySQLRow]?, Error>) {
self.db.query(query).whenComplete { result in
switch result {
case .success(let rows):
if rows.isEmpty {
continuation.resume(returning: nil)
} else {
continuation.resume(returning: rows)
}
case .failure(let error):
let error = error as NSError
print("Error: (error)")
if error.code == 11 { // Disconnected
self.connect_db { error in
if let error = error {
continuation.resume(throwing: error)
} else {
self.query(query, with: continuation)
}
}
} else {
continuation.resume(throwing: error)
}
}
}
}
How can I fix this issue?
2
Answers
I figured out with help of
ChatGPT
but I don't want to answer (coz gpt answer) either I don't want to continue with vapor project.Vapor is just project for kids to understand how web server works. It doesn't mean that it is used for real time production server. That's why it doesn't has proper documentation.
I've moved to laraval PHP which is great for professional server. Not only vapor but never ever choose the immature frameworks for software projects. These kinda frameworks are overhyped and promoted with fanbase. Apple is biggest corporate in the world, because of their selling point, So fancy promotion is must required job.
We really need good alternative, native and real webserver framework. But meanwhile very matured frameworks like Laraval PHP is useful for your server.
If you still choose immature frameworks, you endup with bunch of time wasted and days ruined. Beaware of these kind of traps.
I have never had a database disconnect on me in all the time I have been using Vapor. This is my standard connection code: