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:
parent
406cb4a619
commit
a48766871d
56
core/db.cpp
56
core/db.cpp
|
@ -49,20 +49,24 @@ void Db::Connect()
|
|||
|
||||
pg_conn = PQconnectdb(buf.str().c_str());
|
||||
|
||||
if( !pg_conn )
|
||||
{
|
||||
log << log1 << "Db: Fatal error during connecting" << logend;
|
||||
return;
|
||||
}
|
||||
if( pg_conn )
|
||||
log << log3 << "Db: Socket: " << PQsocket(pg_conn) << logend;
|
||||
|
||||
// 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 to the database is lost, trying to recover" << logend;
|
||||
was_connection = false;
|
||||
PQreset(pg_conn);
|
||||
}
|
||||
|
||||
|
||||
if( PQstatus(pg_conn) != CONNECTION_OK )
|
||||
if( pg_conn && PQstatus(pg_conn) == CONNECTION_OK )
|
||||
{
|
||||
log << log2 << "Db: connection into the database is lost, trying to recover..." << logend;
|
||||
PQreset(pg_conn);
|
||||
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);
|
||||
SetDbParameters();
|
||||
}
|
||||
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;
|
||||
}
|
||||
log << log1 << "Db: Connection to db server cannot be established" << logend;
|
||||
throw Error(Error::db_fatal_error_during_connecting);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -70,14 +70,18 @@ public:
|
|||
|
||||
PGconn * GetPGconn();
|
||||
|
||||
virtual void Connect();
|
||||
|
||||
protected:
|
||||
|
||||
PGconn * pg_conn;
|
||||
std::string db_database, db_user, db_pass;
|
||||
bool close_at_end;
|
||||
|
||||
void Connect();
|
||||
void SetDbParameters();
|
||||
|
||||
void Close();
|
||||
|
||||
void AssertConnection();
|
||||
std::string Escape(const std::string & s);
|
||||
std::string Escape(const char * s);
|
||||
|
|
|
@ -55,9 +55,6 @@ void print_syntax()
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argv, char ** argc)
|
||||
{
|
||||
RequestController req_controller;
|
||||
|
|
|
@ -34,6 +34,7 @@ void RequestController::Close()
|
|||
|
||||
|
||||
|
||||
|
||||
bool RequestController::Init()
|
||||
{
|
||||
const char * sock = data.fcgi_socket.c_str();
|
||||
|
@ -95,7 +96,6 @@ bool RequestController::Init()
|
|||
dup2(s, 0);
|
||||
|
||||
//
|
||||
|
||||
data.dirs.ReadDirs();
|
||||
data.users.ReadUsers();
|
||||
data.groups.ReadGroups();
|
||||
|
|
Loading…
Reference in New Issue