Browse code

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

Daniel-Constantin Mierla authored on 26/09/2013 13:10:22
Showing 2 changed files
... ...
@@ -510,7 +510,7 @@ int db_mysql_affected_rows(const db1_con_t* _h)
510 510
 int db_mysql_start_transaction(db1_con_t* _h, db_locking_t _l)
511 511
 {
512 512
 	str begin_str = str_init("BEGIN");
513
-	str lock_start_str = str_init("LOCK TABLE ");
513
+	str lock_start_str = str_init("LOCK TABLES ");
514 514
 	str lock_end_str  = str_init(" WRITE");
515 515
 	str lock_str = {0, 0};
516 516
 
... ...
@@ -559,6 +559,7 @@ int db_mysql_start_transaction(db1_con_t* _h, db_locking_t _l)
559 559
 		}
560 560
 
561 561
 		if (lock_str.s) pkg_free(lock_str.s);
562
+		CON_LOCKEDTABLES(_h) = 1;
562 563
 		break;
563 564
 
564 565
 	default:
... ...
@@ -574,6 +575,35 @@ error:
574 575
 	return -1;
575 576
 }
576 577
 
578
+/**
579
+ * Unlock tables in the session
580
+ * \param _h database handle
581
+ * \return 0 on success, negative on failure
582
+ */
583
+int db_mysql_unlock_tables(db1_con_t* _h)
584
+{
585
+	str query_str = str_init("UNLOCK TABLES");
586
+
587
+	if (!_h) {
588
+		LM_ERR("invalid parameter value\n");
589
+		return -1;
590
+	}
591
+
592
+	if (CON_LOCKEDTABLES(_h) == 0) {
593
+		LM_DBG("no active locked tables\n");
594
+		return 0;
595
+	}
596
+
597
+	if (db_mysql_raw_query(_h, &query_str, NULL) < 0)
598
+	{
599
+		LM_ERR("executing raw_query\n");
600
+		return -1;
601
+	}
602
+
603
+	CON_LOCKEDTABLES(_h) = 0;
604
+	return 0;
605
+}
606
+
577 607
 /**
578 608
  * Ends a transaction and commits the changes (SQL COMMIT)
579 609
  * \param _h database handle
... ...
@@ -603,6 +633,10 @@ int db_mysql_end_transaction(db1_con_t* _h)
603 633
  	   raw_query fails, and the calling module does an abort_transaction()
604 634
 	   to clean-up, a ROLLBACK will be sent to the DB. */
605 635
 	CON_TRANSACTION(_h) = 0;
636
+
637
+	if(db_mysql_unlock_tables(_h)<0)
638
+		return -1;
639
+
606 640
 	return 0;
607 641
 }
608 642
 
... ...
@@ -614,6 +648,7 @@ int db_mysql_end_transaction(db1_con_t* _h)
614 648
 int db_mysql_abort_transaction(db1_con_t* _h)
615 649
 {
616 650
 	str query_str = str_init("ROLLBACK");
651
+	int ret;
617 652
 
618 653
 	if (!_h) {
619 654
 		LM_ERR("invalid parameter value\n");
... ...
@@ -622,7 +657,8 @@ int db_mysql_abort_transaction(db1_con_t* _h)
622 657
 
623 658
 	if (CON_TRANSACTION(_h) == 0) {
624 659
 		LM_DBG("nothing to rollback\n");
625
-		return 0;
660
+		ret = 0;
661
+		goto done;
626 662
 	}
627 663
 
628 664
 	/* Whether the rollback succeeds or not we need to _end_ the
... ...
@@ -632,10 +668,15 @@ int db_mysql_abort_transaction(db1_con_t* _h)
632 668
 	if (db_mysql_raw_query(_h, &query_str, NULL) < 0)
633 669
 	{
634 670
 		LM_ERR("executing raw_query\n");
635
-		return -1;
671
+		ret = -1;
672
+		goto done;
636 673
 	}
637 674
 
638
-	return 1;
675
+	ret = 1;
676
+
677
+done:
678
+	db_mysql_unlock_tables(_h);
679
+	return ret;
639 680
 }
640 681
 
641 682
 
... ...
@@ -46,7 +46,8 @@ struct my_con {
46 46
 
47 47
 	MYSQL* con;              /*!< Connection representation */
48 48
 	time_t timestamp;        /*!< Timestamp of last query */
49
-	int transaction;	 /*!< indicates whether a multi-query transaction is currently open */
49
+	int transaction;         /*!< Multi-query transaction is currently open */
50
+	int lockedtables;        /*!< Table locks were aquired */
50 51
 };
51 52
 
52 53
 
... ...
@@ -56,6 +57,7 @@ struct my_con {
56 57
 #define CON_CONNECTION(db_con)  (((struct my_con*)((db_con)->tail))->con)
57 58
 #define CON_TIMESTAMP(db_con)   (((struct my_con*)((db_con)->tail))->timestamp)
58 59
 #define CON_TRANSACTION(db_con) (((struct my_con*)((db_con)->tail))->transaction)
60
+#define CON_LOCKEDTABLES(db_con) (((struct my_con*)((db_con)->tail))->lockedtables)
59 61
 
60 62
 
61 63
 /*! \brief