-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtransactions_lang_java.html
173 lines (173 loc) · 14.6 KB
/
transactions_lang_java.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>WiredTiger: Transactions in Java</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
$(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="wiredtiger.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><a href="http://wiredtiger.com/"><img alt="Logo" src="LogoFinal-header.png" alt="WiredTiger" /></a></td>
<td style="padding-left: 0.5em;">
<div id="projectname">
 <span id="projectnumber">Version 2.9.2</span>
</div>
<div id="projectbrief"><!-- 2.9.2 --></div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="banner">
<a href="https://github.com/wiredtiger/wiredtiger">Fork me on GitHub</a>
<a class="last" href="http://groups.google.com/group/wiredtiger-users">Join my user group</a>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
<li><a href="community.html"><span>Community</span></a></li>
<li><a href="license.html"><span>License</span></a></li>
</ul>
</div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('transactions_lang_java.html','');});
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">Transactions in Java </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="transactions_acid_lang_java"></a>
ACID properties</h1>
<p>Transactions provide a powerful abstraction for multiple threads to operate on data concurrently because they have the following properties:</p>
<ul>
<li>Atomicity: all or none of a transaction is completed.</li>
<li>Consistency: if each transaction maintains some property when considered separately, then the combined effect of executing the transactions concurrently will maintain the same property.</li>
<li>Isolation: developers can reason about transactions as if they run single-threaded.</li>
<li>Durability: once a transaction commits, its updates cannot be lost.</li>
</ul>
<p>WiredTiger supports transactions with the following caveats to the ACID properties:</p>
<ul>
<li>the maximum level of isolation supported is snapshot isolation. See <a class="el" href="transactions_lang_java.html#transaction_isolation_lang_java">Isolation levels</a> for more details.</li>
<li>transactional updates are made durable by a combination of checkpoints and logging. See <a class="el" href="checkpoint_lang_java.html">Checkpoint durability in Java</a> for information on checkpoint durability and <a class="el" href="durability_lang_java.html">Commit-level durability in Java</a> for information on commit-level durability.</li>
<li>each transaction's uncommitted changes must fit in memory: for efficiency, WiredTiger does not write to the log until a transaction commits.</li>
</ul>
<h1><a class="anchor" id="transactions_api_lang_java"></a>
Transactional API</h1>
<p>In WiredTiger, transaction operations are methods off the Session class.</p>
<p>Applications call Session.begin_transaction to start a new transaction. Operations subsequently performed using that Session handle, including operations on any cursors open in that Session handle (whether opened before or after the Session.begin_transaction call), are part of the transaction and their effects committed by calling Session.commit_transaction, or discarded by calling Session.rollback_transaction.</p>
<p>If Session.commit_transaction returns an error for any reason, the transaction was rolled back, not committed.</p>
<p>When transactions are used, data operations can encounter a conflict and fail with the <a class="el" href="group__wt.html#ga5ee3c6fcd074e11efd118f3e68e91db8" title="Conflict between concurrent operations. ">WT_ROLLBACK</a> error. If this error occurs, transactions should be rolled back with Session.rollback_transaction and the operation retried.</p>
<p>The Session.rollback_transaction method implicitly resets all cursors in the session as if the Cursor.reset method was called, discarding any cursor position as well as any key and value.</p>
<div class="fragment"><div class="line"> cursor = session.open_cursor(<span class="stringliteral">"table:mytable"</span>, null, null);</div>
<div class="line"> ret = session.begin_transaction(null);</div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Cursors may be opened before or after the transaction begins, and in</span></div>
<div class="line"><span class="comment"> * either case, subsequent operations are included in the transaction.</span></div>
<div class="line"><span class="comment"> * The begin_transaction call resets all open cursors.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"></div>
<div class="line"> cursor.putKeyString(<span class="stringliteral">"key"</span>);</div>
<div class="line"> cursor.putValueString(<span class="stringliteral">"value"</span>);</div>
<div class="line"> <span class="keywordflow">switch</span> (ret = cursor.update()) {</div>
<div class="line"> <span class="keywordflow">case</span> 0: <span class="comment">/* Update success */</span></div>
<div class="line"> ret = session.commit_transaction(null);</div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * The commit_transaction call resets all open cursors.</span></div>
<div class="line"><span class="comment"> * If commit_transaction fails, the transaction was rolled-back.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> <span class="keywordflow">case</span> wiredtiger.WT_ROLLBACK: <span class="comment">/* Update conflict */</span></div>
<div class="line"> <span class="keywordflow">default</span>: <span class="comment">/* Other error */</span></div>
<div class="line"> ret = session.rollback_transaction(null);</div>
<div class="line"> <span class="comment">/* The rollback_transaction call resets all open cursors. */</span></div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Cursors remain open and may be used for multiple transactions. */</span></div>
</div><!-- fragment --> <h1><a class="anchor" id="transactions_implicit_lang_java"></a>
Implicit transactions</h1>
<p>If a cursor is used when no explicit transaction is active in a session, reads are performed at the isolation level of the session, set with the <code>isolation</code> key to Connection.open_session, and successful updates are automatically committed before the update operation returns.</p>
<p>Any operation consisting of multiple related updates should be enclosed in an explicit transaction to ensure the updates are applied atomically.</p>
<p>If an implicit transaction successfully commits, the cursors in the Session remain positioned. If an implicit transaction fails, all cursors in the Session are reset, as if Cursor.reset were called, discarding any position or key/value information they may have.</p>
<p>See <a class="el" href="cursors_lang_java.html#cursors_transactions_lang_java">Cursors and Transactions</a> for more information.</p>
<h1><a class="anchor" id="transactions_concurrency_lang_java"></a>
Concurrency control</h1>
<p>WiredTiger uses optimistic concurrency control algorithms. This avoids the bottleneck of a centralized lock manager and ensures transactional operations do not block: reads do not block writes, and vice versa.</p>
<p>Further, writes do not block writes, although concurrent transactions updating the same value will fail with <a class="el" href="group__wt.html#ga5ee3c6fcd074e11efd118f3e68e91db8" title="Conflict between concurrent operations. ">WT_ROLLBACK</a>. Some applications may benefit from application-level synchronization to avoid repeated attempts to rollback and update the same value.</p>
<p>Operations in transactions may also fail with the <a class="el" href="group__wt.html#ga5ee3c6fcd074e11efd118f3e68e91db8" title="Conflict between concurrent operations. ">WT_ROLLBACK</a> error if some resource cannot be allocated after repeated attempts. For example, if the cache is not large enough to hold the updates required to satisfy transactional readers, an operation may fail and return <a class="el" href="group__wt.html#ga5ee3c6fcd074e11efd118f3e68e91db8" title="Conflict between concurrent operations. ">WT_ROLLBACK</a>.</p>
<h1><a class="anchor" id="transaction_isolation_lang_java"></a>
Isolation levels</h1>
<p>WiredTiger supports <code>read-uncommitted</code>, <code>read-committed</code> and <code>snapshot</code> isolation levels; the default isolation level is <code>read-committed</code>.</p>
<ul>
<li><code>read-uncommitted</code>: Transactions can see changes made by other transactions before those transactions are committed. Dirty reads, non-repeatable reads and phantoms are possible.</li>
<li><code>read-committed</code>: Transactions cannot see changes made by other transactions before those transactions are committed. Dirty reads are not possible; non-repeatable reads and phantoms are possible. Committed changes from concurrent transactions become visible when no cursor is positioned in the read-committed transaction.</li>
<li><code>snapshot</code>: Transactions read the versions of records committed before the transaction started. Dirty reads and non-repeatable reads are not possible; phantoms are possible.<br />
<br />
Snapshot isolation is a strong guarantee, but not equivalent to a single-threaded execution of the transactions, known as serializable isolation. Concurrent transactions T1 and T2 running under snapshot isolation may both commit and produce a state that neither (T1 followed by T2) nor (T2 followed by T1) could have produced, if there is overlap between T1's reads and T2's writes, and between T1's writes and T2's reads.</li>
</ul>
<p>The transaction isolation level can be configured on a per-transaction basis:</p>
<div class="fragment"><div class="line"> <span class="comment">/* A single transaction configured for snapshot isolation. */</span></div>
<div class="line"> cursor = session.open_cursor(<span class="stringliteral">"table:mytable"</span>, null, null);</div>
<div class="line"> ret = session.begin_transaction(<span class="stringliteral">"isolation=snapshot"</span>);</div>
<div class="line"> cursor.putKeyString(<span class="stringliteral">"some-key"</span>);</div>
<div class="line"> cursor.putValueString(<span class="stringliteral">"some-value"</span>);</div>
<div class="line"> ret = cursor.update();</div>
<div class="line"> ret = session.commit_transaction(null);</div>
</div><!-- fragment --><p> Additionally, the default transaction isolation can be configured and re-configured on a per-session basis:</p>
<div class="fragment"><div class="line"> <span class="comment">/* Open a session configured for read-uncommitted isolation. */</span></div>
<div class="line"> session = conn.open_session(</div>
<div class="line"> <span class="stringliteral">"isolation=read_uncommitted"</span>);</div>
</div><!-- fragment --> <div class="fragment"><div class="line"> <span class="comment">/* Re-configure a session for snapshot isolation. */</span></div>
<div class="line"> ret = session.reconfigure(<span class="stringliteral">"isolation=snapshot"</span>);</div>
</div><!-- fragment --> <h1><a class="anchor" id="transaction_named_snapshots_lang_java"></a>
Named Snapshots</h1>
<p>Applications can create named snapshots by calling Session.snapshot with a configuration that includes <code>"name=foo"</code>. This configuration creates a new named snapshot, as if a snapshot isolation transaction were started at the time of the Session.snapshot call.</p>
<p>Subsequent transactions can be started "as of" that snapshot by calling Session.begin_transaction with a configuration that includes <code>snapshot=foo</code>. That transaction will run at snapshot isolation as if the transaction started at the time of the Session.snapshot call that created the snapshot.</p>
<p>Named snapshots keep data pinned in cache as if a real transaction were running for the time that the named snapshot is active. The resources associated with named snapshots should be released by calling Session.snapshot with a configuration that includes <code>"drop="</code>. See Session.snapshot documentation for details of the semantics supported by the drop configuration.</p>
<p>Named snapshots are not durable: they do not survive Connection.close. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="index.html">Reference Guide</a></li><li class="navelem"><a class="el" href="programming_lang_java.html">Writing WiredTiger applications in Java</a></li>
<li class="footer">Copyright (c) 2008-2016 MongoDB, Inc. All rights reserved. Contact <a href="mailto:[email protected]">[email protected]</a> for more information.</li>
</ul>
</div>
</body>
</html>