tkt2854.test 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # 2007 December 20
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # 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. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. db close
  16. ifcapable !shared_cache {
  17. finish_test
  18. return
  19. }
  20. set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  21. # Open 3 database connections. Connection "db" and "db2" share a cache.
  22. # Connection "db3" has its own cache.
  23. #
  24. do_test tkt2854-1.1 {
  25. sqlite3 db test.db
  26. sqlite3 db2 test.db
  27. # This is taken from shared.test. The Windows VFS expands
  28. # ./test.db (and test.db) to be the same thing so the path
  29. # matches and they share a cache. By changing the case
  30. # for Windows platform, we get around this and get a separate
  31. # connection.
  32. if {$::tcl_platform(platform)=="unix"} {
  33. sqlite3 db3 ./test.db
  34. } else {
  35. sqlite3 db3 TEST.DB
  36. }
  37. db eval {
  38. CREATE TABLE abc(a, b, c);
  39. }
  40. } {}
  41. # Check that an exclusive lock cannot be obtained if some other
  42. # shared-cache connection has a read-lock on a table.
  43. #
  44. do_test tkt2854-1.2 {
  45. execsql {
  46. BEGIN;
  47. SELECT * FROM abc;
  48. } db2
  49. } {}
  50. do_test tkt2854-1.3 {
  51. catchsql { BEGIN EXCLUSIVE } db
  52. } {1 {database table is locked}}
  53. do_test tkt2854-1.4 {
  54. execsql { SELECT * FROM abc } db3
  55. } {}
  56. do_test tkt2854-1.5 {
  57. catchsql { INSERT INTO abc VALUES(1, 2, 3) } db3
  58. } {1 {database is locked}}
  59. do_test tkt2854-1.6 {
  60. execsql { COMMIT } db2
  61. } {}
  62. # Check that an exclusive lock prevents other shared-cache users from
  63. # starting a transaction.
  64. #
  65. do_test tkt2854-1.7 {
  66. set ::DB2 [sqlite3_connection_pointer db2]
  67. set ::STMT1 [sqlite3_prepare $DB2 "SELECT * FROM abc" -1 TAIL]
  68. set ::STMT2 [sqlite3_prepare $DB2 "BEGIN EXCLUSIVE" -1 TAIL]
  69. set ::STMT3 [sqlite3_prepare $DB2 "BEGIN IMMEDIATE" -1 TAIL]
  70. set ::STMT4 [sqlite3_prepare $DB2 "BEGIN" -1 TAIL]
  71. set ::STMT5 [sqlite3_prepare $DB2 "COMMIT" -1 TAIL]
  72. execsql { BEGIN EXCLUSIVE } db
  73. } {}
  74. do_test tkt2854-1.8 {
  75. catchsql { BEGIN EXCLUSIVE } db2
  76. } {1 {database schema is locked: main}}
  77. do_test tkt2854-1.9 {
  78. catchsql { BEGIN IMMEDIATE } db2
  79. } {1 {database schema is locked: main}}
  80. do_test tkt2854-1.10 {
  81. # This fails because the schema of main cannot be verified.
  82. catchsql { BEGIN } db2
  83. } {1 {database schema is locked: main}}
  84. # Check that an exclusive lock prevents other shared-cache users from
  85. # reading the database. Use stored statements so that the error occurs
  86. # at the b-tree level, not the schema level.
  87. #
  88. do_test tkt2854-1.11 {
  89. list [sqlite3_step $::STMT1] [sqlite3_finalize $::STMT1]
  90. } {SQLITE_ERROR SQLITE_LOCKED}
  91. do_test tkt2854-1.12 {
  92. list [sqlite3_step $::STMT2] [sqlite3_finalize $::STMT2]
  93. } {SQLITE_ERROR SQLITE_LOCKED}
  94. do_test tkt2854-1.13 {
  95. list [sqlite3_step $::STMT3] [sqlite3_finalize $::STMT3]
  96. } {SQLITE_ERROR SQLITE_LOCKED}
  97. do_test tkt2854-1.14 {
  98. # A regular "BEGIN" doesn't touch any databases. So it succeeds.
  99. list [sqlite3_step $::STMT4] [sqlite3_finalize $::STMT4]
  100. } {SQLITE_DONE SQLITE_OK}
  101. do_test tkt2854-1.15 {
  102. # As does a COMMIT.
  103. list [sqlite3_step $::STMT5] [sqlite3_finalize $::STMT5]
  104. } {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
  112. } {}
  113. do_test tkt2854-1.18 {
  114. execsql { SELECT * FROM abc } db2
  115. } {}
  116. # Check that if an attempt to obtain an exclusive lock fails because an
  117. # attached db cannot be locked, the internal exclusive flag used by
  118. # shared-cache users is correctly cleared.
  119. do_test tkt2854-1.19 {
  120. forcedelete test2.db test2.db-journal
  121. sqlite3 db4 test2.db
  122. execsql { CREATE TABLE def(d, e, f) } db4
  123. execsql { ATTACH 'test2.db' AS aux } db
  124. } {}
  125. do_test tkt2854-1.20 {
  126. execsql {BEGIN IMMEDIATE} db4
  127. catchsql {BEGIN EXCLUSIVE} db
  128. } {1 {database table is locked}}
  129. do_test tkt2854-1.21 {
  130. execsql {SELECT * FROM abc} db2
  131. } {}
  132. db close
  133. db2 close
  134. db3 close
  135. db4 close
  136. sqlite3_enable_shared_cache $::enable_shared_cache
  137. finish_test