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"),
} 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);
return 0;
}
- mdb->connected = TRUE;
+ mdb->connected = true;
V(mutex);
return 1;
}
if (mdb->db_socket) {
free(mdb->db_socket);
}
+ my_postgresql_free_result(mdb);
free(mdb);
}
V(mutex);
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");
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);
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++;
//
// 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++) {
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.
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);
{
if (mdb->result) {
PQclear(mdb->result);
+ mdb->result = NULL;
}
if (mdb->row) {