3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # $Id: shared6.test,v 1.4 2009/06/05 17:09:12 drh Exp $
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 ifcapable !shared_cache { finish_test ; return }
18 do_test shared6-1.1.1 {
20 CREATE TABLE t1(a, b);
21 CREATE TABLE t2(c, d);
22 CREATE TABLE t3(e, f);
26 do_test shared6-1.1.2 {
27 set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
28 sqlite3_enable_shared_cache
31 do_test shared6-1.1.3 {
36 # Exclusive shared-cache locks. Test the following:
38 # 1.2.1: If [db1] has an exclusive lock, [db2] cannot read.
39 # 1.2.2: If [db1] has an exclusive lock, [db1] can read.
40 # 1.2.3: If [db1] has a non-exclusive write-lock, [db2] can read.
42 do_test shared6-1.2.1 {
43 execsql { SELECT * FROM t1 } db2 ;# Cache a compiled statement
44 execsql { BEGIN EXCLUSIVE } db1
45 catchsql { SELECT * FROM t1 } db2 ;# Execute the cached compiled statement
46 } {1 {database table is locked}}
47 do_test shared6-1.2.2 {
48 execsql { SELECT * FROM t1 } db1
50 do_test shared6-1.2.3 {
54 INSERT INTO t2 VALUES(3, 4);
56 execsql { SELECT * FROM t1 } db2
58 do_test shared6-1.2.X {
59 execsql { COMMIT } db1
62 # Regular shared-cache locks. Verify the following:
64 # 1.3.1: If [db1] has a write-lock on t1, [db1] can read from t1.
65 # 1.3.2: If [db1] has a write-lock on t1, [db2] can read from t2.
66 # 1.3.3: If [db1] has a write-lock on t1, [db2] cannot read from t1.
67 # 1.3.4: If [db1] has a write-lock on t1, [db2] cannot write to t1.
68 # 1.3.5: If [db1] has a read-lock on t1, [db2] can read from t1.
69 # 1.3.6: If [db1] has a read-lock on t1, [db2] cannot write to t1.
71 do_test shared6-1.3.1 {
74 INSERT INTO t1 VALUES(1, 2);
76 execsql { SELECT * FROM t1 } db1
78 do_test shared6-1.3.2 {
79 execsql { SELECT * FROM t2 } db2
81 do_test shared6-1.3.3 {
82 catchsql { SELECT * FROM t1 } db2
83 } {1 {database table is locked: t1}}
84 do_test shared6-1.3.4 {
85 catchsql { INSERT INTO t2 VALUES(1, 2) } db2
86 } {1 {database table is locked}}
87 do_test shared6-1.3.5 {
93 execsql { SELECT * FROM t1 } db2
95 do_test shared6-1.3.5 {
96 catchsql { INSERT INTO t1 VALUES(5, 6) } db2
97 } {1 {database table is locked: t1}}
98 do_test shared6-1.3.X {
99 execsql { COMMIT } db1
102 # Read-uncommitted mode.
104 # For these tests, connection [db2] is in read-uncommitted mode.
106 # 1.4.1: If [db1] has a write-lock on t1, [db2] can still read from t1.
107 # 1.4.2: If [db1] has a write-lock on the db schema (sqlite_master table),
108 # [db2] cannot read from the schema.
109 # 1.4.3: If [db1] has a read-lock on t1, [db2] cannot write to t1.
111 do_test shared6-1.4.1 {
112 execsql { PRAGMA read_uncommitted = 1 } db2
115 INSERT INTO t1 VALUES(5, 6);
117 execsql { SELECT * FROM t1 } db2
119 do_test shared6-1.4.2 {
120 execsql { CREATE TABLE t4(a, b) } db1
121 catchsql { SELECT * FROM t1 } db2
122 } {1 {database table is locked}}
123 do_test shared6-1.4.3 {
129 catchsql { INSERT INTO t1 VALUES(7, 8) } db2
130 } {1 {database table is locked: t1}}
132 do_test shared6-1.X {
137 #-------------------------------------------------------------------------
138 # The following tests - shared6-2.* - test that two database connections
139 # that connect to the same file using different VFS implementations do
142 if {$::tcl_platform(platform) eq "unix"} {
143 do_test shared6-2.1 {
144 sqlite3 db1 test.db -vfs unix
145 sqlite3 db2 test.db -vfs unix
146 sqlite3 db3 test.db -vfs unix-none
147 sqlite3 db4 test.db -vfs unix-none
150 do_test shared6-2.2 {
151 execsql { BEGIN; INSERT INTO t1 VALUES(9, 10); } db1
152 catchsql { SELECT * FROM t1 } db2
153 } {1 {database table is locked: t1}}
154 do_test shared6-2.3 {
155 execsql { SELECT * FROM t1 } db3
158 do_test shared6-2.3 {
159 execsql { COMMIT } db1
160 execsql { BEGIN; INSERT INTO t1 VALUES(11, 12); } db3
161 catchsql { SELECT * FROM t1 } db4
162 } {1 {database table is locked: t1}}
164 do_test shared6-2.4 {
165 execsql { SELECT * FROM t1 } db1
168 do_test shared6-2.5 {
169 execsql { COMMIT } db3
172 do_test shared6-2.X {
180 #-------------------------------------------------------------------------
181 # Test that it is possible to open an exclusive transaction while
182 # already holding a read-lock on the database file. And that it is
183 # not possible if some other connection holds such a lock.
185 do_test shared6-3.1 {
190 db1 eval {SELECT * FROM t1} {
191 # Within this block [db1] is holding a read-lock on t1. Test that
192 # this means t1 cannot be written by [db2].
194 do_test shared6-3.2 {
195 catchsql { INSERT INTO t1 VALUES(1, 2) } db2
196 } {1 {database table is locked: t1}}
198 do_test shared6-3.3 {
199 execsql { BEGIN EXCLUSIVE } db1
203 do_test shared6-3.4 {
204 catchsql { SELECT * FROM t1 } db2
205 } {1 {database schema is locked: main}}
206 do_test shared6-3.5 {
209 db2 eval {SELECT * FROM t1} {
210 do_test shared6-3.6 {
211 catchsql { BEGIN EXCLUSIVE } db1
212 } {1 {database table is locked}}
215 do_test shared6-3.7 {
216 execsql { BEGIN } db1
217 execsql { BEGIN } db2
219 db2 eval {SELECT * FROM t1} {
220 do_test shared6-3.8 {
221 catchsql { INSERT INTO t1 VALUES(1, 2) } db1
222 } {1 {database table is locked: t1}}
225 do_test shared6-3.9 {
226 execsql { BEGIN ; ROLLBACK } db3
228 do_test shared6-3.10 {
229 catchsql { SELECT * FROM t1 } db3
230 } {1 {database table is locked}}
231 do_test shared6-3.X {
237 do_test shared6-4.1 {
238 #forcedelete test.db test.db-journal
242 set ::STMT [sqlite3_prepare_v2 db1 "SELECT * FROM t1" -1 DUMMY]
243 execsql { CREATE TABLE t5(a, b) } db2
245 do_test shared6-4.2 {
246 sqlite3_finalize $::STMT
248 do_test shared6-4.X {
254 sqlite3_enable_shared_cache $::enable_shared_cache