-/*
- * Lock database, this can be called multiple times by the same
- * thread without blocking, but must be unlocked the number of
- * times it was locked.
- */
-void _db_lock(const char *file, int line, B_DB *mdb)
-{
- int errstat;
- if ((errstat=rwl_writelock_p(&mdb->lock, file, line)) != 0) {
- berrno be;
- e_msg(file, line, M_FATAL, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
- errstat, be.bstrerror(errstat));
- }
-}
-
-/*
- * Unlock the database. This can be called multiple times by the
- * same thread up to the number of times that thread called
- * db_lock()/
- */
-void _db_unlock(const char *file, int line, B_DB *mdb)
-{
- int errstat;
- if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
- berrno be;
- e_msg(file, line, M_FATAL, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
- errstat, be.bstrerror(errstat));
- }
-}
-
-/*
- * Start a transaction. This groups inserts and makes things
- * much more efficient. Usually started when inserting
- * file attributes.
- */
-void db_start_transaction(JCR *jcr, B_DB *mdb)
-{
- if (!jcr->attr) {
- jcr->attr = get_pool_memory(PM_FNAME);
- }
- if (!jcr->ar) {
- jcr->ar = (ATTR_DBR *)malloc(sizeof(ATTR_DBR));
- }
-
-#ifdef HAVE_SQLITE
- if (!mdb->allow_transactions) {
- return;
- }
- db_lock(mdb);
- /* Allow only 10,000 changes per transaction */
- if (mdb->transaction && mdb->changes > 10000) {
- db_end_transaction(jcr, mdb);
- }
- if (!mdb->transaction) {
- my_sqlite_query(mdb, "BEGIN"); /* begin transaction */
- Dmsg0(400, "Start SQLite transaction\n");
- mdb->transaction = 1;
- }
- db_unlock(mdb);
-#endif
-
-/*
- * This is turned off because transactions break
- * if multiple simultaneous jobs are run.
- */
-#ifdef HAVE_POSTGRESQL
- if (!mdb->allow_transactions) {
- return;
- }
- db_lock(mdb);
- /* Allow only 25,000 changes per transaction */
- if (mdb->transaction && mdb->changes > 25000) {
- db_end_transaction(jcr, mdb);
- }
- if (!mdb->transaction) {
- db_sql_query(mdb, "BEGIN", NULL, NULL); /* begin transaction */
- Dmsg0(400, "Start PosgreSQL transaction\n");
- mdb->transaction = 1;
- }
- db_unlock(mdb);
-#endif
-
-#ifdef HAVE_INGRES
- if (!mdb->allow_transactions) {
- return;
- }
- db_lock(mdb);
- /* Allow only 25,000 changes per transaction */
- if (mdb->transaction && mdb->changes > 25000) {
- db_end_transaction(jcr, mdb);
- }
- if (!mdb->transaction) {
- db_sql_query(mdb, "BEGIN", NULL, NULL); /* begin transaction */
- Dmsg0(400, "Start Ingres transaction\n");
- mdb->transaction = 1;
- }
- db_unlock(mdb);
-#endif
-
-#ifdef HAVE_DBI
- if (db_type == SQL_TYPE_SQLITE) {
- if (!mdb->allow_transactions) {
- return;
- }
- db_lock(mdb);
- /* Allow only 10,000 changes per transaction */
- if (mdb->transaction && mdb->changes > 10000) {
- db_end_transaction(jcr, mdb);
- }
- if (!mdb->transaction) {
- //my_sqlite_query(mdb, "BEGIN"); /* begin transaction */
- db_sql_query(mdb, "BEGIN", NULL, NULL); /* begin transaction */
- Dmsg0(400, "Start SQLite transaction\n");
- mdb->transaction = 1;
- }
- db_unlock(mdb);
- } else if (db_type == SQL_TYPE_POSTGRESQL) {
- if (!mdb->allow_transactions) {
- return;
- }
- db_lock(mdb);
- /* Allow only 25,000 changes per transaction */
- if (mdb->transaction && mdb->changes > 25000) {
- db_end_transaction(jcr, mdb);
- }
- if (!mdb->transaction) {
- db_sql_query(mdb, "BEGIN", NULL, NULL); /* begin transaction */
- Dmsg0(400, "Start PosgreSQL transaction\n");
- mdb->transaction = 1;
- }
- db_unlock(mdb);
- }
-#endif
-}
-
-void db_end_transaction(JCR *jcr, B_DB *mdb)
-{
- /*
- * This can be called during thread cleanup and
- * the db may already be closed. So simply return.
- */
- if (!mdb) {
- return;
- }
-
- if (jcr && jcr->cached_attribute) {
- Dmsg0(400, "Flush last cached attribute.\n");
- if (!db_create_attributes_record(jcr, mdb, jcr->ar)) {
- Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
- }
- jcr->cached_attribute = false;
- }
-
-#ifdef HAVE_SQLITE
- if (!mdb->allow_transactions) {
- return;
- }
- db_lock(mdb);
- if (mdb->transaction) {
- my_sqlite_query(mdb, "COMMIT"); /* end transaction */
- mdb->transaction = 0;
- Dmsg1(400, "End SQLite transaction changes=%d\n", mdb->changes);
- }
- mdb->changes = 0;
- db_unlock(mdb);
-#endif
-
-
-
-#ifdef HAVE_INGRES
- if (!mdb->allow_transactions) {
- return;
- }
- db_lock(mdb);
- if (mdb->transaction) {
- db_sql_query(mdb, "COMMIT", NULL, NULL); /* end transaction */
- mdb->transaction = 0;
- Dmsg1(400, "End Ingres transaction changes=%d\n", mdb->changes);
- }
- mdb->changes = 0;
- db_unlock(mdb);
-#endif
-
-
-#ifdef HAVE_POSTGRESQL
- if (!mdb->allow_transactions) {
- return;
- }
- db_lock(mdb);
- if (mdb->transaction) {
- db_sql_query(mdb, "COMMIT", NULL, NULL); /* end transaction */
- mdb->transaction = 0;
- Dmsg1(400, "End PostgreSQL transaction changes=%d\n", mdb->changes);
- }
- mdb->changes = 0;
- db_unlock(mdb);
-#endif
-
-#ifdef HAVE_DBI
- if (db_type == SQL_TYPE_SQLITE) {
- if (!mdb->allow_transactions) {
- return;
- }
- db_lock(mdb);
- if (mdb->transaction) {
- //my_sqlite_query(mdb, "COMMIT"); /* end transaction */
- db_sql_query(mdb, "COMMIT", NULL, NULL); /* end transaction */
- mdb->transaction = 0;
- Dmsg1(400, "End SQLite transaction changes=%d\n", mdb->changes);
- }
- mdb->changes = 0;
- db_unlock(mdb);
- } else if (db_type == SQL_TYPE_POSTGRESQL) {
- if (!mdb->allow_transactions) {
- return;
- }
- db_lock(mdb);
- if (mdb->transaction) {
- db_sql_query(mdb, "COMMIT", NULL, NULL); /* end transaction */
- mdb->transaction = 0;
- Dmsg1(400, "End PostgreSQL transaction changes=%d\n", mdb->changes);
- }
- mdb->changes = 0;
- db_unlock(mdb);
- }
-#endif
-}
-