I am looking for a database connection pool wrapper for perfect (swift 3 or above). I couldn't find any through search so I decided to ask if someone could provide a hint.
Thanks in advance
I have implemented a simple pool mechanism based on the last part of @PerfectlyRock's answer, check it out:
import PerfectMySQL
import Foundation
import PerfectThread
#if os(Linux)
import Glibc
#else
import Darwin
#endif
class DBManager {
struct MySQLPoolItem {
let mysql = MySQL()
let lock = Threading.Lock()
func connect()->Bool{
return mysql.connect(host: Prefs.dbhost, user: Prefs.dbuser, password: Prefs.dbpass, db: Prefs.schema)
}
}
var pool: [MySQLPoolItem] = []
static let shared = DBManager()
func preparePool() {
var connectionCount = Prefs.connectionPoolCount
while connectionCount > 0 {
let item = MySQLPoolItem()
_ = item.mysql.setOption(.MYSQL_SET_CHARSET_NAME, "utf8")
let connected = item.connect()
guard connected else {
// verify we connected successfully
print(item.mysql.errorMessage())
return
}
print("Database connection \((connectionCount)) success");
pool.append(item)
connectionCount -= 1
}
}
func getAvailableConnection() -> MySQLPoolItem {
var item : MySQLPoolItem? = nil;
while item == nil {
item = tryAvailableConnection()
if(item == nil){
sleep(1)
}
}
return item!
}
private func tryAvailableConnection() -> MySQLPoolItem? {
for item in pool {
if(item.lock.tryLock()){
if(!item.mysql.ping()){
if(item.connect()){
return item
}
item.lock.unlock()
}else{
return item
}
}
}
return nil
}
func runSync(query: String) -> ((result:Bool,items:MySQL.Results?)) {
let poolItem = getAvailableConnection()
defer {
poolItem.lock.unlock()
}
let querySuccess = poolItem.mysql.query(statement: query)
return (querySuccess,poolItem.mysql.storeResults())
}
}
I would rather say high efficient re-use connection other than a pool. Take Perfect-MySQL example:
By using Threading.Lock(), you can easily queue all queries into one connection and maximize the efficiency - fast, atomic, ordered, shared and singleton.
If insisted, you can create a limited number of connections to an array, and each connection keeps one lock to make a balance, let say, a threaded connection pool, so I believe this balanced solution can be established without any special module on Perfect itself: