~ubuntu-branches/ubuntu/wily/sqlite3/wily

« back to all changes in this revision

Viewing changes to sharedcache.html

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2012-06-13 21:43:48 UTC
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: package-import@ubuntu.com-20120613214348-uy14uupdeq0hh04k
Tags: upstream-3.7.13/www
Import upstream version 3.7.13, component www

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 
2
<html><head>
 
3
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
 
4
<title>SQLite Shared-Cache Mode</title>
 
5
<style type="text/css">
 
6
body {
 
7
    margin: auto;
 
8
    font-family: Verdana, sans-serif;
 
9
    padding: 8px 1%;
 
10
}
 
11
 
 
12
a { color: #044a64 }
 
13
a:visited { color: #734559 }
 
14
 
 
15
.logo { position:absolute; margin:3px; }
 
16
.tagline {
 
17
  float:right;
 
18
  text-align:right;
 
19
  font-style:italic;
 
20
  width:300px;
 
21
  margin:12px;
 
22
  margin-top:58px;
 
23
}
 
24
 
 
25
.toolbar {
 
26
  text-align: center;
 
27
  line-height: 1.6em;
 
28
  margin: 0;
 
29
  padding: 0px 8px;
 
30
}
 
31
.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
 
32
.toolbar a:visited { color: white; }
 
33
.toolbar a:hover { color: #044a64; background: white; }
 
34
 
 
35
.content    { margin: 5%; }
 
36
.content dt { font-weight:bold; }
 
37
.content dd { margin-bottom: 25px; margin-left:20%; }
 
38
.content ul { padding:0px; padding-left: 15px; margin:0px; }
 
39
 
 
40
/* rounded corners */
 
41
.se  { background: url(images/se.gif) 100% 100% no-repeat #044a64}
 
42
.sw  { background: url(images/sw.gif) 0% 100% no-repeat }
 
43
.ne  { background: url(images/ne.gif) 100% 0% no-repeat }
 
44
.nw  { background: url(images/nw.gif) 0% 0% no-repeat }
 
45
 
 
46
/* Things for "fancyformat" documents start here. */
 
47
.fancy img+p {font-style:italic}
 
48
.fancy .codeblock i { color: darkblue; }
 
49
.fancy h1,.fancy h2,.fancy h3,.fancy h4 {font-weight:normal;color:#044a64}
 
50
.fancy h2 { margin-left: 10px }
 
51
.fancy h3 { margin-left: 20px }
 
52
.fancy h4 { margin-left: 30px }
 
53
.fancy th {white-space:nowrap;text-align:left;border-bottom:solid 1px #444}
 
54
.fancy th, .fancy td {padding: 0.2em 1ex; vertical-align:top}
 
55
.fancy #toc a        { color: darkblue ; text-decoration: none }
 
56
.fancy .todo         { color: #AA3333 ; font-style : italic }
 
57
.fancy .todo:before  { content: 'TODO:' }
 
58
.fancy p.todo        { border: solid #AA3333 1px; padding: 1ex }
 
59
.fancy img { display:block; }
 
60
.fancy :link:hover, .fancy :visited:hover { background: wheat }
 
61
.fancy p,.fancy ul,.fancy ol { margin: 1em 5ex }
 
62
.fancy li p { margin: 1em 0 }
 
63
/* End of "fancyformat" specific rules. */
 
64
 
 
65
</style>
 
66
  
 
67
</head>
 
68
<body>
 
69
<div><!-- container div to satisfy validator -->
 
70
 
 
71
<a href="index.html">
 
72
<img class="logo" src="images/sqlite370_banner.gif" alt="SQLite Logo"
 
73
 border="0"></a>
 
74
<div><!-- IE hack to prevent disappearing logo--></div>
 
75
<div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div>
 
76
 
 
77
<table width=100% style="clear:both"><tr><td>
 
78
  <div class="se"><div class="sw"><div class="ne"><div class="nw">
 
79
  <table width=100% style="padding:0;margin:0;cell-spacing:0"><tr>
 
80
  <td width=100%>
 
81
  <div class="toolbar">
 
82
    <a href="about.html">About</a>
 
83
    <a href="sitemap.html">Sitemap</a>
 
84
    <a href="docs.html">Documentation</a>
 
85
    <a href="download.html">Download</a>
 
86
    <a href="copyright.html">License</a>
 
87
    <a href="news.html">News</a>
 
88
    <a href="support.html">Support</a>
 
89
  </div>
 
90
<script>
 
91
  gMsg = "Search SQLite Docs..."
 
92
  function entersearch() {
 
93
    var q = document.getElementById("q");
 
94
    if( q.value == gMsg ) { q.value = "" }
 
95
    q.style.color = "black"
 
96
    q.style.fontStyle = "normal"
 
97
  }
 
98
  function leavesearch() {
 
99
    var q = document.getElementById("q");
 
100
    if( q.value == "" ) { 
 
101
      q.value = gMsg
 
102
      q.style.color = "#044a64"
 
103
      q.style.fontStyle = "italic"
 
104
    }
 
105
  }
 
106
</script>
 
107
<td>
 
108
    <div style="padding:0 1em 0px 0;white-space:nowrap">
 
109
    <form name=f method="GET" action="http://www.sqlite.org/search">
 
110
      <input id=q name=q type=text
 
111
       onfocus="entersearch()" onblur="leavesearch()" style="width:24ex;padding:1px 1ex; border:solid white 1px; font-size:0.9em ; font-style:italic;color:#044a64;" value="Search SQLite Docs...">
 
112
      <input type=submit value="Go" style="border:solid white 1px;background-color:#044a64;color:white;font-size:0.9em;padding:0 1ex">
 
113
    </form>
 
114
    </div>
 
115
  </table>
 
116
</div></div></div></div>
 
117
</td></tr></table>
 
118
<div class=startsearch></div>
 
119
  
 
120
 
 
121
 
 
122
 
 
123
 
 
124
 
 
125
<h2>1.0 SQLite Shared-Cache Mode</h2>
 
126
 
 
127
<p>Starting with version 3.3.0, SQLite includes a special "shared-cache"
 
128
mode (disabled by default) intended for use in embedded servers. If
 
129
shared-cache mode is enabled and a thread establishes multiple connections
 
130
to the same database, the connections share a single data and schema cache.
 
131
This can significantly reduce the quantity of memory and IO required by
 
132
the system.</p>
 
133
 
 
134
<p>In version 3.5.0, shared-cache mode was modified so that the same
 
135
cache can be shared across an entire process rather than just within
 
136
a single thread.  Prior to this change, there were restrictions on
 
137
passing database connections between threads.  Those restrictions were
 
138
dropped in 3.5.0 update.  This document describes shared-cache mode
 
139
as of version 3.5.0.</p>
 
140
 
 
141
<p>Shared-cache mode changes the semantics
 
142
of the locking model in some cases. The details are described by
 
143
this document. A basic understanding of the normal SQLite locking model (see
 
144
<a href="lockingv3.html">File Locking And Concurrency In SQLite Version 3</a>
 
145
for details) is assumed.</p>
 
146
 
 
147
<h2>2.0 Shared-Cache Locking Model</h2>
 
148
 
 
149
<p>Externally, from the point of view of another process or thread, two
 
150
or more <a href="c3ref/sqlite3.html">database connections</a> using a shared-cache appear as a single 
 
151
connection. The locking protocol used to arbitrate between multiple 
 
152
shared-caches or regular database users is described elsewhere.
 
153
</p>
 
154
 
 
155
<table style="margin:auto">
 
156
<tr><td>
 
157
<img src="images/shared.gif">
 
158
<!-- <pre>
 
159
            +--------------+      +--------------+
 
160
            | Connection 2 |      | Connection 3 |
 
161
            +--------------+      +--------------+
 
162
                         |          |
 
163
                         V          V
 
164
+--------------+       +--------------+
 
165
| Connection 1 |       | Shared cache |
 
166
+--------------+       +--------------+
 
167
            |            |
 
168
            V            V
 
169
          +----------------+
 
170
          |    Database    |
 
171
          +----------------+
 
172
</pre> -->
 
173
</table>
 
174
<p style="font-style:italic;text-align:center">Figure 1</p>
 
175
 
 
176
<p>Figure 1 depicts an example runtime configuration where three 
 
177
database connections have been established. Connection 1 is a normal
 
178
SQLite database connection. Connections 2 and 3 share a cache 
 
179
The normal locking
 
180
protocol is used to serialize database access between connection 1 and
 
181
the shared cache. The internal protocol used to serialize (or not, see
 
182
"Read-Uncommitted Isolation Mode" below) access to the shared-cache by
 
183
connections 2 and 3 is described in the remainder of this section.
 
184
</p>
 
185
 
 
186
<p>There are three levels to the shared-cache locking model, 
 
187
transaction level locking, table level locking and schema level locking. 
 
188
They are described in the following three sub-sections.</p>
 
189
 
 
190
<h3>2.1 Transaction Level Locking</h3>
 
191
 
 
192
<p>SQLite connections can open two kinds of transactions, read and write
 
193
transactions. This is not done explicitly, a transaction is implicitly a
 
194
read-transaction until it first writes to a database table, at which point
 
195
it becomes a write-transaction.
 
196
</p>
 
197
<p>At most one connection to a single shared cache may open a 
 
198
write transaction at any one time. This may co-exist with any number of read 
 
199
transactions. 
 
200
</p>
 
201
 
 
202
<h3>2.2 Table Level Locking</h3>
 
203
 
 
204
<p>When two or more connections use a shared-cache, locks are used to 
 
205
serialize concurrent access attempts on a per-table basis. Tables support 
 
206
two types of locks, "read-locks" and "write-locks". Locks are granted to
 
207
connections - at any one time, each database connection has either a
 
208
read-lock, write-lock or no lock on each database table.
 
209
</p>
 
210
 
 
211
<p>At any one time, a single table may have any number of active read-locks
 
212
or a single active write lock. To read data a table, a connection must 
 
213
first obtain a read-lock. To write to a table, a connection must obtain a 
 
214
write-lock on that table. If a required table lock cannot be obtained,
 
215
the query fails and SQLITE_LOCKED is returned to the caller.
 
216
</p> 
 
217
 
 
218
<p>Once a connection obtains a table lock, it is not released until the
 
219
current transaction (read or write) is concluded.
 
220
</p>
 
221
 
 
222
<h4>2.2.1 Read-Uncommitted Isolation Mode</h4>
 
223
 
 
224
<p>The behaviour described above may be modified slightly by using the 
 
225
<a href="pragma.html#pragma_read_uncommitted">read_uncommitted</a> pragma to change the isolation level from serialized 
 
226
(the default), to read-uncommitted.</p>
 
227
 
 
228
<p> A database connection in read-uncommitted mode does not attempt 
 
229
to obtain read-locks before reading from database tables as described 
 
230
above. This can lead to inconsistent query results if another database
 
231
connection modifies a table while it is being read, but it also means that
 
232
a read-transaction opened by a connection in read-uncommitted mode can
 
233
neither block nor be blocked by any other connection.</p>
 
234
 
 
235
<p>Read-uncommitted mode has no effect on the locks required to write to
 
236
database tables (i.e. read-uncommitted connections must still obtain 
 
237
write-locks and hence database writes may still block or be blocked). 
 
238
Also, read-uncommitted mode has no effect on the <i>sqlite_master</i> 
 
239
locks required by the rules enumerated below (see section 
 
240
"Schema (sqlite_master) Level Locking").
 
241
</p>
 
242
 
 
243
<blockquote><pre>
 
244
  /* Set the value of the read-uncommitted flag:
 
245
  **
 
246
  **   True  -> Set the connection to read-uncommitted mode.
 
247
  **   False -> Set the connection to serialized (the default) mode.
 
248
  */
 
249
  PRAGMA read_uncommitted = &lt;boolean&gt;;
 
250
 
 
251
  /* Retrieve the current value of the read-uncommitted flag */
 
252
  PRAGMA read_uncommitted;
 
253
</pre></blockquote>
 
254
 
 
255
<h3>2.3 Schema (sqlite_master) Level Locking</h3>
 
256
 
 
257
<p>The <i>sqlite_master</i> table supports shared-cache read and write 
 
258
locks in the same way as all other database tables (see description 
 
259
above). The following special rules also apply:
 
260
</p>
 
261
 
 
262
<ul>
 
263
<li>A connection must obtain a read-lock on <i>sqlite_master</i> before 
 
264
accessing any database tables or obtaining any other read or write locks.</li>
 
265
<li>Before executing a statement that modifies the database schema (i.e. 
 
266
a CREATE or DROP TABLE statement), a connection must obtain a write-lock on 
 
267
<i>sqlite_master</i>.
 
268
</li>
 
269
<li>A connection may not compile an SQL statement if any other connection
 
270
is holding a write-lock on the <i>sqlite_master</i> table of any attached
 
271
database (including the default database, "main"). 
 
272
</li>
 
273
</ul>
 
274
 
 
275
<h2>3.0 Thread Related Issues</h2>
 
276
 
 
277
<p>In SQLite versions 3.3.0 through 3.4.2 when shared-cache mode is enabled, 
 
278
a database connection may only be
 
279
used by the thread that called <a href="c3ref/open.html">sqlite3_open()</a> to create it.
 
280
And a connection could only share cache with another connection in the
 
281
same thread.
 
282
These restrictions were dropped beginning with SQLite version 3.5.0.
 
283
</p>
 
284
 
 
285
<h2>4.0 Shared Cache And Virtual Tables</h2>
 
286
 
 
287
<p>
 
288
In older versions of SQLite,
 
289
shared cache mode could not be used together with virtual tables.
 
290
This restriction was removed in SQLite <a href="releaselog/3_6_17.html">version 3.6.17</a>.
 
291
 
 
292
<h2>5.0 Enabling Shared-Cache Mode</h2>
 
293
 
 
294
<p>Shared-cache mode is enabled on a per-process basis. Using the C 
 
295
interface, the following API can be used to enable or disable shared-cache
 
296
mode for the calling thread:
 
297
</p>
 
298
 
 
299
<blockquote><pre>
 
300
int sqlite3_enable_shared_cache(int);
 
301
</pre></blockquote>
 
302
 
 
303
<p>Each call <a href="c3ref/enable_shared_cache.html">sqlite3_enable_shared_cache()</a> effects subsequent database
 
304
connections created using <a href="c3ref/open.html">sqlite3_open()</a>, <a href="c3ref/open.html">sqlite3_open16()</a>, or
 
305
<a href="c3ref/open.html">sqlite3_open_v2()</a>.  Database connections that already exist are
 
306
unaffected.  Each call to <a href="c3ref/enable_shared_cache.html">sqlite3_enable_shared_cache()</a> overrides
 
307
all previous calls within the same process.
 
308
</p>
 
309
 
 
310
<p>Individual database connections created using <a href="c3ref/open.html">sqlite3_open_v2()</a> can
 
311
choose to participate or not participate in shared cache mode by using
 
312
the <a href="c3ref/c_open_autoproxy.html">SQLITE_OPEN_SHAREDCACHE</a> or <a href="c3ref/c_open_autoproxy.html">SQLITE_OPEN_PRIVATECACHE</a> flags the
 
313
third parameter.  The use of either of these flags overrides the
 
314
global shared cache mode setting established by <a href="c3ref/enable_shared_cache.html">sqlite3_enable_shared_cache()</a>.
 
315
No more than one of the flags should be used; if both SQLITE_OPEN_SHAREDCACHE
 
316
and SQLITE_OPEN_PRIVATECACHE flags are used in the third argument to
 
317
<a href="c3ref/open.html">sqlite3_open_v2()</a> then the behavior is undefined.</p>
 
318
 
 
319
<p>When <a href="uri.html">URI filenames</a> are used, the "cache" query parameter can be used
 
320
to specify whether or not the database will use shared cache.  Use
 
321
"cache=shared" to enable shared cache and "cache=private" to disable
 
322
shared cache.   The ability to use URI query parameters to specify the
 
323
cache sharing behavior of a database connection allows cache sharing to
 
324
be controlled in <a href="lang_attach.html">ATTACH</a> statements.  For example:</p>
 
325
 
 
326
<blockquote><pre>
 
327
ATTACH 'file:aux.db?cache=shared' AS aux;
 
328
</pre></blockquote>
 
329
 
 
330
<a name="inmemsharedcache"></a>
 
331
<h2>6.0 Shared Cache And In-Memory Databases</h2>
 
332
 
 
333
<p>
 
334
Beginning with SQLite <a href="releaselog/3_7_13.html">version 3.7.13</a>, shared cache can be used on
 
335
<a href="inmemorydb.html">in-memory databases</a>, provided that the database is created using
 
336
a <a href="uri.html">URI filename</a>.  For backwards compatibility, shared cache is always
 
337
disable for in-memory
 
338
databases if the unadorned name ":memory:" is used to open the database.
 
339
Prior to version 3.7.13, shared cache was always
 
340
disabled for in-memory databases regardless of the database name used,
 
341
current system shared cache setting, or query parameters or flags.
 
342
</p>
 
343
 
 
344
<p>
 
345
Enabling shared-cache for an in-mmeory database allows two or more
 
346
database connections in the same process to have access to the same
 
347
in-memory database.  An in-memory database in shared cache is automatically
 
348
deleted and memory is reclaimed when the last connection to that database
 
349
closes.
 
350
</p>
 
351