03.07.2011, 11:14
(
Последний раз редактировалось Simon; 03.07.2011 в 11:25.
Причина: Added context example
)
Hey,
I'm looking for a way to automatically clean up my database resources so I do not have to worry about it. One way I've thought of doing it is to have something like this:
This code would be used in a context like so:
Now if my understanding of Pawn using a single thread is correct (see code comments) then I shouldn't run into problems. Does anybody know of any problems this may cause that are not immediately obvious? It's important this database handle is closed because outside applications may be writing to the database.
The main problem I can see is that the open-close time frame has probably increased to >=1ms (depending on how accurate Pawn timers are) rather than <1ms for faster queries. If this time frame becomes problematic then I suppose this can be solved by the outside application having a query buffer.
Does anybody know any other problems that I have not forseen?
Thanks for reading.
I'm looking for a way to automatically clean up my database resources so I do not have to worry about it. One way I've thought of doing it is to have something like this:
pawn Код:
static DB:databaseHandle = DB:0;
stock DB:Database_GetHandle() {
if (databaseHandle == DB:0) {
databaseHandle = db_open("my.db");
// Set a timer which will operate at the next opportunity, this will
// be sometime after a callback (since Pawn is single threaded)
SetTimer("Database_Close", 1, false);
}
return databaseHandle ;
}
public Database_Close() {
if (databaseHandle != DB:0) {
// Close the database ..
db_close(databaseHandle);
// Invalidate the database handle.
databaseHandle = DB:0;
}
}
pawn Код:
public OnPlayerConnect(playerid) {
new DBResult: dbResult = db_query(Database_GetHandle(), "SELECT ..");
// Additional code to handle the result
db_free_result(dbResult);
// Any additional code, maybe additional queries. The additional queries will
// use the same method which points to the same handle ..
// No need to explicitly close the database.
}
The main problem I can see is that the open-close time frame has probably increased to >=1ms (depending on how accurate Pawn timers are) rather than <1ms for faster queries. If this time frame becomes problematic then I suppose this can be solved by the outside application having a query buffer.
Does anybody know any other problems that I have not forseen?
Thanks for reading.