aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/www/sharedcache.tcl
diff options
context:
space:
mode:
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.tcl221
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#
4set rcsid {$Id: }
5source common.tcl
6header {SQLite Shared-Cache Mode}
7
8proc 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}
22set pnum(1) 0
23set pnum(2) 0
24set pnum(3) 0
25set pnum(4) 0
26set pnum(5) 0
27set pnum(6) 0
28set pnum(7) 0
29set pnum(8) 0
30
31HEADING 1 {SQLite Shared-Cache Mode}
32
33puts {
34<p>Starting with version 3.3.0, SQLite includes a special "shared-cache"
35mode (disabled by default) intended for use in embedded servers. If
36shared-cache mode is enabled and a thread establishes multiple connections
37to the same database, the connections share a single data and schema cache.
38This can significantly reduce the quantity of memory and IO required by
39the system.</p>
40
41<p>Using shared-cache mode imposes some extra restrictions on
42passing database handles between threads and changes the semantics
43of the locking model in some cases. These details are described in full by
44this 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>
46for details) is assumed.</p>
47}
48
49HEADING 1 {Shared-Cache Locking Model}
50
51puts {
52<p>Externally, from the point of view of another process or thread, two
53or more database connections using a shared-cache appear as a single
54connection. The locking protocol used to arbitrate between multiple
55shared-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
80database connections have been established. Connection 1 is a normal
81SQLite database connection. Connections 2 and 3 share a cache (and so must
82have been established by the same process thread). The normal locking
83protocol is used to serialize database access between connection 1 and
84the shared cache. The internal protocol used to serialize (or not, see
85"Read-Uncommitted Isolation Mode" below) access to the shared-cache by
86connections 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,
90transaction level locking, table level locking and schema level locking.
91They are described in the following three sub-sections.</p>
92
93}
94
95HEADING 2 {Transaction Level Locking}
96
97puts {
98<p>SQLite connections can open two kinds of transactions, read and write
99transactions. This is not done explicitly, a transaction is implicitly a
100read-transaction until it first writes to a database table, at which point
101it becomes a write-transaction.
102</p>
103<p>At most one connection to a single shared cache may open a
104write transaction at any one time. This may co-exist with any number of read
105transactions.
106</p>
107}
108
109HEADING 2 {Table Level Locking}
110
111puts {
112<p>When two or more connections use a shared-cache, locks are used to
113serialize concurrent access attempts on a per-table basis. Tables support
114two types of locks, "read-locks" and "write-locks". Locks are granted to
115connections - at any one time, each database connection has either a
116read-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
120or a single active write lock. To read data a table, a connection must
121first obtain a read-lock. To write to a table, a connection must obtain a
122write-lock on that table. If a required table lock cannot be obtained,
123the 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
127current transaction (read or write) is concluded.
128</p>
129}
130
131HEADING 3 {Read-Uncommitted Isolation Mode}
132
133puts {
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
139to obtain read-locks before reading from database tables as described
140above. This can lead to inconsistent query results if another database
141connection modifies a table while it is being read, but it also means that
142a read-transaction opened by a connection in read-uncommitted mode can
143neither 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
146database tables (i.e. read-uncommitted connections must still obtain
147write-locks and hence database writes may still block or be blocked).
148Also, read-uncommitted mode has no effect on the <i>sqlite_master</i>
149locks 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 = &lt;boolean&gt;;
160
161 /* Retrieve the current value of the read-uncommitted flag */
162 PRAGMA read_uncommitted;
163</pre>
164}
165
166HEADING 2 {Schema (sqlite_master) Level Locking}
167
168puts {
169<p>The <i>sqlite_master</i> table supports shared-cache read and write
170locks in the same way as all other database tables (see description
171above). 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
176accessing 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.
178a 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
182is holding a write-lock on the <i>sqlite_master</i> table of any attached
183database (including the default database, "main").
184</li>
185</ul>
186}
187
188HEADING 1 {Thread Related Issues}
189
190puts {
191<p>When shared-cache mode is enabled, a database connection may only be
192used by the thread that called sqlite3_open() to create it. If another
193thread attempts to use the database connection, in most cases an
194SQLITE_MISUSE error is returned. However this is not guaranteed and
195programs should not depend on this behaviour, in some cases a segfault
196may result.
197</p>
198}
199
200HEADING 1 {Enabling Shared-Cache Mode}
201
202puts {
203<p>Shared-cache mode is enabled on a thread-wide basis. Using the C
204interface, the following API can be used to enable or disable shared-cache
205mode for the calling thread:
206</p>
207
208<pre>
209int sqlite3_enable_shared_cache(int);
210</pre>
211
212<p>It is illegal to call sqlite3_enable_shared_cache() if one or more
213open database connections were opened by the calling thread. If the argument
214is non-zero, shared-cache mode is enabled. If the argument is zero,
215shared-cache mode is disabled. The return value is either SQLITE_OK (if the
216operation was successful), SQLITE_NOMEM (if a malloc() failed), or
217SQLITE_MISUSE (if the thread has open database connections).
218</p>
219}
220
221footer $rcsid