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.
31 sqlite3 db3 "file:test.db?cache=private" -uri 1
34 CREATE TABLE abc(a, b, c);
38 # Check that an exclusive lock cannot be obtained if some other
39 # shared-cache connection has a read-lock on a table.
48 catchsql { BEGIN EXCLUSIVE } db
49 } {1 {database table is locked}}
51 execsql { SELECT * FROM abc } db3
54 catchsql { INSERT INTO abc VALUES(1, 2, 3) } db3
55 } {1 {database is locked}}
57 execsql { COMMIT } db2
60 # Check that an exclusive lock prevents other shared-cache users from
61 # starting a transaction.
64 set ::DB2 [sqlite3_connection_pointer db2]
65 set ::STMT1 [sqlite3_prepare $DB2 "SELECT * FROM abc" -1 TAIL]
66 set ::STMT2 [sqlite3_prepare $DB2 "BEGIN EXCLUSIVE" -1 TAIL]
67 set ::STMT3 [sqlite3_prepare $DB2 "BEGIN IMMEDIATE" -1 TAIL]
68 set ::STMT4 [sqlite3_prepare $DB2 "BEGIN" -1 TAIL]
69 set ::STMT5 [sqlite3_prepare $DB2 "COMMIT" -1 TAIL]
70 execsql { BEGIN EXCLUSIVE } db
73 catchsql { BEGIN EXCLUSIVE } db2
74 } {1 {database schema is locked: main}}
76 catchsql { BEGIN IMMEDIATE } db2
77 } {1 {database schema is locked: main}}
78 do_test tkt2854-1.10 {
79 # This fails because the schema of main cannot be verified.
80 catchsql { BEGIN } db2
81 } {1 {database schema is locked: main}}
83 # Check that an exclusive lock prevents other shared-cache users from
84 # reading the database. Use stored statements so that the error occurs
85 # at the b-tree level, not the schema level.
87 do_test tkt2854-1.11 {
88 list [sqlite3_step $::STMT1] [sqlite3_finalize $::STMT1]
89 } {SQLITE_ERROR SQLITE_LOCKED}
90 do_test tkt2854-1.12 {
91 list [sqlite3_step $::STMT2] [sqlite3_finalize $::STMT2]
92 } {SQLITE_ERROR SQLITE_LOCKED}
93 do_test tkt2854-1.13 {
94 list [sqlite3_step $::STMT3] [sqlite3_finalize $::STMT3]
95 } {SQLITE_ERROR SQLITE_LOCKED}
96 do_test tkt2854-1.14 {
97 # A regular "BEGIN" doesn't touch any databases. So it succeeds.
98 list [sqlite3_step $::STMT4] [sqlite3_finalize $::STMT4]
99 } {SQLITE_DONE SQLITE_OK}
100 do_test tkt2854-1.15 {
102 list [sqlite3_step $::STMT5] [sqlite3_finalize $::STMT5]
103 } {SQLITE_DONE SQLITE_OK}
105 # Try to read the database using connection "db3" (which does not share
106 # a cache with "db"). The database should be locked.
107 do_test tkt2854-1.16 {
108 catchsql { SELECT * FROM abc } db3
109 } {1 {database is locked}}
110 do_test tkt2854-1.17 {
111 execsql { COMMIT } db
113 do_test tkt2854-1.18 {
114 execsql { SELECT * FROM abc } db2
117 # Check that if an attempt to obtain an exclusive lock fails because an
118 # attached db cannot be locked, the internal exclusive flag used by
119 # shared-cache users is correctly cleared.
120 do_test tkt2854-1.19 {
121 forcedelete test2.db test2.db-journal
123 execsql { CREATE TABLE def(d, e, f) } db4
124 execsql { ATTACH 'test2.db' AS aux } db
126 do_test tkt2854-1.20 {
127 execsql {BEGIN IMMEDIATE} db4
128 catchsql {BEGIN EXCLUSIVE} db
129 } {1 {database table is locked}}
130 do_test tkt2854-1.21 {
131 execsql {SELECT * FROM abc} db2
138 sqlite3_enable_shared_cache $::enable_shared_cache