Use the following code snippet (add error checking, set v_file to the full path 
name of your SQLite db file) to check.

It attempts to take the same locks as SQLite would, but prints the pid of the 
blocking process. It also prints the journal mode.

#include <sys/types.h>
#include <sys/stat.h>
#ifdef AIX64
#include </usr/include/sys/flock.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#include "sqliteInt.h"

static const char  *g_mode[] = {
    "[0/0]", "[del]", "[WAL]", "[?/?]"
};

    char            v_buff[256];
    int             v_mode;

    struct flock    v_pending;   // = { F_WRLCK, SEEK_SET, PENDING_BYTE  ,      
     1, 0};
    struct flock    v_reserved;  // = { F_WRLCK, SEEK_SET, RESERVED_BYTE ,      
     1, 0};
    struct flock    v_shared;    // = { F_WRLCK, SEEK_SET, SHARED_FIRST  , 
SHARED_SIZE, 0};
    int             v_fd        = -1;
    int             v_ret       = -1;

    memset( &v_pending, 0, sizeof( v_pending));
    v_pending.l_type    = F_WRLCK;
    v_pending.l_whence  = SEEK_SET;
    v_pending.l_start   = PENDING_BYTE;
    v_pending.l_len             = 1;
    v_pending.l_pid             = 0;

    memset( &v_reserved, 0, sizeof( v_reserved));
    v_reserved.l_type   = F_WRLCK;
    v_reserved.l_whence = SEEK_SET;
    v_reserved.l_start  = RESERVED_BYTE;
    v_reserved.l_len    = 1;
    v_reserved.l_pid    = 0;

    memset( &v_shared, 0, sizeof( v_shared));
    v_shared.l_type             = F_WRLCK;
    v_shared.l_whence   = SEEK_SET;
    v_shared.l_start    = SHARED_FIRST;
    v_shared.l_len              = SHARED_SIZE;
    v_shared.l_pid              = 0;

    /* open the file readonly */
    v_fd = open(v_file,O_RDONLY);

    // read the file header
    read(v_fd, v_buff, 100);

    // interpret the bytes
    switch (v_buff[18]) {
        case  0: v_mode = (v_buff[18] == v_buff[19]) ? 0 : 3; break; // empty
        case  1: v_mode = (v_buff[18] == v_buff[19]) ? 1 : 3; break; // 
journal_mode=delete
        case  2: v_mode = (v_buff[18] == v_buff[19]) ? 2 : 3; break; // 
journal_mode=wal
        default: v_mode =                                  3; break; // invalid
    };

    /* check for a PENDING lock */
    fcntl(v_fd,F_GETLK,&v_pending);

    /* check for a RESERVED lock */
    fcntl(v_fd,F_GETLK,&v_reserved);

    /* check for a SHARED/EXCLUSIVE lock */
    fcntl(v_fd,F_GETLK,&v_shared);

    /* print in ascending restrictivity */
    if (v_pending.l_type == F_RDLCK)
        printf("%s File:%s, Process %d PENDING (SHARED)\n"   ,g_mode[v_mode] 
,v_file, (v_ret = v_pending .l_pid));

    if (v_shared .l_type == F_RDLCK)
        printf("%s File:%s, Process %d SHARED\n"             ,g_mode[v_mode] 
,v_file, (v_ret = v_shared  .l_pid));

    switch (v_reserved.l_type) {
        case F_WRLCK:
        case F_RDLCK:
        printf("%s File:%s, Process %d RESERVED\n"           ,g_mode[v_mode] 
,v_file, (v_ret = v_reserved.l_pid));
            break;
        default: break;
    }

    if (v_pending.l_type == F_WRLCK)
        printf("%s File: %s,Process %d PENDING (EXCLUSIVE)\n",g_mode[v_mode] 
,v_file, (v_ret = v_pending .l_pid));

    if (v_shared .l_type == F_WRLCK)
        printf("%s File %s, Process %d EXCLUSIVE\n"          ,g_mode[v_mode] 
,v_file, (v_ret = v_shared  .l_pid));

    if (v_ret == -1)
        printf("%s File:%s, <none>\n"                        ,g_mode[v_mode] 
,v_file);


-----Ursprüngliche Nachricht-----
Von: Roman Fleysher [mailto:roman.fleys...@einstein.yu.edu]
Gesendet: Mittwoch, 14. Jänner 2015 18:54
An: General Discussion of SQLite Database
Betreff: Re: [sqlite] database is locked

SQLite shell version 3.7.2
on Linux 2.6.18
NTFS



________________________________________
From: sqlite-users-boun...@sqlite.org<mailto:sqlite-users-boun...@sqlite.org> 
[sqlite-users-boun...@sqlite.org] on behalf of Richard Hipp [d...@sqlite.org]
Sent: Wednesday, January 14, 2015 12:50 PM
To: General Discussion of SQLite Database
Subject: Re: [sqlite] database is locked

On 1/14/15, Roman Fleysher 
<roman.fleys...@einstein.yu.edu<mailto:roman.fleys...@einstein.yu.edu>> wrote:
> Dear SQLiters,
>
> There has been a lot of discussion, I remember, on this subject by others.
> Please forgive me for asking this for a millionth time.
>
> I somehow got my database in a locked state. I updated a table
> yesterday and I am rather sure that no one on our multi-user system is 
> updating it now.
> The time stamp on the file is from yesterday, showing correct time. I
> need to update a table (I use shell) and I get "database is locked".
> Is there a way to figure out what is happening? Clear the lock?

What operating system and filesystem are you using?  And are you specifying an 
alternative VFS for SQLite or using the default?

>
> Thank you for your help,
>
> Roman
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org<mailto:sqlite-users@sqlite.org>
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>


--
D. Richard Hipp
d...@sqlite.org<mailto:d...@sqlite.org>
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org<mailto:sqlite-users@sqlite.org>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org<mailto:sqlite-users@sqlite.org>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


___________________________________________
 Gunter Hick
Software Engineer
Scientific Games International GmbH
FN 157284 a, HG Wien
Klitschgasse 2-4, A-1130 Vienna, Austria
Tel: +43 1 80100 0
E-Mail: h...@scigames.at

This communication (including any attachments) is intended for the use of the 
intended recipient(s) only and may contain information that is confidential, 
privileged or legally protected. Any unauthorized use or dissemination of this 
communication is strictly prohibited. If you have received this communication 
in error, please immediately notify the sender by return e-mail message and 
delete all copies of the original communication. Thank you for your cooperation.



_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to