Previous Blog Next Blog
Prev/Next Blog
by date

Async (non-blocking) Postgres Database Handler

Async (non-blocking) Postgres Database Handler
Name:Danni 
Date Posted:Jun 01, 2008
Rating:5.0 out of 5
Public:YES
Comments:YES
RSS Feed:GarageGames Blog feedor Subscribe with .
Profile Page:View profile page for Danni

Blog post
This post is out of date, check the completed resource here.
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=1497...

After looking at how wonderfully C looking the async API would be I figured a better solution would be threading with a pool handler. So here it is! Basically the way this works is one can create a pool of threads each with their own connection into the PGSQL database. You execute a query to the pool and one of the threads picks it up then processes it returning to a callback (C++ or TS). In the callback you have full access to the result for the run of the callback.

Here is how i set mine up.


function createServer(%serverType, %mission)
{
.....
PGSQLPool::createPool();
.....
}


The Database connection arguments are now in the prefs.cs files;

$pref::Server::Database::Database = "TGEA";
$pref::Server::Database::Host = "localhost";
$pref::Server::Database::Password = "test";
$pref::Server::Database::Port = "5432";
$pref::Server::Database::Threads = "2";
$pref::Server::Database::Username = "tgeauser";


In TS you can query anywhere with the following;


function getResult(%r)
{
%db = PGSQLPool::getPool();
%val = %db.getValue(%r, 0, 1);
echo("DATABASE GOT " @ %val);
}

function otherFunction()
{
%db = PGSQLPool::getPool();
%db.Exec("getResult", "SELECT * FROM tgea_test WHERE first = %s;", "something");
}


C++ is almost identical.

void getResult(PGQuery* r,const void *user)
{
const char* val = r->getValue(0,1);
}

void function()
{
PGSQLPool* db = PGSQLPool::getPool();
int userval = 1;
db->Exec(&getResult, &userval, "SELECT * FROM tgea_test WHERE first = %s;", "something");
}

Recent Blog Posts
List:07/17/08 - AFX Selected Objects, AutoAttack AI and Jerkiness.
06/01/08 - Async (non-blocking) Postgres Database Handler
05/18/08 - Postgres as a database solution?
05/04/08 - Step sounds, recoil and hit scans.
01/27/08 - Torque prediction hack for hitscans

Submit ResourceSubmit your own resources!

Tony Richards   (Jun 02, 2008 at 00:04 GMT)
Awesome work...

Brian Jones   (Jun 02, 2008 at 00:42 GMT)   Resource Rating: 5
That's really slick Danni.

Ronald V Rieger   (Jun 09, 2008 at 20:15 GMT)   Resource Rating: 5
Danni, this looks like exactly what I need for some non-profit educational stuff that we are doing. I am about to try it with TGE, which is the engine we are using. Do you foresee any problems with this? By the way, we use TGE because it seems to use less resources, and many of the target machines belong to schools which are often under powered.

Thank you.

Danni   (Jun 09, 2008 at 23:26 GMT)   Resource Rating: 5
It should work just fine, as long as TGE has good multithreading support. I think the big problem is that Con::executes must create a event instead of run the script directly, not sure if TGE does.

You must be a member and be logged in to either append comments or rate this resource.