[sr-dev] git:master: db_mysql: unlock tables at the end of transaction if they were locked

Daniel-Constantin Mierla miconda at gmail.com
Thu Sep 26 15:11:04 CEST 2013


Module: sip-router
Branch: master
Commit: 78d25dd11a64f36c5372cff3fe5d8a65a0e13396
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=78d25dd11a64f36c5372cff3fe5d8a65a0e13396

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Thu Sep 26 15:10:22 2013 +0200

db_mysql: unlock tables at the end of transaction if they were locked

---

 modules/db_mysql/km_dbase.c  |   49 ++++++++++++++++++++++++++++++++++++++---
 modules/db_mysql/km_my_con.h |    4 ++-
 2 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/modules/db_mysql/km_dbase.c b/modules/db_mysql/km_dbase.c
index 4cdcef0..4587681 100644
--- a/modules/db_mysql/km_dbase.c
+++ b/modules/db_mysql/km_dbase.c
@@ -510,7 +510,7 @@ int db_mysql_affected_rows(const db1_con_t* _h)
 int db_mysql_start_transaction(db1_con_t* _h, db_locking_t _l)
 {
 	str begin_str = str_init("BEGIN");
-	str lock_start_str = str_init("LOCK TABLE ");
+	str lock_start_str = str_init("LOCK TABLES ");
 	str lock_end_str  = str_init(" WRITE");
 	str lock_str = {0, 0};
 
@@ -559,6 +559,7 @@ int db_mysql_start_transaction(db1_con_t* _h, db_locking_t _l)
 		}
 
 		if (lock_str.s) pkg_free(lock_str.s);
+		CON_LOCKEDTABLES(_h) = 1;
 		break;
 
 	default:
@@ -575,6 +576,35 @@ error:
 }
 
 /**
+ * Unlock tables in the session
+ * \param _h database handle
+ * \return 0 on success, negative on failure
+ */
+int db_mysql_unlock_tables(db1_con_t* _h)
+{
+	str query_str = str_init("UNLOCK TABLES");
+
+	if (!_h) {
+		LM_ERR("invalid parameter value\n");
+		return -1;
+	}
+
+	if (CON_LOCKEDTABLES(_h) == 0) {
+		LM_DBG("no active locked tables\n");
+		return 0;
+	}
+
+	if (db_mysql_raw_query(_h, &query_str, NULL) < 0)
+	{
+		LM_ERR("executing raw_query\n");
+		return -1;
+	}
+
+	CON_LOCKEDTABLES(_h) = 0;
+	return 0;
+}
+
+/**
  * Ends a transaction and commits the changes (SQL COMMIT)
  * \param _h database handle
  * \return 0 on success, negative on failure
@@ -603,6 +633,10 @@ int db_mysql_end_transaction(db1_con_t* _h)
  	   raw_query fails, and the calling module does an abort_transaction()
 	   to clean-up, a ROLLBACK will be sent to the DB. */
 	CON_TRANSACTION(_h) = 0;
+
+	if(db_mysql_unlock_tables(_h)<0)
+		return -1;
+
 	return 0;
 }
 
@@ -614,6 +648,7 @@ int db_mysql_end_transaction(db1_con_t* _h)
 int db_mysql_abort_transaction(db1_con_t* _h)
 {
 	str query_str = str_init("ROLLBACK");
+	int ret;
 
 	if (!_h) {
 		LM_ERR("invalid parameter value\n");
@@ -622,7 +657,8 @@ int db_mysql_abort_transaction(db1_con_t* _h)
 
 	if (CON_TRANSACTION(_h) == 0) {
 		LM_DBG("nothing to rollback\n");
-		return 0;
+		ret = 0;
+		goto done;
 	}
 
 	/* Whether the rollback succeeds or not we need to _end_ the
@@ -632,10 +668,15 @@ int db_mysql_abort_transaction(db1_con_t* _h)
 	if (db_mysql_raw_query(_h, &query_str, NULL) < 0)
 	{
 		LM_ERR("executing raw_query\n");
-		return -1;
+		ret = -1;
+		goto done;
 	}
 
-	return 1;
+	ret = 1;
+
+done:
+	db_mysql_unlock_tables(_h);
+	return ret;
 }
 
 
diff --git a/modules/db_mysql/km_my_con.h b/modules/db_mysql/km_my_con.h
index 8e96e20..7a58530 100644
--- a/modules/db_mysql/km_my_con.h
+++ b/modules/db_mysql/km_my_con.h
@@ -46,7 +46,8 @@ struct my_con {
 
 	MYSQL* con;              /*!< Connection representation */
 	time_t timestamp;        /*!< Timestamp of last query */
-	int transaction;	 /*!< indicates whether a multi-query transaction is currently open */
+	int transaction;         /*!< Multi-query transaction is currently open */
+	int lockedtables;        /*!< Table locks were aquired */
 };
 
 
@@ -56,6 +57,7 @@ struct my_con {
 #define CON_CONNECTION(db_con)  (((struct my_con*)((db_con)->tail))->con)
 #define CON_TIMESTAMP(db_con)   (((struct my_con*)((db_con)->tail))->timestamp)
 #define CON_TRANSACTION(db_con) (((struct my_con*)((db_con)->tail))->transaction)
+#define CON_LOCKEDTABLES(db_con) (((struct my_con*)((db_con)->tail))->lockedtables)
 
 
 /*! \brief




More information about the sr-dev mailing list