2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this script is database locks.
17 # $Id: lock.test,v 1.20 2004/02/14 16:31:04 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Create an alternative connection to the database
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
43 #} {1 {database schema has changed}}
46 SELECT name FROM sqlite_master WHERE type='table' ORDER BY name
51 execsql {INSERT INTO t1 VALUES(1,2)}
52 execsql {SELECT * FROM t1}
55 execsql {SELECT * FROM t1} db2
58 execsql {UPDATE t1 SET a=b, b=a} db2
59 execsql {SELECT * FROM t1} db2
62 execsql {SELECT * FROM t1}
65 execsql {BEGIN TRANSACTION}
66 execsql {SELECT * FROM t1}
69 catchsql {SELECT * FROM t1} db2
70 } {1 {database is locked}}
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
83 } {1 {no such table: t2}}
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.
108 db eval {SELECT * FROM t1} qv {
109 set r [catch {db eval {UPDATE t1 SET a=b, b=a}} msg]
113 } {1 {database table is locked}}
115 # But you can UPDATE a different table from the one that is used in
119 db eval {SELECT * FROM t1} qv {
120 set r [catch {db eval {UPDATE t2 SET x=y, y=x}} msg]
126 execsql {SELECT * FROM t2}
129 # It is possible to do a SELECT of the same table within the
130 # callback of another SELECT on that same table because two
131 # or more read-only cursors can be open at once.
134 db eval {SELECT * FROM t1} qv {
135 set r [catch {db eval {SELECT a FROM t1}} msg]
141 # Under UNIX you can do two SELECTs at once with different database
142 # connections, because UNIX supports reader/writer locks. Under windows,
143 # this is not possible.
145 if {$::tcl_platform(platform)=="unix"} {
147 db eval {SELECT * FROM t1} qv {
148 set r [catch {db2 eval {SELECT a FROM t1}} msg]
154 integrity_check lock-1.23
156 # If one thread has a transaction another thread cannot start
160 execsql {BEGIN TRANSACTION}
161 set r [catch {execsql {BEGIN TRANSACTION} db2} msg]
163 } {1 {database is locked}}
165 # Nor can the other thread do a query.
168 set r [catch {execsql {SELECT * FROM t2} db2} msg]
170 } {1 {database is locked}}
172 # If the other thread (the one that does not hold the transaction)
173 # tries to start a transaction, we get a busy callback.
176 proc callback {args} {
177 set ::callback_value $args
180 set ::callback_value {}
182 set r [catch {execsql {BEGIN TRANSACTION} db2} msg]
184 lappend r $::callback_value
185 } {1 {database is locked} {{} 1}}
187 proc callback {file count} {
188 lappend ::callback_value $count
191 set ::callback_value {}
193 set r [catch {execsql {BEGIN TRANSACTION} db2} msg]
195 lappend r $::callback_value
196 } {1 {database is locked} {1 2 3 4 5}}
198 proc callback {file count} {
199 lappend ::callback_value $count
202 set ::callback_value {}
204 set r [catch {execsql {SELECT * FROM t1} db2} msg]
206 lappend r $::callback_value
207 } {1 {database is locked} {1 2 3 4 5}}
209 # In this test, the 3rd invocation of the busy callback causes
210 # the first thread to release its transaction. That allows the
211 # second thread to continue.
214 proc callback {file count} {
215 lappend ::callback_value $count
220 set ::callback_value {}
222 set r [catch {execsql {SELECT * FROM t2} db2} msg]
224 lappend r $::callback_value
227 execsql {BEGIN TRANSACTION}
228 proc callback {file count} {
229 lappend ::callback_value $count
234 set ::callback_value {}
236 set r [catch {execsql {BEGIN TRANSACTION} db2} msg]
237 execsql {ROLLBACK} db2
239 lappend r $::callback_value
242 # Test the built-in busy timeout handler
248 } {1 {database is locked}}
253 integrity_check lock-2.10
255 # Try to start two transactions in a row
258 execsql {BEGIN TRANSACTION}
259 set r [catch {execsql {BEGIN TRANSACTION}} msg]
262 } {1 {cannot start a transaction within a transaction}}
263 integrity_check lock-3.2
265 # Make sure the busy handler and error messages work when
266 # opening a new pointer to the database while another pointer
267 # has the database locked.
271 catch {db eval ROLLBACK}
274 set rc [catch {db2 eval {SELECT * FROM t1}} msg]
276 } {1 {database is locked}}
278 set ::callback_value {}
279 set rc [catch {db2 eval {SELECT * FROM t1}} msg]
280 lappend rc $msg $::callback_value
281 } {1 {database is locked} {}}
283 proc callback {file count} {
284 lappend ::callback_value $count
288 set rc [catch {db2 eval {SELECT * FROM t1}} msg]
289 lappend rc $msg $::callback_value
290 } {1 {database is locked} {1 2 3 4 5}}
293 # When one thread is writing, other threads cannot read. Except if the
294 # writing thread is writing to its temporary tables, the other threads
306 db function tx_exec tx_exec
308 INSERT INTO t1(a,b) SELECT 3, tx_exec('SELECT y FROM t2 LIMIT 1');
310 } {1 {database is locked}}
313 CREATE TEMP TABLE t3(x);
319 INSERT INTO t3 SELECT tx_exec('SELECT y FROM t2 LIMIT 1');
329 UPDATE t1 SET a=tx_exec('SELECT x FROM t2');
331 } {1 {database is locked}}
339 UPDATE t3 SET x=tx_exec('SELECT x FROM t2');