Skip to content

Commit

Permalink
First attempt at more robust database connectivity.
Browse files Browse the repository at this point in the history
- Allow reconnect of database connection when no transaction is pending.
- Allow setting of database connection failure to be fatal e.g. end
  program.
  • Loading branch information
Marco van Wieringen committed Apr 28, 2015
1 parent 7b2f281 commit 894d61a
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 132 deletions.
4 changes: 3 additions & 1 deletion src/cats/bdb_dbi.h
Expand Up @@ -44,7 +44,9 @@ class B_DB_DBI: public B_DB_PRIV {
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool is_private);
bool need_private,
bool try_reconnect,
bool exit_on_fatal);
~B_DB_DBI();

/* low level operations */
Expand Down
4 changes: 3 additions & 1 deletion src/cats/bdb_ingres.h
Expand Up @@ -40,7 +40,9 @@ class B_DB_INGRES: public B_DB_PRIV {
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool is_private);
bool need_private,
bool try_reconnect,
bool exit_on_fatal);
~B_DB_INGRES();

/* low level operations */
Expand Down
4 changes: 3 additions & 1 deletion src/cats/bdb_mysql.h
Expand Up @@ -45,7 +45,9 @@ class B_DB_MYSQL: public B_DB_PRIV {
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool is_private);
bool need_private,
bool try_reconnect,
bool exit_on_fatal);
~B_DB_MYSQL();

/* low level operations */
Expand Down
4 changes: 3 additions & 1 deletion src/cats/bdb_postgresql.h
Expand Up @@ -38,7 +38,9 @@ class B_DB_POSTGRESQL: public B_DB_PRIV {
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool is_private);
bool need_private,
bool try_reconnect,
bool exit_on_fatal);
~B_DB_POSTGRESQL();

/* low level operations */
Expand Down
24 changes: 13 additions & 11 deletions src/cats/bdb_priv.h
Expand Up @@ -42,17 +42,19 @@ typedef struct sql_field {

class CATS_IMP_EXP B_DB_PRIV: public B_DB {
protected:
int m_status; /* status */
int m_num_rows; /* number of rows returned by last query */
int m_num_fields; /* number of fields returned by last query */
int m_rows_size; /* size of malloced rows */
int m_fields_size; /* size of malloced fields */
int m_row_number; /* row number from xx_data_seek */
int m_field_number; /* field number from sql_field_seek */
SQL_ROW m_rows; /* defined rows */
SQL_FIELD *m_fields; /* defined fields */
bool m_allow_transactions; /* transactions allowed */
bool m_transaction; /* transaction started */
int m_status; /* Status */
int m_num_rows; /* Number of rows returned by last query */
int m_num_fields; /* Number of fields returned by last query */
int m_rows_size; /* Size of malloced rows */
int m_fields_size; /* Size of malloced fields */
int m_row_number; /* Row number from xx_data_seek */
int m_field_number; /* Field number from sql_field_seek */
SQL_ROW m_rows; /* Defined rows */
SQL_FIELD *m_fields; /* Defined fields */
bool m_allow_transactions; /* Transactions allowed */
bool m_transaction; /* Transaction started */
bool m_try_reconnect; /* Try reconnecting DB connection */
bool m_exit_on_fatal; /* Exit on FATAL DB errors */

public:
/* methods */
Expand Down
4 changes: 3 additions & 1 deletion src/cats/bdb_sqlite.h
Expand Up @@ -40,7 +40,9 @@ class B_DB_SQLITE: public B_DB_PRIV {
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool is_private);
bool need_private,
bool try_reconnect,
bool exit_on_fatal);
~B_DB_SQLITE();

/* Used internaly by sqlite.c to access fields in db_sql_query() */
Expand Down
17 changes: 12 additions & 5 deletions src/cats/cats_backends.c
Expand Up @@ -90,7 +90,9 @@ B_DB *db_init_database(JCR *jcr,
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool need_private)
bool need_private,
bool try_reconnect,
bool exit_on_fatal)
{
struct stat st;
char *backend_dir;
Expand Down Expand Up @@ -141,7 +143,9 @@ B_DB *db_init_database(JCR *jcr,
db_socket,
mult_db_connections,
disable_batch_insert,
need_private);
need_private,
try_reconnect,
exit_on_fatal);
}
}
}
Expand Down Expand Up @@ -231,7 +235,9 @@ B_DB *db_init_database(JCR *jcr,
db_socket,
mult_db_connections,
disable_batch_insert,
need_private);
need_private,
try_reconnect,
exit_on_fatal);
} else {
Jmsg(jcr, M_ABORT, 0, _("Unable to load any shared library for libbareoscats-%s%s\n"),
backend_interface_mapping->interface_name, DYN_LIB_EXTENSION);
Expand Down Expand Up @@ -262,7 +268,6 @@ void db_flush_backends(void)
}
}
#else

