AryaWu/sqlite
0
1# 2007 December 202#3# The author disclaims copyright to this source code. In place of4# a legal notice, here is a blessing:5#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.9#10#***********************************************************************11#12# $Id: tkt2854.test,v 1.4 2009/03/16 13:19:36 danielk1977 Exp $13 14set testdir [file dirname $argv0]15source $testdir/tester.tcl16db close17 18ifcapable !shared_cache {19 finish_test20 return21}22 23set ::enable_shared_cache [sqlite3_enable_shared_cache 1]24 25# Open 3 database connections. Connection "db" and "db2" share a cache.26# Connection "db3" has its own cache.27#28do_test tkt2854-1.1 {29 sqlite3 db test.db30 sqlite3 db2 test.db31 sqlite3 db3 "file:test.db?cache=private" -uri 132 33 db eval {34 CREATE TABLE abc(a, b, c);35 }36} {}37 38# Check that an exclusive lock cannot be obtained if some other 39# shared-cache connection has a read-lock on a table.40#41do_test tkt2854-1.2 {42 execsql { 43 BEGIN;44 SELECT * FROM abc;45 } db246} {}47do_test tkt2854-1.3 {48 catchsql { BEGIN EXCLUSIVE } db49} {1 {database table is locked}}50do_test tkt2854-1.4 {51 execsql { SELECT * FROM abc } db352} {}53do_test tkt2854-1.5 {54 catchsql { INSERT INTO abc VALUES(1, 2, 3) } db355} {1 {database is locked}}56do_test tkt2854-1.6 {57 execsql { COMMIT } db258} {}59 60# Check that an exclusive lock prevents other shared-cache users from61# starting a transaction.62#63do_test tkt2854-1.7 {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 } db71} {}72do_test tkt2854-1.8 {73 catchsql { BEGIN EXCLUSIVE } db274} {1 {database schema is locked: main}}75do_test tkt2854-1.9 {76 catchsql { BEGIN IMMEDIATE } db277} {1 {database schema is locked: main}}78do_test tkt2854-1.10 {79 # This fails because the schema of main cannot be verified.80 catchsql { BEGIN } db281} {1 {database schema is locked: main}}82 83# Check that an exclusive lock prevents other shared-cache users from84# reading the database. Use stored statements so that the error occurs85# at the b-tree level, not the schema level.86#87do_test tkt2854-1.11 {88 list [sqlite3_step $::STMT1] [sqlite3_finalize $::STMT1]89} {SQLITE_ERROR SQLITE_LOCKED}90do_test tkt2854-1.12 {91 list [sqlite3_step $::STMT2] [sqlite3_finalize $::STMT2]92} {SQLITE_ERROR SQLITE_LOCKED}93do_test tkt2854-1.13 {94 list [sqlite3_step $::STMT3] [sqlite3_finalize $::STMT3]95} {SQLITE_ERROR SQLITE_LOCKED}96do_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}100do_test tkt2854-1.15 {101 # As does a COMMIT.102 list [sqlite3_step $::STMT5] [sqlite3_finalize $::STMT5]103} {SQLITE_DONE SQLITE_OK}104 105# Try to read the database using connection "db3" (which does not share106# a cache with "db"). The database should be locked.107do_test tkt2854-1.16 {108 catchsql { SELECT * FROM abc } db3109} {1 {database is locked}}110do_test tkt2854-1.17 {111 execsql { COMMIT } db112} {}113do_test tkt2854-1.18 {114 execsql { SELECT * FROM abc } db2115} {}116 117# Check that if an attempt to obtain an exclusive lock fails because an118# attached db cannot be locked, the internal exclusive flag used by119# shared-cache users is correctly cleared.120do_test tkt2854-1.19 {121 forcedelete test2.db test2.db-journal122 sqlite3 db4 test2.db123 execsql { CREATE TABLE def(d, e, f) } db4124 execsql { ATTACH 'test2.db' AS aux } db125} {}126do_test tkt2854-1.20 {127 execsql {BEGIN IMMEDIATE} db4128 catchsql {BEGIN EXCLUSIVE} db129} {1 {database table is locked}}130do_test tkt2854-1.21 {131 execsql {SELECT * FROM abc} db2132} {}133 134db close135db2 close136db3 close137db4 close138sqlite3_enable_shared_cache $::enable_shared_cache139finish_test140 