• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 2007 December 20
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
6#    May you do good and not evil.
7#    May you find forgiveness for yourself and forgive others.
8#    May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# $Id: tkt2854.test,v 1.4 2009/03/16 13:19:36 danielk1977 Exp $
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16db close
17
18ifcapable !shared_cache {
19  finish_test
20  return
21}
22
23set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
24
25# Open 3 database connections. Connection "db" and "db2" share a cache.
26# Connection "db3" has its own cache.
27#
28do_test tkt2854-1.1 {
29  sqlite3 db test.db
30  sqlite3 db2 test.db
31
32  # This is taken from shared.test.  The Windows VFS expands
33  # ./test.db (and test.db) to be the same thing so the path
34  # matches and they share a cache.  By changing the case
35  # for Windows platform, we get around this and get a separate
36  # connection.
37  if {$::tcl_platform(platform)=="unix"} {
38    sqlite3 db3 ./test.db
39  } else {
40    sqlite3 db3 TEST.DB
41  }
42
43  db eval {
44    CREATE TABLE abc(a, b, c);
45  }
46} {}
47
48# Check that an exclusive lock cannot be obtained if some other
49# shared-cache connection has a read-lock on a table.
50#
51do_test tkt2854-1.2 {
52  execsql {
53    BEGIN;
54    SELECT * FROM abc;
55  } db2
56} {}
57do_test tkt2854-1.3 {
58  catchsql { BEGIN EXCLUSIVE } db
59} {1 {database table is locked}}
60do_test tkt2854-1.4 {
61  execsql { SELECT * FROM abc } db3
62} {}
63do_test tkt2854-1.5 {
64  catchsql { INSERT INTO abc VALUES(1, 2, 3) } db3
65} {1 {database is locked}}
66do_test tkt2854-1.6 {
67  execsql { COMMIT } db2
68} {}
69
70# Check that an exclusive lock prevents other shared-cache users from
71# starting a transaction.
72#
73do_test tkt2854-1.7 {
74  set ::DB2 [sqlite3_connection_pointer db2]
75  set ::STMT1 [sqlite3_prepare $DB2 "SELECT * FROM abc" -1 TAIL]
76  set ::STMT2 [sqlite3_prepare $DB2 "BEGIN EXCLUSIVE" -1 TAIL]
77  set ::STMT3 [sqlite3_prepare $DB2 "BEGIN IMMEDIATE" -1 TAIL]
78  set ::STMT4 [sqlite3_prepare $DB2 "BEGIN" -1 TAIL]
79  set ::STMT5 [sqlite3_prepare $DB2 "COMMIT" -1 TAIL]
80  execsql { BEGIN EXCLUSIVE } db
81} {}
82do_test tkt2854-1.8 {
83  catchsql { BEGIN EXCLUSIVE } db2
84} {1 {database schema is locked: main}}
85do_test tkt2854-1.9 {
86  catchsql { BEGIN IMMEDIATE } db2
87} {1 {database schema is locked: main}}
88do_test tkt2854-1.10 {
89  # This fails because the schema of main cannot be verified.
90  catchsql { BEGIN } db2
91} {1 {database schema is locked: main}}
92
93# Check that an exclusive lock prevents other shared-cache users from
94# reading the database. Use stored statements so that the error occurs
95# at the b-tree level, not the schema level.
96#
97do_test tkt2854-1.11 {
98  list [sqlite3_step $::STMT1] [sqlite3_finalize $::STMT1]
99} {SQLITE_ERROR SQLITE_LOCKED}
100do_test tkt2854-1.12 {
101  list [sqlite3_step $::STMT2] [sqlite3_finalize $::STMT2]
102} {SQLITE_ERROR SQLITE_LOCKED}
103do_test tkt2854-1.13 {
104  list [sqlite3_step $::STMT3] [sqlite3_finalize $::STMT3]
105} {SQLITE_ERROR SQLITE_LOCKED}
106do_test tkt2854-1.14 {
107  # A regular "BEGIN" doesn't touch any databases. So it succeeds.
108  list [sqlite3_step $::STMT4] [sqlite3_finalize $::STMT4]
109} {SQLITE_DONE SQLITE_OK}
110do_test tkt2854-1.15 {
111  # As does a COMMIT.
112  list [sqlite3_step $::STMT5] [sqlite3_finalize $::STMT5]
113} {SQLITE_DONE SQLITE_OK}
114
115# Try to read the database using connection "db3" (which does not share
116# a cache with "db"). The database should be locked.
117do_test tkt2854-1.16 {
118  catchsql { SELECT * FROM abc } db3
119} {1 {database is locked}}
120do_test tkt2854-1.17 {
121  execsql { COMMIT } db
122} {}
123do_test tkt2854-1.18 {
124  execsql { SELECT * FROM abc } db2
125} {}
126
127# Check that if an attempt to obtain an exclusive lock fails because an
128# attached db cannot be locked, the internal exclusive flag used by
129# shared-cache users is correctly cleared.
130do_test tkt2854-1.19 {
131  file delete -force test2.db test2.db-journal
132  sqlite3 db4 test2.db
133  execsql { CREATE TABLE def(d, e, f) } db4
134  execsql { ATTACH 'test2.db' AS aux } db
135} {}
136do_test tkt2854-1.20 {
137  execsql {BEGIN IMMEDIATE} db4
138  catchsql {BEGIN EXCLUSIVE} db
139} {1 {database table is locked}}
140do_test tkt2854-1.21 {
141  execsql {SELECT * FROM abc} db2
142} {}
143
144db close
145db2 close
146db3 close
147db4 close
148sqlite3_enable_shared_cache $::enable_shared_cache
149finish_test
150