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
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;
|
buf << "dbname=" << db_database << " user=" << db_user << " password=" << db_pass;
|
||||||
|
|
||||||
pg_conn = PQconnectdb(buf.str().c_str());
|
pg_conn = PQconnectdb(buf.str().c_str());
|
||||||
|
|
||||||
|
if( pg_conn )
|
||||||
|
log << log3 << "Db: Socket: " << PQsocket(pg_conn) << logend;
|
||||||
|
|
||||||
if( !pg_conn )
|
// 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
|
||||||
log << log1 << "Db: Fatal error during connecting" << logend;
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Db::SetDbParameters()
|
||||||
|
{
|
||||||
if( PQsetClientEncoding(pg_conn, "LATIN2") == -1 )
|
if( PQsetClientEncoding(pg_conn, "LATIN2") == -1 )
|
||||||
log << log1 << "Db: Can't set the proper client encoding" << logend;
|
log << log1 << "Db: Can't set the proper client encoding" << logend;
|
||||||
|
|
||||||
log << log3 << "Db: Socket: " << PQsocket(pg_conn) << logend;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Db::Close()
|
void Db::Close()
|
||||||
{
|
{
|
||||||
if( pg_conn )
|
if( pg_conn )
|
||||||
|
@ -75,38 +79,34 @@ void Db::Close()
|
||||||
|
|
||||||
void Db::AssertConnection()
|
void Db::AssertConnection()
|
||||||
{
|
{
|
||||||
|
bool was_connection = true;
|
||||||
|
|
||||||
|
|
||||||
if( !pg_conn )
|
if( !pg_conn )
|
||||||
{
|
{
|
||||||
log << log1 << "Db: Trying to connect into the database...";
|
was_connection = false;
|
||||||
Connect();
|
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 )
|
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);
|
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 )
|
SetDbParameters();
|
||||||
{
|
}
|
||||||
log << log2 << "no" << logend;
|
else
|
||||||
throw Error(Error::db_fatal_error_during_connecting);
|
{
|
||||||
}
|
log << log1 << "Db: Connection to db server cannot be established" << logend;
|
||||||
else
|
throw Error(Error::db_fatal_error_during_connecting);
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,14 +70,18 @@ public:
|
||||||
|
|
||||||
PGconn * GetPGconn();
|
PGconn * GetPGconn();
|
||||||
|
|
||||||
|
virtual void Connect();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
PGconn * pg_conn;
|
PGconn * pg_conn;
|
||||||
std::string db_database, db_user, db_pass;
|
std::string db_database, db_user, db_pass;
|
||||||
bool close_at_end;
|
bool close_at_end;
|
||||||
|
|
||||||
void Connect();
|
void SetDbParameters();
|
||||||
|
|
||||||
void Close();
|
void Close();
|
||||||
|
|
||||||
void AssertConnection();
|
void AssertConnection();
|
||||||
std::string Escape(const std::string & s);
|
std::string Escape(const std::string & s);
|
||||||
std::string Escape(const char * s);
|
std::string Escape(const char * s);
|
||||||
|
|
|
@ -55,9 +55,6 @@ void print_syntax()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argv, char ** argc)
|
int main(int argv, char ** argc)
|
||||||
{
|
{
|
||||||
RequestController req_controller;
|
RequestController req_controller;
|
||||||
|
|
|
@ -34,6 +34,7 @@ void RequestController::Close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool RequestController::Init()
|
bool RequestController::Init()
|
||||||
{
|
{
|
||||||
const char * sock = data.fcgi_socket.c_str();
|
const char * sock = data.fcgi_socket.c_str();
|
||||||
|
@ -95,7 +96,6 @@ bool RequestController::Init()
|
||||||
dup2(s, 0);
|
dup2(s, 0);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
data.dirs.ReadDirs();
|
data.dirs.ReadDirs();
|
||||||
data.users.ReadUsers();
|
data.users.ReadUsers();
|
||||||
data.groups.ReadGroups();
|
data.groups.ReadGroups();
|
||||||
|
|
Loading…
Reference in New Issue