### Description
I am using Kamailio with Mariadb module And i am get in Kamailio log some error messages: ``` "ERROR: db_mysql [km_dbase.c:122]: db_mysql_submit_query(): driver error on query: Table 'active_watchers' was not locked with LOCK TABLES (1100)" "ERROR: db_mysql [km_dbase.c:122]: db_mysql_submit_query(): driver error on query: Table 'active_watchers' was not locked with LOCK TABLES (1100)" "ERROR: db_mysql [km_dbase.c:122]: db_mysql_submit_query(): driver error on query: Table 'active_watchers' was not locked with LOCK TABLES (1100)" ``` After start debug log queries in Mariadb, i see queries in logfile like this: ``` 1141 Query SET autocommit=0 1141 Query LOCK TABLES presentity WRITE 1141 Query update `presentity` set `etag`='a.1620236063.17716.2514.101',`expires`=1625215189,`received_time`=1625214589,`priority`=205144189,`body`='<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<presence xmlns:dm="urn:ietf:params:xml:ns:pidf:data-model" xmlns:rpid="urn:ietf:params:xml:ns:pidf:rpid" xmlns:pidfonline="http://www.linphone.org/xsds/pidfonline.xsd%5C" entity="sip:name_user@office-dev.com" xmlns="urn:ietf:params:xml:ns:pidf">\n <tuple id="bephbl">\n <status>\n <basic>open</basic>\n pidfonline:online/\n </status>\n <contact priority="0.8">sip:name_user@office-dev.com</contact>\n <timestamp>2021-07-01T17:20:41Z</timestamp>\n </tuple>\n</presence>\n' where `domain`='office-dev.com' AND `username`='name_user' AND `event`='presence' AND `etag`='a.1620236063.17716.2513.100' 1141 Query select `to_user`,`to_domain`,`from_user`,`from_domain`,`watcher_username`,`watcher_domain`,`event_id`,`from_tag`,`to_tag`,`callid`,`local_cseq`,`record_route`,`contact`,`expires`,`reason`,`socket_info`,`local_contact`,`version`,`flags`,`user_agent` from `active_watchers` where `presentity_uri`='sip:name_user@office-dev.com' AND `event`='presence' AND `status`=1 AND `contact`<>'' 1141 Query COMMIT 1141 Query SET autocommit=1 1141 Query UNLOCK TABLES ``` or this one ``` 1141 Query LOCK TABLES presentity WRITE 1141 Query insert into `presentity` (`domain`,`username`,`event`,`etag`,`sender`,`body`,`received_time`,`priority`,`expires` ) values ('dev.com','+121342','presence','a.1620236063.17716.2532.0','','<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<presence xmlns:dm="urn:ietf:params:xml:ns:pidf:data-model" xmlns:rpid="urn:ietf:params:xml:ns:pidf:rpid" xmlns:pidfonline="http://www.linphone.org/xsds/pidfonline.xsd%5C" entity="sip:+121342@dev.com" xmlns="urn:ietf:params:xml:ns:pidf">\n <tuple id="wumhh7">\n <status>\n <basic>open</basic>\n pidfonline:online/\n </status>\n <contact priority="0.8">sip:+121342@dev.com</contact>\n <timestamp>2021-07-02T10:55:26Z</timestamp>\n </tuple>\n</presence>\n',1625223329,205152929,1625226929) 1141 Query select `to_user`,`to_domain`,`from_user`,`from_domain`,`watcher_username`,`watcher_domain`,`event_id`,`from_tag`,`to_tag`,`callid`,`local_cseq`,`record_route`,`contact`,`expires`,`reason`,`socket_info`,`local_contact`,`version`,`flags`,`user_agent` from `active_watchers` where `presentity_uri`='sip:+121342@dev.com' AND `event`='presence' AND `status`=1 AND `contact`<>'' 1141 Query COMMIT 1141 Query SET autocommit=1 1141 Query UNLOCK TABLES ```
This queries matches for time with reproduced errors in above kamailio log.
#### Reproduction In test lab ``` MariaDB [kamailio]> LOCK TABLES presentity WRITE; Query OK, 0 rows affected (0.00 sec)
MariaDB [kamailio]> select * from presentity; Empty set (0.00 sec)
MariaDB [kamailio]> select * from active_watcher; ERROR 1100 (HY000): Table 'active_watcher' was not locked with LOCK TABLES MariaDB [kamailio]> ``` I get same error
Also i am found in additional documentation from Mysql next: ``` A session that requires locks must acquire all the locks that it needs in a single LOCK TABLES statement. While the locks thus obtained are held, the session can access only the locked tables. ``` or from Mariadb docs
``` While a connection holds an explicit lock on a table, it cannot access a non-locked table. If you try, the following error will be produced:
ERROR 1100 (HY000): Table 'tab_name' was not locked with LOCK TABLES
``` ### Additional Information
* **Kamailio Version** *
``` Kamailio ver 5.4.5 or 5.6.0 ```
So, maybe need add additional lock for table active_watchers or need take out query select from space LOCK - UNLOCK for resolve this issue?
What is the operating system and the mariadb version?
Hello Daniel ``` [centos@proxy-us-west-2-intrado-1 ~]$ uname -a Linux proxy-us-west-2-intrado-1.nga911.com 4.18.0-305.3.1.el8.aarch64 #1 SMP Tue Jun 1 16:22:50 UTC 2021 aarch64 aarch64 aarch64 GNU/Linux
[centos@proxy-us-west-2-intrado-1 ~]$ cat /etc/os-release NAME="CentOS Linux" VERSION="8" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="8" PLATFORM_ID="platform:el8" PRETTY_NAME="CentOS Linux 8" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:8" HOME_URL="https://centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-8" CENTOS_MANTISBT_PROJECT_VERSION="8"
[centos@proxy-us-west-2-intrado-1 ~]$ rpm -qa | grep mariadb mariadb-server-utils-10.3.28-1.module_el8.3.0+757+d382997d.aarch64 mariadb-errmsg-10.3.28-1.module_el8.3.0+757+d382997d.aarch64 mariadb-gssapi-server-10.3.28-1.module_el8.3.0+757+d382997d.aarch64 mariadb-backup-10.3.28-1.module_el8.3.0+757+d382997d.aarch64 mariadb-10.3.28-1.module_el8.3.0+757+d382997d.aarch64 mariadb-connector-c-config-3.1.11-2.el8_3.noarch mariadb-common-10.3.28-1.module_el8.3.0+757+d382997d.aarch64 mariadb-connector-c-3.1.11-2.el8_3.aarch64 mariadb-server-10.3.28-1.module_el8.3.0+757+d382997d.aarch64 ```
Indeed, newer versions seem require to lock all tables used in the queries. For a quick solution, if you want to dig in the code, you can try to figure out if the select can be moved out. In long term, this part of code has to be updated for the new requirements.
After the Kamailio code investigation found a request to the `active_watchers` table related to “presence“ module. I think required this module update. Related function [get_subs_db](https://github.com/kamailio/kamailio/blob/16e6bfe4ed950851807c735cc3039046b7...) called from [get_subs_dialog](https://github.com/kamailio/kamailio/blob/16e6bfe4ed950851807c735cc3039046b7...). According this code ``` if(pres_subs_dbmode == DB_ONLY) { if(get_subs_db(pres_uri, event, sender, &s_array, &n) < 0) { LM_ERR("getting dialogs from database\n"); goto error; } } else { hash_code = core_case_hash(pres_uri, &event->name, shtable_size);
lock_get(&subs_htable[hash_code].lock); ``` To avoid call `get_subs_db` required do not use `pres_subs_dbmode == DB_ONLY`. We changed `subs_db_mode` to 2 and error message do not apper more. ``` modparam("presence", "subs_db_mode", 2) ``` In our use case database is used only by one Kamailio server and this approach works for us.
This issue is stale because it has been open 6 weeks with no activity. Remove stale label or comment or this will be closed in 2 weeks.
Closed #2805 as not planned.