/*
* Dummy bareos backend function replaced with the correct one at install time.
*/
Expand All @@ -276,7 +281,9 @@ B_DB *db_init_database(JCR *jcr,
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool need_private)
bool need_private,
bool try_reconnect,
bool exit_on_fatal)
{
Jmsg(jcr, M_FATAL, 0, _("Please replace this dummy libbareoscats library with a proper one.\n"));
Dmsg0(0, _("Please replace this dummy libbareoscats library with a proper one.\n"));
Expand Down
4 changes: 3 additions & 1 deletion src/cats/cats_backends.h
Expand Up @@ -39,7 +39,9 @@ typedef B_DB *(*t_backend_instantiate)(JCR *jcr,
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool need_private);
bool need_private,
bool try_reconnect,
bool exit_on_fatal);

typedef void (*t_flush_backend)(void);
}
Expand Down
26 changes: 18 additions & 8 deletions src/cats/dbi.c
Expand Up @@ -89,7 +89,9 @@ B_DB_DBI::B_DB_DBI(JCR *jcr,
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool need_private)
bool need_private,
bool try_reconnect,
bool exit_on_fatal)
{
char *p;
char new_db_driver[10];
Expand Down Expand Up @@ -167,6 +169,8 @@ B_DB_DBI::B_DB_DBI(JCR *jcr,
esc_obj = get_pool_memory(PM_FNAME);
m_allow_transactions = mult_db_connections;
m_is_private = need_private;
m_try_reconnect = try_reconnect;
m_exit_on_fatal = exit_on_fatal;

/*
* Initialize the private members.
Expand Down Expand Up @@ -674,8 +678,8 @@ bool B_DB_DBI::sql_query(const char *query, int flags)
/*
* We are starting a new query. reset everything.
*/
m_num_rows = -1;
m_row_number = -1;
m_num_rows = -1;
m_row_number = -1;
m_field_number = -1;

if (m_result) {
Expand Down Expand Up @@ -1226,8 +1230,8 @@ bool B_DB_DBI::sql_batch_start(JCR *jcr)
/*
* We are starting a new query. reset everything.
*/
m_num_rows = -1;
m_row_number = -1;
m_num_rows = -1;
m_row_number = -1;
m_field_number = -1;

sql_free_result();
Expand Down Expand Up @@ -1461,7 +1465,9 @@ extern "C" B_DB CATS_IMP_EXP *backend_instantiate(JCR *jcr,
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool need_private)
bool need_private,
bool try_reconnect,
bool exit_on_fatal)
#else
B_DB *db_init_database(JCR *jcr,
const char *db_driver,
Expand All @@ -1473,7 +1479,9 @@ B_DB *db_init_database(JCR *jcr,
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool need_private)
bool need_private,
bool try_reconnect,
bool exit_on_fatal)
#endif
{
B_DB_DBI *mdb = NULL;
Expand Down Expand Up @@ -1520,7 +1528,9 @@ B_DB *db_init_database(JCR *jcr,
db_socket,
mult_db_connections,
disable_batch_insert,
need_private));
need_private,
try_reconnect,
exit_on_fatal));

bail_out:
V(mutex);
Expand Down
22 changes: 16 additions & 6 deletions src/cats/ingres.c
Expand Up @@ -140,7 +140,9 @@ B_DB_INGRES::B_DB_INGRES(JCR *jcr,
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool need_private)
bool need_private,
bool try_reconnect,
bool exit_on_fatal)
{
B_DB_INGRES *mdb;
int next_session_id = 0;
Expand Down Expand Up @@ -200,6 +202,8 @@ B_DB_INGRES::B_DB_INGRES(JCR *jcr,
esc_obj = get_pool_memory(PM_FNAME);
m_allow_transactions = mult_db_connections;
m_is_private = need_private;
m_try_reconnect = try_reconnect;
m_exit_on_fatal = exit_on_fatal;

/*
* Initialize the private members.
Expand Down Expand Up @@ -543,8 +547,8 @@ bool B_DB_INGRES::sql_query(const char *query, int flags)
/*
* We are starting a new query. reset everything.
*/
m_num_rows = -1;
m_row_number = -1;
m_num_rows = -1;
m_row_number = -1;
m_field_number = -1;

if (m_result) {
Expand Down Expand Up @@ -1010,7 +1014,9 @@ extern "C" B_DB CATS_IMP_EXP *backend_instantiate(JCR *jcr,
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool need_private)
bool need_private,
bool try_reconnect,
bool exit_on_fatal)
#else
B_DB *db_init_database(JCR *jcr,
const char *db_driver,
Expand All @@ -1022,7 +1028,9 @@ B_DB *db_init_database(JCR *jcr,
const char *db_socket,
bool mult_db_connections,
bool disable_batch_insert,
bool need_private)
bool need_private,
bool try_reconnect,
bool exit_on_fatal)
#endif
{
B_DB_INGRES *mdb = NULL;
Expand Down Expand Up @@ -1062,7 +1070,9 @@ B_DB *db_init_database(JCR *jcr,
db_socket,
mult_db_connections,
disable_batch_insert,
need_private));
need_private,
try_reconnect,
exit_on_fatal));

bail_out:
V(mutex);
Expand Down

0 comments on commit 894d61a

Please sign in to comment.