]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/cats/postgresql.c
Add regex to FileSet + fix ACL crash + gnome1 Makefile + 30 second retry for DB
[bacula/bacula] / bacula / src / cats / postgresql.c
index 9dfa401e3f4ea1d4c5ac8ef0050dce5c872b5258..360168288ec9eda88750c58ca898a83e5fa03d27 100644 (file)
@@ -124,7 +124,7 @@ db_open_database(JCR *jcr, B_DB *mdb)
       V(mutex);
       return 1;
    }
-   mdb->connected = FALSE;
+   mdb->connected = false;
 
    if ((errstat=rwl_init(&mdb->lock)) != 0) {
       Mmsg1(&mdb->errmsg, _("Unable to initialize DB lock. ERR=%s\n"), 
@@ -139,28 +139,26 @@ db_open_database(JCR *jcr, B_DB *mdb)
    } else {
       port = NULL;
    }
-   /* connect to the database */
-   mdb->db = PQsetdbLogin(
-       mdb->db_address,              /* default = localhost */
-       port,                         /* default port */
-       NULL,                         /* pg options */
-       NULL,                         /* tty, ignored */
-       mdb->db_name,                 /* database name */
-       mdb->db_user,                 /* login name */
-       mdb->db_password);            /* password */
-
-   /* If no connect, try once more in case it is a timing problem */
-   if (PQstatus(mdb->db) != CONNECTION_OK) {
-   mdb->db = PQsetdbLogin(
-       mdb->db_address,              /* default = localhost */
-       port,                         /* default port */
-       NULL,                         /* pg options */
-       NULL,                         /* tty, ignored */
-       mdb->db_name,                 /* database name */
-       mdb->db_user,                 /* login name */
-       mdb->db_password);            /* password */
+
+   /* If connection fails, try at 5 sec intervals for 30 seconds. */
+   for (int retry=0; retry < 6; retry++) {
+      /* connect to the database */
+      mdb->db = PQsetdbLogin(
+          mdb->db_address,           /* default = localhost */
+          port,                      /* default port */
+          NULL,                      /* pg options */
+          NULL,                      /* tty, ignored */
+          mdb->db_name,              /* database name */
+          mdb->db_user,              /* login name */
+          mdb->db_password);         /* password */
+
+      /* If no connect, try once more in case it is a timing problem */
+      if (PQstatus(mdb->db) == CONNECTION_OK) {
+        break;   
+      }
+      bmicrosleep(5, 0);
    }
-    
+
    Dmsg0(50, "pg_real_connect done\n");
    Dmsg3(50, "db_user=%s db_name=%s db_password=%s\n", mdb->db_user, mdb->db_name, 
             mdb->db_password==NULL?"(NULL)":mdb->db_password);
@@ -179,7 +177,7 @@ db_open_database(JCR *jcr, B_DB *mdb)
       return 0;
    }
 
-   mdb->connected = TRUE;
+   mdb->connected = true;
    V(mutex);
    return 1;
 }
@@ -219,6 +217,7 @@ db_close_database(JCR *jcr, B_DB *mdb)
       if (mdb->db_socket) {
         free(mdb->db_socket);
       }
+      my_postgresql_free_result(mdb);      
       free(mdb);
    }
    V(mutex);
@@ -297,7 +296,7 @@ int db_sql_query(B_DB *mdb, const char *query, DB_RESULT_HANDLER *result_handler
 
 POSTGRESQL_ROW my_postgresql_fetch_row(B_DB *mdb) 
 {
-   int           j;
+   int j;
    POSTGRESQL_ROW row = NULL; // by default, return NULL
 
    Dmsg0(500, "my_postgresql_fetch_row start\n");
@@ -306,9 +305,9 @@ POSTGRESQL_ROW my_postgresql_fetch_row(B_DB *mdb)
       Dmsg1(500, "we have need space of %d bytes\n", sizeof(char *) * mdb->num_fields);
 
       if (mdb->row != NULL) {
-              Dmsg0(500, "my_postgresql_fetch_row freeing space\n");
-             free(mdb->row);
-             mdb->row = NULL;
+         Dmsg0(500, "my_postgresql_fetch_row freeing space\n");
+        free(mdb->row);
+        mdb->row = NULL;
       }
 
       mdb->row = (POSTGRESQL_ROW) malloc(sizeof(char *) * mdb->num_fields);
@@ -322,8 +321,8 @@ POSTGRESQL_ROW my_postgresql_fetch_row(B_DB *mdb)
       Dmsg2(500, "my_postgresql_fetch_row row number '%d' is acceptable (0..%d)\n", mdb->row_number, mdb->num_rows);
       // get each value from this row
       for (j = 0; j < mdb->num_fields; j++) {
-             mdb->row[j] = PQgetvalue(mdb->result, mdb->row_number, j);
-              Dmsg2(500, "my_postgresql_fetch_row field '%d' has value '%s'\n", j, mdb->row[j]);
+        mdb->row[j] = PQgetvalue(mdb->result, mdb->row_number, j);
+         Dmsg2(500, "my_postgresql_fetch_row field '%d' has value '%s'\n", j, mdb->row[j]);
       }
       // increment the row number for the next call
       mdb->row_number++;
@@ -342,9 +341,9 @@ int my_postgresql_max_length(B_DB *mdb, int field_num) {
    //
    // for a given column, find the max length
    //
-   int    max_length;
+   int max_length;
    int i;
-   int    this_length;
+   int this_length;
 
    max_length = 0;
    for (i = 0; i < mdb->num_rows; i++) {
@@ -402,6 +401,10 @@ void my_postgresql_field_seek(B_DB *mdb, int field)
    mdb->field_number = field;
 }
 
+/*
+ * Note, if this routine returns 1 (failure), Bacula expects
+ *  that no result has been stored.
+ */
 int my_postgresql_query(B_DB *mdb, char *query) {
    Dmsg0(500, "my_postgresql_query started\n");
    // We are starting a new query.  reset everything.
@@ -409,6 +412,9 @@ int my_postgresql_query(B_DB *mdb, char *query) {
    mdb->row_number   = -1;
    mdb->field_number = -1;
 
+   if (mdb->result != NULL) {
+      PQclear(mdb->result);  /* hmm, someone forgot to free?? */
+   }
 
    Dmsg1(500, "my_postgresql_query starts with '%s'\n", query);
    mdb->result = PQexec(mdb->db, query);
@@ -438,6 +444,7 @@ void my_postgresql_free_result (B_DB *mdb)
 {
    if (mdb->result) {
       PQclear(mdb->result);
+      mdb->result = NULL;
    }
 
    if (mdb->row) {