static int verifyObjectPCRUD( osrfMethodContext*, osrfHash*, const jsonObject*, int );
static const char* org_tree_root( osrfMethodContext* ctx );
static jsonObject* single_hash( const char* key, const char* value );
+static dbi_conn oilsConnectDB_impl( const char* mod_name, int keep_trying );
+
static int child_initialized = 0; /* boolean */
@return A database connection if successful, or NULL if not.
*/
dbi_conn oilsConnectDB( const char* mod_name ) {
+ return oilsConnectDB_impl( mod_name, 0 );
+}
+
+dbi_conn oilsReconnectDB( const char* mod_name ) {
+ return oilsConnectDB_impl( mod_name, 1 );
+}
+
+dbi_conn oilsConnectDB_impl( const char* mod_name, int keep_trying ) {
osrfLogDebug( OSRF_LOG_MARK, "Attempting to initialize libdbi..." );
if( dbi_initialize( NULL ) == -1 ) {
free( pw );
free( pg_app );
- if( dbi_conn_connect( handle ) < 0 ) {
- sleep( 1 );
- if( dbi_conn_connect( handle ) < 0 ) {
+ int res = dbi_conn_connect( handle );
+ if( res < 0 ) {
+ do {
const char* msg;
dbi_conn_error( handle, &msg );
osrfLogError( OSRF_LOG_MARK, "Error connecting to database: %s",
msg ? msg : "(No description available)" );
- return NULL;
- }
+ sleep( 1 );
+ res = dbi_conn_connect( handle );
+ } while (res < 0 && keep_trying)
}
+ if (res < 0) return NULL;
+
osrfLogInfo( OSRF_LOG_MARK, "%s successfully connected to the database", mod_name );
return handle;
// E.g. 3624914: ERROR: current transaction is aborted, commands ignored until end of transaction block
// Substring test should work regardless.
const char* substr = strstr(msg, ok_msg);
- if( substr == NULL ) {
- osrfLogError( OSRF_LOG_MARK, "Database connection isn't working : %s", msg );
- return 0;
- } else
- return 1; // ignoring SELECT due to previous error; that's okay
+
+ if( substr == NULL ) { // No matching error, connection must be in bad shape
+ dbi_conn_close( handle ); // Just in case
+ if (!(handle = oilsReconnectDB(modulename))) {
+ osrfLogError( OSRF_LOG_MARK, "Database connection isn't working : %s", msg );
+ return 0;
+ }
+ }
+
+ return 1;
}
}