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 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this script is database locks.
14 # $Id: lock.test,v 1.40 2009/06/16 17:49:36 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Create an alternative connection to the database
23 # Give a complex pathname to stress the path simplification logic in
24 # the vxworks driver and in test_async.
25 file mkdir tempdir/t1/t2
26 sqlite3 db2 ./tempdir/../tempdir/t1/.//t2/../../..//test.db
30 execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
33 execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name} db2
36 execsql {CREATE TABLE t1(a int, b int)}
37 execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
41 SELECT name FROM sqlite_master WHERE type='table' ORDER BY name
46 execsql {INSERT INTO t1 VALUES(1,2)}
47 execsql {SELECT * FROM t1}
49 # Update: The schema is now brought up to date by test lock-1.5.
50 # do_test lock-1.7.1 {
51 # catchsql {SELECT * FROM t1} db2
52 # } {1 {no such table: t1}}
54 catchsql {SELECT * FROM t1} db2
57 execsql {UPDATE t1 SET a=b, b=a} db2
58 execsql {SELECT * FROM t1} db2
61 execsql {SELECT * FROM t1}
64 execsql {BEGIN TRANSACTION}
65 execsql {UPDATE t1 SET a = 0 WHERE 0}
66 execsql {SELECT * FROM t1}
69 catchsql {SELECT * FROM t1} db2
73 catchsql {SELECT * FROM t1}
77 execsql {CREATE TABLE t2(x int, y int)}
78 execsql {INSERT INTO t2 VALUES(8,9)}
79 execsql {SELECT * FROM t2}
82 catchsql {SELECT * FROM t2} db2
85 catchsql {SELECT * FROM t1} db2
88 catchsql {SELECT * FROM t2} db2
92 db eval {SELECT * FROM t1} qv {
93 set x [db eval {SELECT * FROM t1}]
98 db eval {SELECT * FROM t1} qv {
99 set x [db eval {SELECT * FROM t2}]
104 # You cannot UPDATE a table from within the callback of a SELECT
105 # on that same table because the SELECT has the table locked.
107 # 2006-08-16: Reads no longer block writes within the same
108 # database connection.
111 # db eval {SELECT * FROM t1} qv {
112 # set r [catch {db eval {UPDATE t1 SET a=b, b=a}} msg]
116 #} {1 {database table is locked}}
118 # But you can UPDATE a different table from the one that is used in
122 db eval {SELECT * FROM t1} qv {
123 set r [catch {db eval {UPDATE t2 SET x=y, y=x}} msg]
129 execsql {SELECT * FROM t2}
132 # It is possible to do a SELECT of the same table within the
133 # callback of another SELECT on that same table because two
134 # or more read-only cursors can be open at once.
137 db eval {SELECT * FROM t1} qv {
138 set r [catch {db eval {SELECT a FROM t1}} msg]
144 # Under UNIX you can do two SELECTs at once with different database
145 # connections, because UNIX supports reader/writer locks. Under windows,
146 # this is not possible.
148 if {$::tcl_platform(platform)=="unix"} {
150 db eval {SELECT * FROM t1} qv {
151 set r [catch {db2 eval {SELECT a FROM t1}} msg]
157 integrity_check lock-1.23
159 # If one thread has a transaction another thread cannot start
160 # a transaction. -> Not true in version 3.0. But if one thread
161 # as a RESERVED lock another thread cannot acquire one.
164 execsql {BEGIN TRANSACTION}
165 execsql {UPDATE t1 SET a = 0 WHERE 0}
166 execsql {BEGIN TRANSACTION} db2
167 set r [catch {execsql {UPDATE t1 SET a = 0 WHERE 0} db2} msg]
168 execsql {ROLLBACK} db2
170 } {1 {database is locked}}
172 # A thread can read when another has a RESERVED lock.
175 catchsql {SELECT * FROM t2} db2
178 # If the other thread (the one that does not hold the transaction with
179 # a RESERVED lock) tries to get a RESERVED lock, we do get a busy callback
180 # as long as we were not orginally holding a READ lock.
183 proc callback {count} {
184 set ::callback_value $count
187 set ::callback_value {}
189 # db2 does not hold a lock so we should get a busy callback here
190 set r [catch {execsql {UPDATE t1 SET a=b, b=a} db2} msg]
192 lappend r $::callback_value
193 } {1 {database is locked} 0}
195 set ::callback_value {}
196 execsql {BEGIN; SELECT rowid FROM sqlite_master LIMIT 1} db2
197 # This time db2 does hold a read lock. No busy callback this time.
198 set r [catch {execsql {UPDATE t1 SET a=b, b=a} db2} msg]
200 lappend r $::callback_value
201 } {1 {database is locked} {}}
202 catch {execsql {ROLLBACK} db2}
204 proc callback {count} {
205 lappend ::callback_value $count
208 set ::callback_value {}
210 # We get a busy callback because db2 is not holding a lock
211 set r [catch {execsql {UPDATE t1 SET a=b, b=a} db2} msg]
213 lappend r $::callback_value
214 } {1 {database is locked} {0 1 2 3 4 5}}
216 proc callback {count} {
217 lappend ::callback_value $count
220 set ::callback_value {}
222 execsql {BEGIN; SELECT rowid FROM sqlite_master LIMIT 1} db2
223 # No busy callback this time because we are holding a lock
224 set r [catch {execsql {UPDATE t1 SET a=b, b=a} db2} msg]
226 lappend r $::callback_value
227 } {1 {database is locked} {}}
228 catch {execsql {ROLLBACK} db2}
230 proc callback {count} {
231 lappend ::callback_value $count
234 set ::callback_value {}
236 set r [catch {execsql {SELECT * FROM t1} db2} msg]
238 lappend r $::callback_value
242 # Test the built-in busy timeout handler
244 # EVIDENCE-OF: R-23579-05241 PRAGMA busy_timeout; PRAGMA busy_timeout =
245 # milliseconds; Query or change the setting of the busy timeout.
250 execsql {UPDATE t1 SET a = 0 WHERE 0}
251 catchsql {BEGIN EXCLUSIVE;} db2
252 } {1 {database is locked}}
254 db2 eval {PRAGMA busy_timeout}
261 db2 eval {PRAGMA busy_timeout}
263 integrity_check lock-2.10
265 db2 eval {PRAGMA busy_timeout(400)}
267 execsql {UPDATE t1 SET a = 0 WHERE 0}
268 catchsql {BEGIN EXCLUSIVE;} db2
269 } {1 {database is locked}}
271 db2 eval {PRAGMA busy_timeout}
274 db2 eval {PRAGMA busy_timeout(0)}
278 db2 eval {PRAGMA busy_timeout}
280 integrity_check lock-2.13
282 # Try to start two transactions in a row
285 execsql {BEGIN TRANSACTION}
286 set r [catch {execsql {BEGIN TRANSACTION}} msg]
289 } {1 {cannot start a transaction within a transaction}}
290 integrity_check lock-3.2
292 # Make sure the busy handler and error messages work when
293 # opening a new pointer to the database while another pointer
294 # has the database locked.
298 catch {db eval ROLLBACK}
300 db eval {UPDATE t1 SET a=0 WHERE 0}
301 sqlite3 db2 ./test.db
302 catchsql {UPDATE t1 SET a=0} db2
303 } {1 {database is locked}}
305 set ::callback_value {}
306 set rc [catch {db2 eval {UPDATE t1 SET a=0}} msg]
307 lappend rc $msg $::callback_value
308 } {1 {database is locked} {}}
310 proc callback {count} {
311 lappend ::callback_value $count
315 set rc [catch {db2 eval {UPDATE t1 SET a=0}} msg]
316 lappend rc $msg $::callback_value
317 } {1 {database is locked} {0 1 2 3 4 5}}
320 # When one thread is writing, other threads cannot read. Except if the
321 # writing thread is writing to its temporary tables, the other threads
322 # can still read. -> Not so in 3.0. One thread can read while another
323 # holds a RESERVED lock.
334 db function tx_exec tx_exec
336 INSERT INTO t1(a,b) SELECT 3, tx_exec('SELECT y FROM t2 LIMIT 1');
343 CREATE TEMP TABLE t3(x);
349 INSERT INTO t3 SELECT tx_exec('SELECT y FROM t2 LIMIT 1');
359 UPDATE t1 SET a=tx_exec('SELECT x FROM t2');
369 UPDATE t3 SET x=tx_exec('SELECT x FROM t2');
381 CREATE TABLE t4(a PRIMARY KEY, b);
382 INSERT INTO t4 VALUES(1, 'one');
383 INSERT INTO t4 VALUES(2, 'two');
384 INSERT INTO t4 VALUES(3, 'three');
387 set STMT [sqlite3_prepare $DB "SELECT * FROM sqlite_master" -1 TAIL]
390 execsql { DELETE FROM t4 }
391 execsql { SELECT * FROM sqlite_master } db2
392 execsql { SELECT * FROM t4 } db2
398 INSERT INTO t4 VALUES(1, 'one');
399 INSERT INTO t4 VALUES(2, 'two');
400 INSERT INTO t4 VALUES(3, 'three');
404 execsql { SELECT * FROM t4 } db2
405 } {1 one 2 two 3 three}
408 execsql { SELECT a FROM t4 ORDER BY a } db2
412 execsql { PRAGMA integrity_check } db2
416 sqlite3_finalize $STMT
419 # At one point the following set of conditions would cause SQLite to
420 # retain a RESERVED or EXCLUSIVE lock after the transaction was committed:
422 # * The journal-mode is set to something other than 'delete', and
423 # * there exists one or more active read-only statements, and
424 # * a transaction that modified zero database pages is committed.
426 #set temp_status unlocked
427 #if {$TEMP_STORE>=2} {set temp_status unknown}
428 set temp_status unknown
430 set STMT [sqlite3_prepare $DB "SELECT * FROM sqlite_master" -1 TAIL]
434 execsql { PRAGMA lock_status }
435 } [list main shared temp $temp_status]
438 PRAGMA journal_mode = truncate;
440 UPDATE t4 SET a = 10 WHERE 0;
443 execsql { PRAGMA lock_status }
444 } [list main shared temp $temp_status]
446 sqlite3_finalize $STMT