fixed: the way AppDb works (pg_conn and Connect method)
git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@482 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
62
core/db.cpp
62
core/db.cpp
@@ -48,21 +48,25 @@ void Db::Connect()
|
||||
buf << "dbname=" << db_database << " user=" << db_user << " password=" << db_pass;
|
||||
|
||||
pg_conn = PQconnectdb(buf.str().c_str());
|
||||
|
||||
if( pg_conn )
|
||||
log << log3 << "Db: Socket: " << PQsocket(pg_conn) << logend;
|
||||
|
||||
if( !pg_conn )
|
||||
{
|
||||
log << log1 << "Db: Fatal error during connecting" << logend;
|
||||
return;
|
||||
}
|
||||
// warning! pg_conn can be not null but there cannnot be a connection established
|
||||
// use PQstatus(pg_conn) to check whether the connection works fine
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Db::SetDbParameters()
|
||||
{
|
||||
if( PQsetClientEncoding(pg_conn, "LATIN2") == -1 )
|
||||
log << log1 << "Db: Can't set the proper client encoding" << logend;
|
||||
|
||||
log << log3 << "Db: Socket: " << PQsocket(pg_conn) << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Db::Close()
|
||||
{
|
||||
if( pg_conn )
|
||||
@@ -75,38 +79,34 @@ void Db::Close()
|
||||
|
||||
void Db::AssertConnection()
|
||||
{
|
||||
bool was_connection = true;
|
||||
|
||||
|
||||
if( !pg_conn )
|
||||
{
|
||||
log << log1 << "Db: Trying to connect into the database...";
|
||||
was_connection = false;
|
||||
Connect();
|
||||
|
||||
if( !pg_conn )
|
||||
// logend from Connect()
|
||||
throw Error(Error::db_fatal_error_during_connecting);
|
||||
|
||||
log << log1 << "ok" << logend;
|
||||
}
|
||||
|
||||
|
||||
else
|
||||
if( PQstatus(pg_conn) != CONNECTION_OK )
|
||||
{
|
||||
log << log2 << "Db: connection into the database is lost, trying to recover..." << logend;
|
||||
log << log2 << "Db: connection to the database is lost, trying to recover" << logend;
|
||||
was_connection = false;
|
||||
PQreset(pg_conn);
|
||||
}
|
||||
|
||||
|
||||
if( pg_conn && PQstatus(pg_conn) == CONNECTION_OK )
|
||||
{
|
||||
if( was_connection == false )
|
||||
log << log2 << "Db: Connection to the database works fine" << logend;
|
||||
|
||||
if( PQstatus(pg_conn) != CONNECTION_OK )
|
||||
{
|
||||
log << log2 << "no" << logend;
|
||||
throw Error(Error::db_fatal_error_during_connecting);
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log2 << "ok" << logend;
|
||||
|
||||
if( PQsetClientEncoding(pg_conn, "LATIN2") == -1 )
|
||||
log << log1 << "Db: Can't set the proper client encoding" << logend;
|
||||
|
||||
log << log3 << "Db: Socket: " << PQsocket(pg_conn) << logend;
|
||||
}
|
||||
SetDbParameters();
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "Db: Connection to db server cannot be established" << logend;
|
||||
throw Error(Error::db_fatal_error_during_connecting);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user