Document request-scoped MySQL cleanup
This commit is contained in:
parent
6bdccacf02
commit
0ff42f357f
@ -15,6 +15,8 @@ Establishes a connection to a MySQL server and returns a pointer to the connecti
|
|||||||
|
|
||||||
This connection handle is then used with helpers such as `mysql_query()`, `mysql_error()`, and `mysql_disconnect()`.
|
This connection handle is then used with helpers such as `mysql_query()`, `mysql_error()`, and `mysql_disconnect()`.
|
||||||
|
|
||||||
|
MySQL handles are request-scoped framework resources. If a request exits without calling `mysql_disconnect()`, UCE closes any remaining MySQL handles during request cleanup. Call `mysql_disconnect()` when you want to close early, but never store a `MySQL*` in globals, sessions, or other state that can outlive the current request.
|
||||||
|
|
||||||
:example
|
:example
|
||||||
MySQL* db = mysql_connect();
|
MySQL* db = mysql_connect();
|
||||||
print(db != 0 ? "connected to MySQL" : "no MySQL server reachable with these credentials", "\n");
|
print(db != 0 ? "connected to MySQL" : "no MySQL server reachable with these credentials", "\n");
|
||||||
|
|||||||
@ -10,7 +10,7 @@ m : pointer to an existing MySQL connection struct
|
|||||||
:content
|
:content
|
||||||
Closes an existing connection to a MySQL server.
|
Closes an existing connection to a MySQL server.
|
||||||
|
|
||||||
Call this when you are done using a `MySQL*` connection handle.
|
Call this when you want to release a `MySQL*` connection handle before the request ends. UCE also closes any remaining MySQL handles automatically during request cleanup, including handles skipped by exceptions or fatal request recovery. A `MySQL*` is only valid inside the request that opened it; do not cache it across requests.
|
||||||
|
|
||||||
:example
|
:example
|
||||||
MySQL* db = mysql_connect();
|
MySQL* db = mysql_connect();
|
||||||
|
|||||||
@ -3,6 +3,10 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "mysql-connector.h"
|
#include "mysql-connector.h"
|
||||||
|
|
||||||
|
// MySQL handles are request-scoped framework resources. User code may call
|
||||||
|
// mysql_disconnect() to close early, but leaked/exception-skipped handles are
|
||||||
|
// still closed by cleanup_mysql_connections() at request end. Handles must not
|
||||||
|
// be cached across requests or stored in globals/sessions.
|
||||||
static void mysql_register_request_connection(MySQL* db)
|
static void mysql_register_request_connection(MySQL* db)
|
||||||
{
|
{
|
||||||
if(!context || !db)
|
if(!context || !db)
|
||||||
|
|||||||
@ -33,8 +33,10 @@ struct MySQL {
|
|||||||
DValue query(String q, StringMap params);
|
DValue query(String q, StringMap params);
|
||||||
DValue get_pending_result();
|
DValue get_pending_result();
|
||||||
|
|
||||||
// Unregisters from the request's connection tracking, so stack-allocated
|
// Unregisters from request connection tracking, so stack-allocated instances
|
||||||
// instances cannot leave dangling pointers behind for request cleanup.
|
// 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.
|
||||||
~MySQL() { disconnect(); }
|
~MySQL() { disconnect(); }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -799,7 +799,9 @@ public:
|
|||||||
|
|
||||||
#ifdef UCE_WASM_HOST_CONNECTORS
|
#ifdef UCE_WASM_HOST_CONNECTORS
|
||||||
// Host-owned resource handle table (§3.1): connections opened by the guest
|
// Host-owned resource handle table (§3.1): connections opened by the guest
|
||||||
// live here and are closed when the workspace drops at request end.
|
// live here and are closed when the workspace drops at request end. This is
|
||||||
|
// the wasm-side enforcement of request-scoped DB lifecycle; app code should
|
||||||
|
// never cache these opaque handles across requests.
|
||||||
std::vector<SQLite*> sqlite_handles;
|
std::vector<SQLite*> sqlite_handles;
|
||||||
std::vector<MySQL*> mysql_handles;
|
std::vector<MySQL*> mysql_handles;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user