shared6.test 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # 2009 April 01
  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: shared6.test,v 1.4 2009/06/05 17:09:12 drh Exp $
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. ifcapable !shared_cache { finish_test ; return }
  16. do_test shared6-1.1.1 {
  17. execsql {
  18. CREATE TABLE t1(a, b);
  19. CREATE TABLE t2(c, d);
  20. CREATE TABLE t3(e, f);
  21. }
  22. db close
  23. } {}
  24. do_test shared6-1.1.2 {
  25. set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  26. sqlite3_enable_shared_cache
  27. } {1}
  28. do_test shared6-1.1.3 {
  29. sqlite3 db1 test.db
  30. sqlite3 db2 test.db
  31. } {}
  32. # Exclusive shared-cache locks. Test the following:
  33. #
  34. # 1.2.1: If [db1] has an exclusive lock, [db2] cannot read.
  35. # 1.2.2: If [db1] has an exclusive lock, [db1] can read.
  36. # 1.2.3: If [db1] has a non-exclusive write-lock, [db2] can read.
  37. #
  38. do_test shared6-1.2.1 {
  39. execsql { SELECT * FROM t1 } db2 ;# Cache a compiled statement
  40. execsql { BEGIN EXCLUSIVE } db1
  41. catchsql { SELECT * FROM t1 } db2 ;# Execute the cached compiled statement
  42. } {1 {database table is locked}}
  43. do_test shared6-1.2.2 {
  44. execsql { SELECT * FROM t1 } db1
  45. } {}
  46. do_test shared6-1.2.3 {
  47. execsql {
  48. COMMIT;
  49. BEGIN;
  50. INSERT INTO t2 VALUES(3, 4);
  51. } db1
  52. execsql { SELECT * FROM t1 } db2
  53. } {}
  54. do_test shared6-1.2.X {
  55. execsql { COMMIT } db1
  56. } {}
  57. # Regular shared-cache locks. Verify the following:
  58. #
  59. # 1.3.1: If [db1] has a write-lock on t1, [db1] can read from t1.
  60. # 1.3.2: If [db1] has a write-lock on t1, [db2] can read from t2.
  61. # 1.3.3: If [db1] has a write-lock on t1, [db2] cannot read from t1.
  62. # 1.3.4: If [db1] has a write-lock on t1, [db2] cannot write to t1.
  63. # 1.3.5: If [db1] has a read-lock on t1, [db2] can read from t1.
  64. # 1.3.6: If [db1] has a read-lock on t1, [db2] cannot write to t1.
  65. #
  66. do_test shared6-1.3.1 {
  67. execsql {
  68. BEGIN;
  69. INSERT INTO t1 VALUES(1, 2);
  70. } db1
  71. execsql { SELECT * FROM t1 } db1
  72. } {1 2}
  73. do_test shared6-1.3.2 {
  74. execsql { SELECT * FROM t2 } db2
  75. } {3 4}
  76. do_test shared6-1.3.3 {
  77. catchsql { SELECT * FROM t1 } db2
  78. } {1 {database table is locked: t1}}
  79. do_test shared6-1.3.4 {
  80. catchsql { INSERT INTO t2 VALUES(1, 2) } db2
  81. } {1 {database table is locked}}
  82. do_test shared6-1.3.5 {
  83. execsql {
  84. COMMIT;
  85. BEGIN;
  86. SELECT * FROM t1;
  87. } db1
  88. execsql { SELECT * FROM t1 } db2
  89. } {1 2}
  90. do_test shared6-1.3.5 {
  91. catchsql { INSERT INTO t1 VALUES(5, 6) } db2
  92. } {1 {database table is locked: t1}}
  93. do_test shared6-1.3.X {
  94. execsql { COMMIT } db1
  95. } {}
  96. # Read-uncommitted mode.
  97. #
  98. # For these tests, connection [db2] is in read-uncommitted mode.
  99. #
  100. # 1.4.1: If [db1] has a write-lock on t1, [db2] can still read from t1.
  101. # 1.4.2: If [db1] has a write-lock on the db schema (sqlite_master table),
  102. # [db2] cannot read from the schema.
  103. # 1.4.3: If [db1] has a read-lock on t1, [db2] cannot write to t1.
  104. #
  105. do_test shared6-1.4.1 {
  106. execsql { PRAGMA read_uncommitted = 1 } db2
  107. execsql {
  108. BEGIN;
  109. INSERT INTO t1 VALUES(5, 6);
  110. } db1
  111. execsql { SELECT * FROM t1 } db2
  112. } {1 2 5 6}
  113. do_test shared6-1.4.2 {
  114. execsql { CREATE TABLE t4(a, b) } db1
  115. catchsql { SELECT * FROM t1 } db2
  116. } {1 {database table is locked}}
  117. do_test shared6-1.4.3 {
  118. execsql {
  119. COMMIT;
  120. BEGIN;
  121. SELECT * FROM t1;
  122. } db1
  123. catchsql { INSERT INTO t1 VALUES(7, 8) } db2
  124. } {1 {database table is locked: t1}}
  125. do_test shared6-1.X {
  126. db1 close
  127. db2 close
  128. } {}
  129. #-------------------------------------------------------------------------
  130. # The following tests - shared6-2.* - test that two database connections
  131. # that connect to the same file using different VFS implementations do
  132. # not share a cache.
  133. #
  134. if {$::tcl_platform(platform) eq "unix"} {
  135. do_test shared6-2.1 {
  136. sqlite3 db1 test.db -vfs unix
  137. sqlite3 db2 test.db -vfs unix
  138. sqlite3 db3 test.db -vfs unix-none
  139. sqlite3 db4 test.db -vfs unix-none
  140. } {}
  141. do_test shared6-2.2 {
  142. execsql { BEGIN; INSERT INTO t1 VALUES(9, 10); } db1
  143. catchsql { SELECT * FROM t1 } db2
  144. } {1 {database table is locked: t1}}
  145. do_test shared6-2.3 {
  146. execsql { SELECT * FROM t1 } db3
  147. } {1 2 5 6}
  148. do_test shared6-2.3 {
  149. execsql { COMMIT } db1
  150. execsql { BEGIN; INSERT INTO t1 VALUES(11, 12); } db3
  151. catchsql { SELECT * FROM t1 } db4
  152. } {1 {database table is locked: t1}}
  153. do_test shared6-2.4 {
  154. execsql { SELECT * FROM t1 } db1
  155. } {1 2 5 6 9 10}
  156. do_test shared6-2.5 {
  157. execsql { COMMIT } db3
  158. } {}
  159. do_test shared6-2.X {
  160. db1 close
  161. db2 close
  162. db3 close
  163. db4 close
  164. } {}
  165. }
  166. #-------------------------------------------------------------------------
  167. # Test that it is possible to open an exclusive transaction while
  168. # already holding a read-lock on the database file. And that it is
  169. # not possible if some other connection holds such a lock.
  170. #
  171. do_test shared6-3.1 {
  172. sqlite3 db1 test.db
  173. sqlite3 db2 test.db
  174. sqlite3 db3 test.db
  175. } {}
  176. db1 eval {SELECT * FROM t1} {
  177. # Within this block [db1] is holding a read-lock on t1. Test that
  178. # this means t1 cannot be written by [db2].
  179. #
  180. do_test shared6-3.2 {
  181. catchsql { INSERT INTO t1 VALUES(1, 2) } db2
  182. } {1 {database table is locked: t1}}
  183. do_test shared6-3.3 {
  184. execsql { BEGIN EXCLUSIVE } db1
  185. } {}
  186. break
  187. }
  188. do_test shared6-3.4 {
  189. catchsql { SELECT * FROM t1 } db2
  190. } {1 {database schema is locked: main}}
  191. do_test shared6-3.5 {
  192. execsql COMMIT db1
  193. } {}
  194. db2 eval {SELECT * FROM t1} {
  195. do_test shared6-3.6 {
  196. catchsql { BEGIN EXCLUSIVE } db1
  197. } {1 {database table is locked}}
  198. break
  199. }
  200. do_test shared6-3.7 {
  201. execsql { BEGIN } db1
  202. execsql { BEGIN } db2
  203. } {}
  204. db2 eval {SELECT * FROM t1} {
  205. do_test shared6-3.8 {
  206. catchsql { INSERT INTO t1 VALUES(1, 2) } db1
  207. } {1 {database table is locked: t1}}
  208. break
  209. }
  210. do_test shared6-3.9 {
  211. execsql { BEGIN ; ROLLBACK } db3
  212. } {}
  213. do_test shared6-3.10 {
  214. catchsql { SELECT * FROM t1 } db3
  215. } {1 {database table is locked}}
  216. do_test shared6-3.X {
  217. db1 close
  218. db2 close
  219. db3 close
  220. } {}
  221. do_test shared6-4.1 {
  222. #forcedelete test.db test.db-journal
  223. sqlite3 db1 test.db
  224. sqlite3 db2 test.db
  225. set ::STMT [sqlite3_prepare_v2 db1 "SELECT * FROM t1" -1 DUMMY]
  226. execsql { CREATE TABLE t5(a, b) } db2
  227. } {}
  228. do_test shared6-4.2 {
  229. sqlite3_finalize $::STMT
  230. } {SQLITE_OK}
  231. do_test shared6-4.X {
  232. db1 close
  233. db2 close
  234. } {}
  235. sqlite3_enable_shared_cache $::enable_shared_cache
  236. finish_test