add database-aware MySQL pooling
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
namespace {
|
||||
|
||||
const u64 UCE_UNIT_ABI_VERSION = 10;
|
||||
const u64 UCE_UNIT_ABI_VERSION = 11;
|
||||
|
||||
struct SharedUnitFilesystemState
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
#include "mysql-connector.h"
|
||||
|
||||
// Same-credential mysql_connect() calls lease one request-local connection.
|
||||
// Same-target mysql_connect() calls lease one request-local connection.
|
||||
// mysql_disconnect() releases a lease; cleanup_mysql_connections() owns the
|
||||
// actual close at request end, including exception/fatal recovery paths.
|
||||
static void mysql_register_request_connection(MySQL* db)
|
||||
@@ -15,8 +15,12 @@ static void mysql_register_request_connection(MySQL* db)
|
||||
connections.push_back(db);
|
||||
}
|
||||
|
||||
bool MySQL::connect(String host, String username, String password)
|
||||
bool MySQL::connect(String host, String username, String password, String database)
|
||||
{
|
||||
request_host = host;
|
||||
request_username = username;
|
||||
request_password = password;
|
||||
request_database = database;
|
||||
// Register regardless of outcome: tracking is about the wrapper's
|
||||
// lifetime, not the connection's. disconnect()/~MySQL() unregister.
|
||||
mysql_register_request_connection(this);
|
||||
@@ -32,7 +36,7 @@ bool MySQL::connect(String host, String username, String password)
|
||||
}
|
||||
|
||||
if (mysql_real_connect((MYSQL*)connection, host.c_str(), username.c_str(), password.c_str(),
|
||||
NULL, 0, NULL, 0) == NULL)
|
||||
database == "" ? NULL : database.c_str(), 0, NULL, 0) == NULL)
|
||||
{
|
||||
auto e = mysql_error((MYSQL*)connection);
|
||||
fprintf(stderr, "%s\n", e);
|
||||
@@ -59,7 +63,17 @@ bool MySQL::connect(String host, String username, String password)
|
||||
|
||||
bool MySQL::reset_connection()
|
||||
{
|
||||
if(!connection || mysql_reset_connection((MYSQL*)connection) != 0)
|
||||
if(!connection)
|
||||
return(false);
|
||||
MYSQL* mysql = (MYSQL*)connection;
|
||||
String selected_database = mysql->db == NULL ? "" : mysql->db;
|
||||
// RESET CONNECTION deliberately preserves the selected database. An
|
||||
// unqualified lease cannot safely inherit one selected by its prior request.
|
||||
if(request_database == "" && selected_database != "")
|
||||
return(false);
|
||||
if(mysql_reset_connection(mysql) != 0)
|
||||
return(false);
|
||||
if(request_database != "" && selected_database != request_database && mysql_select_db(mysql, request_database.c_str()) != 0)
|
||||
return(false);
|
||||
_preload_next_error_code = 0;
|
||||
affected_rows = 0;
|
||||
|
||||
@@ -27,10 +27,11 @@ struct MySQL {
|
||||
String request_host;
|
||||
String request_username;
|
||||
String request_password;
|
||||
String request_database;
|
||||
|
||||
std::vector<MySQLFieldInfo> field_info;
|
||||
|
||||
bool connect(String host = "localhost", String username = "root", String password = "");
|
||||
bool connect(String host = "localhost", String username = "root", String password = "", String database = "");
|
||||
bool reset_connection();
|
||||
void disconnect();
|
||||
String error();
|
||||
@@ -46,14 +47,14 @@ struct MySQL {
|
||||
|
||||
};
|
||||
|
||||
inline MySQL* mysql_connect(String host = "localhost", String username = "root", String password = "")
|
||||
inline MySQL* mysql_connect(String host = "localhost", String username = "root", String password = "", String database = "")
|
||||
{
|
||||
if(context)
|
||||
{
|
||||
for(void* raw : context->resources.mysql_connections)
|
||||
{
|
||||
MySQL* db = (MySQL*)raw;
|
||||
if(db && db->request_pooled && db->connection && db->request_host == host && db->request_username == username && db->request_password == password)
|
||||
if(db && db->request_pooled && db->connection && db->request_host == host && db->request_username == username && db->request_password == password && db->request_database == database)
|
||||
{
|
||||
db->request_leases++;
|
||||
return(db);
|
||||
@@ -67,7 +68,8 @@ inline MySQL* mysql_connect(String host = "localhost", String username = "root",
|
||||
db->request_host = host;
|
||||
db->request_username = username;
|
||||
db->request_password = password;
|
||||
db->connect(host, username, password);
|
||||
db->request_database = database;
|
||||
db->connect(host, username, password, database);
|
||||
return(db);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user