Reuse MySQL connections within requests
This commit is contained in:
+14
-11
@@ -21,6 +21,11 @@ struct MySQL {
|
||||
u64 insert_id = 0;
|
||||
String statement_info = ""; //
|
||||
bool request_cleanup_delete = false;
|
||||
bool request_pooled = false;
|
||||
u32 request_leases = 0;
|
||||
String request_host;
|
||||
String request_username;
|
||||
String request_password;
|
||||
|
||||
std::vector<MySQLFieldInfo> field_info;
|
||||
|
||||
@@ -33,26 +38,24 @@ struct MySQL {
|
||||
DValue query(String q, StringMap params);
|
||||
DValue get_pending_result();
|
||||
|
||||
// Unregisters from request connection tracking, so stack-allocated instances
|
||||
// cannot leave dangling pointers behind for request cleanup. Heap handles
|
||||
// returned by mysql_connect() are also request-owned and auto-closed at the
|
||||
// end of the request if user code does not call mysql_disconnect() first.
|
||||
// Unregisters non-pooled instances from request tracking. mysql_connect()
|
||||
// handles remain request-owned until cleanup after their leases are released.
|
||||
~MySQL() { disconnect(); }
|
||||
|
||||
};
|
||||
|
||||
inline MySQL* mysql_connect(String host = "localhost", String username = "root", String password = "")
|
||||
{
|
||||
MySQL* m = new MySQL();
|
||||
m->request_cleanup_delete = true;
|
||||
m->connect(host, username, password);
|
||||
return(m);
|
||||
}
|
||||
MySQL* mysql_connect(String host = "localhost", String username = "root", String password = "");
|
||||
|
||||
inline void mysql_disconnect(MySQL* m)
|
||||
{
|
||||
if(!m)
|
||||
return;
|
||||
if(m->request_pooled)
|
||||
{
|
||||
if(m->request_leases > 0)
|
||||
m->request_leases--;
|
||||
return;
|
||||
}
|
||||
m->disconnect();
|
||||
delete m;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user