diff --git a/core/db.cpp b/core/db.cpp index 0d896de..da42f25 100755 --- a/core/db.cpp +++ b/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); } } diff --git a/core/db.h b/core/db.h index 2a8e3ef..6c3a723 100755 --- a/core/db.h +++ b/core/db.h @@ -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); diff --git a/core/main.cpp b/core/main.cpp index a1bdb11..026451e 100755 --- a/core/main.cpp +++ b/core/main.cpp @@ -55,9 +55,6 @@ void print_syntax() - - - int main(int argv, char ** argc) { RequestController req_controller; diff --git a/core/requestcontroller.cpp b/core/requestcontroller.cpp index 3eecda4..24990ff 100755 --- a/core/requestcontroller.cpp +++ b/core/requestcontroller.cpp @@ -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();