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: tkt2854.test,v 1.4 2009/03/16 13:19:36 danielk1977 Exp $
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
18 ifcapable !shared_cache {
23 set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
25 # Open 3 database connections. Connection "db" and "db2" share a cache.
26 # Connection "db3" has its own cache.
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
37 if {$::tcl_platform(platform)=="unix"} {
44 CREATE TABLE abc(a, b, c);
48 # Check that an exclusive lock cannot be obtained if some other
49 # shared-cache connection has a read-lock on a table.
58 catchsql { BEGIN EXCLUSIVE } db
59 } {1 {database table is locked}}
61 execsql { SELECT * FROM abc } db3
64 catchsql { INSERT INTO abc VALUES(1, 2, 3) } db3
65 } {1 {database is locked}}
67 execsql { COMMIT } db2
70 # Check that an exclusive lock prevents other shared-cache users from
71 # starting a transaction.
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
83 catchsql { BEGIN EXCLUSIVE } db2
84 } {1 {database schema is locked: main}}
86 catchsql { BEGIN IMMEDIATE } db2
87 } {1 {database schema is locked: main}}
88 do_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}}
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.
97 do_test tkt2854-1.11 {
98 list [sqlite3_step $::STMT1] [sqlite3_finalize $::STMT1]
99 } {SQLITE_ERROR SQLITE_LOCKED}
100 do_test tkt2854-1.12 {
101 list [sqlite3_step $::STMT2] [sqlite3_finalize $::STMT2]
102 } {SQLITE_ERROR SQLITE_LOCKED}
103 do_test tkt2854-1.13 {
104 list [sqlite3_step $::STMT3] [sqlite3_finalize $::STMT3]
105 } {SQLITE_ERROR SQLITE_LOCKED}
106 do_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}
110 do_test tkt2854-1.15 {
112 list [sqlite3_step $::STMT5] [sqlite3_finalize $::STMT5]
113 } {SQLITE_DONE SQLITE_OK}
115 # Try to read the database using connection "db3" (which does not share
116 # a cache with "db"). The database should be locked.
117 do_test tkt2854-1.16 {
118 catchsql { SELECT * FROM abc } db3
119 } {1 {database is locked}}
120 do_test tkt2854-1.17 {
121 execsql { COMMIT } db
123 do_test tkt2854-1.18 {
124 execsql { SELECT * FROM abc } db2
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.
130 do_test tkt2854-1.19 {
131 forcedelete test2.db test2.db-journal
133 execsql { CREATE TABLE def(d, e, f) } db4
134 execsql { ATTACH 'test2.db' AS aux } db
136 do_test tkt2854-1.20 {
137 execsql {BEGIN IMMEDIATE} db4
138 catchsql {BEGIN EXCLUSIVE} db
139 } {1 {database table is locked}}
140 do_test tkt2854-1.21 {
141 execsql {SELECT * FROM abc} db2
148 sqlite3_enable_shared_cache $::enable_shared_cache