diff options
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/www/sharedcache.tcl')
-rw-r--r-- | libraries/sqlite/unix/sqlite-3.5.1/www/sharedcache.tcl | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/www/sharedcache.tcl b/libraries/sqlite/unix/sqlite-3.5.1/www/sharedcache.tcl new file mode 100644 index 0000000..c06c888 --- /dev/null +++ b/libraries/sqlite/unix/sqlite-3.5.1/www/sharedcache.tcl | |||
@@ -0,0 +1,221 @@ | |||
1 | # | ||
2 | # Run this script to generated a sharedcache.html output file | ||
3 | # | ||
4 | set rcsid {$Id: } | ||
5 | source common.tcl | ||
6 | header {SQLite Shared-Cache Mode} | ||
7 | |||
8 | proc HEADING {level title} { | ||
9 | global pnum | ||
10 | incr pnum($level) | ||
11 | foreach i [array names pnum] { | ||
12 | if {$i>$level} {set pnum($i) 0} | ||
13 | } | ||
14 | set h [expr {$level+1}] | ||
15 | if {$h>6} {set h 6} | ||
16 | set n $pnum(1).$pnum(2) | ||
17 | for {set i 3} {$i<=$level} {incr i} { | ||
18 | append n .$pnum($i) | ||
19 | } | ||
20 | puts "<h$h>$n $title</h$h>" | ||
21 | } | ||
22 | set pnum(1) 0 | ||
23 | set pnum(2) 0 | ||
24 | set pnum(3) 0 | ||
25 | set pnum(4) 0 | ||
26 | set pnum(5) 0 | ||
27 | set pnum(6) 0 | ||
28 | set pnum(7) 0 | ||
29 | set pnum(8) 0 | ||
30 | |||
31 | HEADING 1 {SQLite Shared-Cache Mode} | ||
32 | |||
33 | puts { | ||
34 | <p>Starting with version 3.3.0, SQLite includes a special "shared-cache" | ||
35 | mode (disabled by default) intended for use in embedded servers. If | ||
36 | shared-cache mode is enabled and a thread establishes multiple connections | ||
37 | to the same database, the connections share a single data and schema cache. | ||
38 | This can significantly reduce the quantity of memory and IO required by | ||
39 | the system.</p> | ||
40 | |||
41 | <p>Using shared-cache mode imposes some extra restrictions on | ||
42 | passing database handles between threads and changes the semantics | ||
43 | of the locking model in some cases. These details are described in full by | ||
44 | this document. A basic understanding of the normal SQLite locking model (see | ||
45 | <a href="lockingv3.html">File Locking And Concurrency In SQLite Version 3</a> | ||
46 | for details) is assumed.</p> | ||
47 | } | ||
48 | |||
49 | HEADING 1 {Shared-Cache Locking Model} | ||
50 | |||
51 | puts { | ||
52 | <p>Externally, from the point of view of another process or thread, two | ||
53 | or more database connections using a shared-cache appear as a single | ||
54 | connection. The locking protocol used to arbitrate between multiple | ||
55 | shared-caches or regular database users is described elsewhere. | ||
56 | </p> | ||
57 | |||
58 | <table style="margin:auto"> | ||
59 | <tr><td> | ||
60 | <img src="shared.gif"> | ||
61 | <!-- <pre> | ||
62 | +--------------+ +--------------+ | ||
63 | | Connection 2 | | Connection 3 | | ||
64 | +--------------+ +--------------+ | ||
65 | | | | ||
66 | V V | ||
67 | +--------------+ +--------------+ | ||
68 | | Connection 1 | | Shared cache | | ||
69 | +--------------+ +--------------+ | ||
70 | | | | ||
71 | V V | ||
72 | +----------------+ | ||
73 | | Database | | ||
74 | +----------------+ | ||
75 | </pre> --> | ||
76 | </table> | ||
77 | <p style="font-style:italic;text-align:center">Figure 1</p> | ||
78 | |||
79 | <p>Figure 1 depicts an example runtime configuration where three | ||
80 | database connections have been established. Connection 1 is a normal | ||
81 | SQLite database connection. Connections 2 and 3 share a cache (and so must | ||
82 | have been established by the same process thread). The normal locking | ||
83 | protocol is used to serialize database access between connection 1 and | ||
84 | the shared cache. The internal protocol used to serialize (or not, see | ||
85 | "Read-Uncommitted Isolation Mode" below) access to the shared-cache by | ||
86 | connections 2 and 3 is described in the remainder of this section. | ||
87 | </p> | ||
88 | |||
89 | <p>There are three levels to the shared-cache locking model, | ||
90 | transaction level locking, table level locking and schema level locking. | ||
91 | They are described in the following three sub-sections.</p> | ||
92 | |||
93 | } | ||
94 | |||
95 | HEADING 2 {Transaction Level Locking} | ||
96 | |||
97 | puts { | ||
98 | <p>SQLite connections can open two kinds of transactions, read and write | ||
99 | transactions. This is not done explicitly, a transaction is implicitly a | ||
100 | read-transaction until it first writes to a database table, at which point | ||
101 | it becomes a write-transaction. | ||
102 | </p> | ||
103 | <p>At most one connection to a single shared cache may open a | ||
104 | write transaction at any one time. This may co-exist with any number of read | ||
105 | transactions. | ||
106 | </p> | ||
107 | } | ||
108 | |||
109 | HEADING 2 {Table Level Locking} | ||
110 | |||
111 | puts { | ||
112 | <p>When two or more connections use a shared-cache, locks are used to | ||
113 | serialize concurrent access attempts on a per-table basis. Tables support | ||
114 | two types of locks, "read-locks" and "write-locks". Locks are granted to | ||
115 | connections - at any one time, each database connection has either a | ||
116 | read-lock, write-lock or no lock on each database table. | ||
117 | </p> | ||
118 | |||
119 | <p>At any one time, a single table may have any number of active read-locks | ||
120 | or a single active write lock. To read data a table, a connection must | ||
121 | first obtain a read-lock. To write to a table, a connection must obtain a | ||
122 | write-lock on that table. If a required table lock cannot be obtained, | ||
123 | the query fails and SQLITE_LOCKED is returned to the caller. | ||
124 | </p> | ||
125 | |||
126 | <p>Once a connection obtains a table lock, it is not released until the | ||
127 | current transaction (read or write) is concluded. | ||
128 | </p> | ||
129 | } | ||
130 | |||
131 | HEADING 3 {Read-Uncommitted Isolation Mode} | ||
132 | |||
133 | puts { | ||
134 | <p>The behaviour described above may be modified slightly by using the | ||
135 | <i>read_uncommitted</i> pragma to change the isolation level from serialized | ||
136 | (the default), to read-uncommitted.</p> | ||
137 | |||
138 | <p> A database connection in read-uncommitted mode does not attempt | ||
139 | to obtain read-locks before reading from database tables as described | ||
140 | above. This can lead to inconsistent query results if another database | ||
141 | connection modifies a table while it is being read, but it also means that | ||
142 | a read-transaction opened by a connection in read-uncommitted mode can | ||
143 | neither block nor be blocked by any other connection.</p> | ||
144 | |||
145 | <p>Read-uncommitted mode has no effect on the locks required to write to | ||
146 | database tables (i.e. read-uncommitted connections must still obtain | ||
147 | write-locks and hence database writes may still block or be blocked). | ||
148 | Also, read-uncommitted mode has no effect on the <i>sqlite_master</i> | ||
149 | locks required by the rules enumerated below (see section | ||
150 | "Schema (sqlite_master) Level Locking"). | ||
151 | </p> | ||
152 | |||
153 | <pre> | ||
154 | /* Set the value of the read-uncommitted flag: | ||
155 | ** | ||
156 | ** True -> Set the connection to read-uncommitted mode. | ||
157 | ** False -> Set the connectino to serialized (the default) mode. | ||
158 | */ | ||
159 | PRAGMA read_uncommitted = <boolean>; | ||
160 | |||
161 | /* Retrieve the current value of the read-uncommitted flag */ | ||
162 | PRAGMA read_uncommitted; | ||
163 | </pre> | ||
164 | } | ||
165 | |||
166 | HEADING 2 {Schema (sqlite_master) Level Locking} | ||
167 | |||
168 | puts { | ||
169 | <p>The <i>sqlite_master</i> table supports shared-cache read and write | ||
170 | locks in the same way as all other database tables (see description | ||
171 | above). The following special rules also apply: | ||
172 | </p> | ||
173 | |||
174 | <ul> | ||
175 | <li>A connection must obtain a read-lock on <i>sqlite_master</i> before | ||
176 | accessing any database tables or obtaining any other read or write locks.</li> | ||
177 | <li>Before executing a statement that modifies the database schema (i.e. | ||
178 | a CREATE or DROP TABLE statement), a connection must obtain a write-lock on | ||
179 | <i>sqlite_master</i>. | ||
180 | </li> | ||
181 | <li>A connection may not compile an SQL statement if any other connection | ||
182 | is holding a write-lock on the <i>sqlite_master</i> table of any attached | ||
183 | database (including the default database, "main"). | ||
184 | </li> | ||
185 | </ul> | ||
186 | } | ||
187 | |||
188 | HEADING 1 {Thread Related Issues} | ||
189 | |||
190 | puts { | ||
191 | <p>When shared-cache mode is enabled, a database connection may only be | ||
192 | used by the thread that called sqlite3_open() to create it. If another | ||
193 | thread attempts to use the database connection, in most cases an | ||
194 | SQLITE_MISUSE error is returned. However this is not guaranteed and | ||
195 | programs should not depend on this behaviour, in some cases a segfault | ||
196 | may result. | ||
197 | </p> | ||
198 | } | ||
199 | |||
200 | HEADING 1 {Enabling Shared-Cache Mode} | ||
201 | |||
202 | puts { | ||
203 | <p>Shared-cache mode is enabled on a thread-wide basis. Using the C | ||
204 | interface, the following API can be used to enable or disable shared-cache | ||
205 | mode for the calling thread: | ||
206 | </p> | ||
207 | |||
208 | <pre> | ||
209 | int sqlite3_enable_shared_cache(int); | ||
210 | </pre> | ||
211 | |||
212 | <p>It is illegal to call sqlite3_enable_shared_cache() if one or more | ||
213 | open database connections were opened by the calling thread. If the argument | ||
214 | is non-zero, shared-cache mode is enabled. If the argument is zero, | ||
215 | shared-cache mode is disabled. The return value is either SQLITE_OK (if the | ||
216 | operation was successful), SQLITE_NOMEM (if a malloc() failed), or | ||
217 | SQLITE_MISUSE (if the thread has open database connections). | ||
218 | </p> | ||
219 | } | ||
220 | |||
221 | footer $rcsid | ||