Skip to main content

Sqlite database is locked


sqlite는 embedded system에서 널리 사용되는 무료 dbms?(dbms라고 말하긴 좀 그렇지만, dbms라 불러주자 ㅎ) 이다.

특히 memory db 기능이 아주 유용하다. 그 밖의 dbms에서도 이 기능이 있으나, 이 기능이 지원되는 버전은 대부분 고가이다. 따라서, 무료인 sqlite를 많이들 애용하는 것 같다.


멀티쓰레드를 sqlite DB를 구현하고 롱런테스트를 하다보면,
pthread_mutex_lock으로 쓰레드 간의 교착상태를 막아줘도,
sqlite lock 에러가 간헐적으로 발생할 것이다.


이에 대해 본인은 다음과 같은 에러 처리 구문을 준비하여 사용하고 있다.
sqlite Error가 발생하면, sqlite3_exception함수를 호출한다.

이 함수에서 sqlite error code를 구분하여, 만약, busy 또는 locked이면
최대 2초간 sleep 상태로 만드는 sqlite3_busy_timeout, busy handler를 호출한다.
그 다음, goto 구문으로 재차 sqlite3_exec를 실행한다.

단, sqlite3_exec는 transaction의 begin과 commit 또는 rollback 구문 사이에서 실행한다. 대부분 lock 에러가 발생하더라도 1~2번 실패 후에, 처리된다는 것이 본인의 테스트 결과이다.
단, journal를 WAL로 변경하였음.


기존 journal은 멀티쓰레드 지향적이지 않다는 점을 잊지마시길....




Error Code SQLITE_LOCKED (6): Database Is Locked

This error code occurs when you try to do two incompatible things with a database at the same time from the same database connection. For example, if you are in the middle of a SELECT statement and you try to DROP one of the tables being read by the SELECT, you will get an SQLITE_LOCKED error. Here is an example (using Tcl):

db eval {SELECT rowid FROM ex1} {
     if {$rowid==10} {
       db eval {DROP TABLE ex1}  ;# will give SQLITE_LOCKED error
     }
   }

Note that an SQLITE_LOCKED error is distinct from SQLITE_BUSY (5). SQLITE_BUSY means that another database connection (probably in another process) is using the database in a way that prevents you from using it. SQLITE_LOCKED means the source of contention is internal and comes from the same database connection that received the SQLITE_LOCKED error.
Here are other reasons for getting an SQLITE_LOCKED error:
  1. Trying to CREATE or DROP a table or index while a SELECT statement is still pending.
    • Sometimes people think they have finished with a SELECT statement because sqlite3_step() has returned SQLITE_DONE. But the SELECT is not really complete until sqlite3_reset() or sqlite3_finalize() have been called.
    • As of check-in [3902] (2007-05-02 after version 3.3.17) this is now allowed for CREATE statement.
  2. Trying to write to a table while a SELECT is active on that same table.
    • As of check-in [3355] (2006-08-16 after version 3.3.7) this is now allowed.
  3. Trying to do two SELECT on the same table at the same time in a multithread application, if sqlite is not set to do so.
  4. fcntl(3, F_SETLK call on DB file fails. This could be caused by an NFS locking issue, for example. One solution for this issue, is to mv the DB away, and copy it back so that it has a new Inode value.
Anonymous adds: can someone please expand on the 'not set to do so' above? DRH replys: This third bullet item was added anonymously. I am not aware of any restrictions on doing two or more SELECTs from different threads at the same time, as long as each SELECT is happening in an independent database connection (that is: a separate sqlite3* object obtained from separate sqlite3_open() calls). As far as I know, the only time you can get an SQLITE_LOCKED error in the latest code is according to the first bullet above.

Comments

Popular posts from this blog

nginx로 다중 포트 설정 및 다중 react app 설정

nginx에 다중 포트 설정하기 react로 여러 webapp를 개발하고, 동일한 서버에 실행시킬 필요가 있다. 이때 간단히 nginx에서 다중포트로 여러 webapp를 동일한 서버에서 실행시킬 수 있다. /etc/nginx/sites-available/에 default 파일이 있다. 보통 port 80로 설정되어 있다. 이 파일을 복사해서 sub로 명명한다.   아래와 같이 포트 8080으로 정하고, react app를 build한 디렉터리를 root <dir>로 지정하고, $> sudo service nginx restart 하면 된다. $>sudo systemctl status nginx.service 로 nginx 상태 확인이 가능하면, $> netstat -ano | grep 8080 으로 8080 포트가 살아있는 또는 외부PC에서  $>telnet ip 8080으로 telnet 접속이 되는지 확인하면 된다. 종종 Nginx 500 Interval Server Error가 발생하는데, 이는 1) root <dir>에서 <dir> 주소가 정확하지 않거나, 2) 파일 권한이 없거나이다. chmod 755 <dir>하면 대부분 해결될 것이다. 그럼 오늘도 유익한 하루 되시길..   

사자성어(가)

奇想天外(기상천외) 보통 사람이 쉽게 짐작할 수 없을 정도로 엉뚱하고 기발한 생각 技成眼昏(기성안혼) 재주를 다 배우니 눈이 어두워짐. 늙어서 재주가 쓸모 없게 됨 起承轉結(기승전결) 한시의 詩 作에서, 절구체의 전형적인 구성법을 지칭 제1구를 기구, 제2구를 승구, 제3구를 절구, 제4구를 결구 문장 구성에 있어서의 4단계, 서론, 설명, 증명, 결론 寄與補裨(기여보비) 이바지하여 돕고 부족함을 보태어 줌. 杞人之優(기인지우) 杞나라 사람의 군걱정이란 뜻. 곧 쓸데없는 걱정이나 무익한 근심을 말함.(=杞憂) 旣張之舞(기장지무) 이미 벌린 춤. 이미 시작한 일을 중간에서 그만 둘 수 없다 騎虎難下(기호난하) 이미 시작된 일을 중도에서 그만 둘 수 없음을 비유한 말 騎虎之勢(기호지세) = 기호난하 奇貨可居(기화가거) 진기한 물건을 사 두었다가 때를 기다리면 큰 이익을 볼 수 있다 吉祥善事(길상선사) 매우 기쁘고 좋은 일 吉凶禍福(길흉화복) 길한 일, 흉한 일, 언짢은 일, 복된 일 寄與補裨(기여보비) 이바지하여 돕고 부족함을 보태어 줌. 杞人之優(기인지우) 杞나라 사람의 군걱정이란 뜻. 곧 쓸데없는 걱정이나 무익한 근심을 말함.(=杞憂)  B A B A B A B A B A B A B A