Debian Codegraph @

docs/gsg_txn/JAVA/lockingsubsystem.html

raw
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>The Locking Subsystem</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
10 <link rel="up" href="txnconcurrency.html" title="Chapter 4. Concurrency" />
11 <link rel="prev" href="blocking_deadlocks.html" title="Locks, Blocks, and Deadlocks" />
12 <link rel="next" href="isolation.html" title="Isolation" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">The Locking Subsystem</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="blocking_deadlocks.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 4. Concurrency</th>
26 <td width="20%" align="right"> <a accesskey="n" href="isolation.html">Next</a></td>
27 </tr>
28 </table>
29 <hr />
30 </div>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
33 <div>
34 <div>
35 <h2 class="title" style="clear: both"><a id="lockingsubsystem"></a>The Locking Subsystem</h2>
36 </div>
37 </div>
38 </div>
39 <div class="toc">
40 <dl>
41 <dt>
42 <span class="sect2">
43 <a href="lockingsubsystem.html#configuringlock">Configuring the Locking Subsystem</a>
44 </span>
45 </dt>
46 <dt>
47 <span class="sect2">
48 <a href="lockingsubsystem.html#configdeadlkdetect">Configuring Deadlock Detection</a>
49 </span>
50 </dt>
51 <dt>
52 <span class="sect2">
53 <a href="lockingsubsystem.html#deadlockresolve">Resolving Deadlocks</a>
54 </span>
55 </dt>
56 <dt>
57 <span class="sect2">
58 <a href="lockingsubsystem.html#setpriority">Setting Transaction Priorities</a>
59 </span>
60 </dt>
61 </dl>
62 </div>
63 <p>
64 In order to allow concurrent operations, DB provides the locking
65 subsystem. This subsystem provides inter- and intra- process
66 concurrency mechanisms. It is extensively used by DB concurrent
67 applications, but it can also be generally used for non-DB
68 resources.
69 </p>
70 <p>
71 This section describes the locking subsystem as it is used to
72 protect DB resources. In particular, issues on configuration are
73 examined here. For information on using the locking subsystem to
74 manage non-DB resources, see the
75 <em class="citetitle">Berkeley DB Programmer's Reference Guide</em>.
76 </p>
77 <div class="sect2" lang="en" xml:lang="en">
78 <div class="titlepage">
79 <div>
80 <div>
81 <h3 class="title"><a id="configuringlock"></a>Configuring the Locking Subsystem</h3>
82 </div>
83 </div>
84 </div>
85 <p>
86 You initialize the locking subsystem by specifying
87
88 <span>
89 <code class="literal">true</code> to the
90 <code class="methodname">EnvironmentConfig.setInitializeLocking()</code>
91 method.
92 </span>
93 </p>
94 <p>
95 Before opening your environment, you can configure various
96 values for your locking subsystem. Note that these
97 limits can only be configured before the environment is
98 opened. Also, these methods configure the entire environment,
99 not just a specific environment handle.
100 </p>
101 <p>
102 Finally, each bullet below identifies the
103 <code class="filename">DB_CONFIG</code> file parameter that can be used
104 to specify the specific locking limit. If used, these
105 <code class="filename">DB_CONFIG</code> file parameters override any
106 value that you might specify using the environment handle.
107 </p>
108 <p>
109 The limits that you can configure are as follows:
110 </p>
111 <div class="itemizedlist">
112 <ul type="disc">
113 <li>
114 <p>
115 The number of lockers supported by the environment.
116 This value is used by the environment when it is
117 opened to estimate the amount of space that it
118 should allocate for various internal data
119 structures. By default, 1,000 lockers are
120 supported.
121 </p>
122 <p>
123 To configure this value, use the
124
125
126 <span>
127 <code class="methodname">EnvironmentConfig.setMaxLockers()</code>
128 method.
129 </span>
130 </p>
131 <p>
132 As an alternative to this method, you can configure this
133 value using the <code class="filename">DB_CONFIG</code> file's
134 <code class="literal">set_lk_max_lockers</code> parameter.
135 </p>
136 </li>
137 <li>
138 <p>
139 The number of locks supported by the environment.
140 By default, 1,000 locks are supported.
141 </p>
142 <p>
143 To configure this value, use the
144
145 <span>
146 <code class="methodname">EnvironmentConfig.setMaxLocks()</code>
147 method.
148 </span>
149 </p>
150 <p>
151 As an alternative to this method, you can configure this
152 value using the <code class="filename">DB_CONFIG</code> file's
153 <code class="literal">set_lk_max_locks</code> parameter.
154 </p>
155 </li>
156 <li>
157 <p>
158 The number of locked objects supported by the
159 environment. By default, 1,000 objects can be
160 locked.
161 </p>
162 <p>
163 To configure this value, use the
164
165 <span>
166 <code class="methodname">EnvironmentConfig.setMaxLockObjects()</code>
167 method.
168 </span>
169 </p>
170 <p>
171 As an alternative to this method, you can configure this
172 value using the <code class="filename">DB_CONFIG</code> file's
173 <code class="literal">set_lk_max_objects</code> parameter.
174 </p>
175 </li>
176 </ul>
177 </div>
178 <p>
179 For a definition of lockers, locks, and locked objects, see
180 <a class="xref" href="blocking_deadlocks.html#lockresources" title="Lock Resources">Lock Resources</a>.
181 </p>
182 <p>
183 For example, to configure the number of locks that your
184 environment can use:
185 </p>
186 <pre class="programlisting">package db.txn;
187
188 import com.sleepycat.db.DatabaseException;
189 import com.sleepycat.db.Environment;
190 import com.sleepycat.db.EnvironmentConfig;
191
192 import java.io.File;
193 import java.io.FileNotFoundException;
194
195 ...
196
197 Environment myEnv = null;
198 try {
199 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
200 myEnvConfig.setTransactional(true);
201 myEnvConfig.setMaxLocks(5000);
202
203 myEnv = new Environment(new File("/my/env/home"),
204 myEnvConfig);
205
206 } catch (DatabaseException de) {
207 // Exception handling goes here
208 } catch (FileNotFoundException fnfe) {
209 // Exception handling goes here
210 }</pre>
211 </div>
212 <div class="sect2" lang="en" xml:lang="en">
213 <div class="titlepage">
214 <div>
215 <div>
216 <h3 class="title"><a id="configdeadlkdetect"></a>Configuring Deadlock Detection</h3>
217 </div>
218 </div>
219 </div>
220 <p>
221 In order for DB to know that a deadlock has occurred,
222 some mechanism must be used to perform deadlock
223 detection. There are three ways that deadlock detection can
224 occur:
225 </p>
226 <div class="orderedlist">
227 <ol type="1">
228 <li>
229 <p>
230 Allow DB to internally detect deadlocks as they
231 occur.
232 </p>
233 <p>
234 To do this, you use
235
236
237 <span><code class="methodname">EnvironmentConfig.setLockDetectMode()</code>.</span>
238 This method causes DB to walk its internal lock table
239 looking for a deadlock whenever a lock request
240 is blocked. This method also identifies how DB decides which lock
241 requests are rejected when deadlocks are detected. For example,
242 DB can decide to reject the lock request for the transaction
243 that has the most number of locks, the least number of locks,
244 holds the oldest lock, holds the most number of write locks, and
245 so forth (see the API reference documentation for a complete
246 list of the lock detection policies).
247 </p>
248 <p>
249 You can call this method at any time during your application's
250 lifetime, but typically it is used before you open your environment.
251 </p>
252 <p>
253 Note that how you want DB to decide which thread of control should break a deadlock is
254 extremely dependent on the nature of your application. It is not unusual for some performance
255 testing to be required in order to make this determination. That said, a transaction that is
256 holding the most number of locks is usually indicative of the transaction that has performed
257 the most amount of work. Frequently you will not want a transaction that has performed a lot of
258 work to abandon its efforts and start all over again. It is not therefore uncommon for
259 application developers to initially select the transaction with the <span class="emphasis"><em>minimum</em></span>
260 number of write locks to break the deadlock.
261 </p>
262 <p>
263 Using this mechanism for deadlock detection means
264 that your application will never have to wait on a
265 lock before discovering that a deadlock has
266 occurred. However, walking the lock table every
267 time a lock request is blocked can be expensive
268 from a performance perspective.
269 </p>
270 </li>
271 <li>
272 <p>
273 Use a dedicated thread or external process to perform
274 deadlock detection. Note that this thread must be
275 performing no other database operations beyond deadlock
276 detection.
277 </p>
278 <p>
279 To externally perform lock detection, you can use
280 either the
281
282
283 <code class="methodname">Environment.detectDeadlocks()</code>
284 method, or use the
285 <span class="command"><strong>db_deadlock</strong></span> command line
286 utility. This method (or command) causes DB to walk the
287 lock table looking for deadlocks.
288 </p>
289 <p>
290 Note that like
291
292
293 <span><code class="methodname">EnvironmentConfig.setLockDetectMode()</code>,</span>
294 you also use this method (or command line utility)
295 to identify which lock requests are rejected in the
296 event that a deadlock is detected.
297 </p>
298 <p>
299 Applications that perform deadlock detection in
300 this way typically run deadlock detection between every few
301 seconds and a minute. This means that your
302 application may have to wait to be notified of a
303 deadlock, but you also save the overhead of walking
304 the lock table every time a lock request is blocked.
305 </p>
306 </li>
307 <li>
308 <p>
309 Lock timeouts.
310 </p>
311 <p>
312 You can configure your locking subsystem such that
313 it times out any lock that is not released within a
314 specified amount of time. To do this, use the
315
316
317 <span><code class="methodname">EnvironmentConfig.setLockTimeout()</code></span>
318 method.
319 Note that lock timeouts are only checked when a
320 lock request is blocked or when deadlock
321 detection is otherwise performed. Therefore, a lock can have timed out and still be held for
322 some length of time until DB has a reason to examine its locking tables.
323 </p>
324 <p>
325 Be aware that extremely long-lived transactions, or
326 operations that hold locks for a long time, may be
327 inappropriately timed out before the transaction or
328 operation has a chance to complete. You should
329 therefore use this mechanism only if you know your
330 application will hold locks for very short periods
331 of time.
332 </p>
333 </li>
334 </ol>
335 </div>
336 <p>
337 For example, to configure your application such that DB
338 checks the lock table for deadlocks every time a lock
339 request is blocked:
340 </p>
341 <pre class="programlisting">package db.txn;
342
343 import com.sleepycat.db.Environment;
344 import com.sleepycat.db.EnvironmentConfig;
345 import com.sleepycat.db.LockDetectMode;
346
347 import java.io.File;
348
349 ...
350
351 Environment myEnv = null;
352 try {
353 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
354 myEnvConfig.setTransactional(true);
355 myEnvConfig.setInitializeCache(true);
356 myEnvConfig.setInitializeLocking(true);
357 myEnvConfig.setInitializeLogging(true);
358
359 // Configure db to perform deadlock detection internally, and to
360 // choose the transaction that has performed the least amount
361 // of writing to break the deadlock in the event that one
362 // is detected.
363 envConfig.setLockDetectMode(LockDetectMode.MINWRITE);
364
365 myEnv = new Environment(new File("/my/env/home"),
366 myEnvConfig);
367
368 // From here, you open your databases, proceed with your
369 // database operations, and respond to deadlocks as
370 // is normal (omitted for brevity).
371
372 ...</pre>
373 <p>
374 Finally, the following command line call causes
375 deadlock detection to be run against the
376 environment contained in <code class="literal">/export/dbenv</code>. The
377 transaction with the youngest lock is chosen to break the
378 deadlock:
379 </p>
380 <pre class="programlisting">&gt; /usr/local/db_install/bin/db_deadlock -h /export/dbenv -a y</pre>
381 <p>
382 For more information, see the
383 <a class="ulink" href="http://download.oracle.com/docs/cd/E17076_02/html/api_reference/C/db_deadlock.html" target="_top">
384 <code class="literal">db_deadlock</code> reference documentation.
385 </a>
386 </p>
387 </div>
388 <div class="sect2" lang="en" xml:lang="en">
389 <div class="titlepage">
390 <div>
391 <div>
392 <h3 class="title"><a id="deadlockresolve"></a>Resolving Deadlocks</h3>
393 </div>
394 </div>
395 </div>
396 <p>
397 When DB determines that a deadlock has occurred, it will
398 select a thread of control to resolve the deadlock and then
399
400
401 <span>
402 throws <code class="literal">DeadlockException</code> in that
403 thread.
404 </span>
405
406 If a deadlock is detected, the thread must:
407 </p>
408 <div class="orderedlist">
409 <ol type="1">
410 <li>
411 <p>
412 Cease all read and write operations.
413 </p>
414 </li>
415 <li>
416 <p>
417 Close all open cursors.
418 </p>
419 </li>
420 <li>
421 <p>
422 Abort the transaction.
423 </p>
424 </li>
425 <li>
426 <p>
427 Optionally retry the operation. If your application
428 retries deadlocked operations, the new attempt must
429 be made using a new transaction.
430 </p>
431 </li>
432 </ol>
433 </div>
434 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
435 <h3 class="title">Note</h3>
436 <p>
437 If a thread has deadlocked, it may not make any
438 additional database calls using the handle that has
439 deadlocked.
440 </p>
441 </div>
442 <p>
443 For example:
444 </p>
445 <pre class="programlisting">// retry_count is a counter used to identify how many times
446 // we've retried this operation. To avoid the potential for
447 // endless looping, we won't retry more than MAX_DEADLOCK_RETRIES
448 // times.
449
450 // txn is a transaction handle.
451 // key and data are DatabaseEntry handles. Their usage is not shown here.
452 while (retry_count &lt; MAX_DEADLOCK_RETRIES) {
453 try {
454 txn = myEnv.beginTransaction(null, null);
455 myDatabase.put(txn, key, data);
456 txn.commit();
457 return 0;
458 } catch (DeadlockException de) {
459 try {
460 // Abort the transaction and increment the
461 // retry counter
462 txn.abort();
463 retry_count++;
464 if (retry_count &gt;= MAX_DEADLOCK_RETRIES) {
465 System.err.println("Exceeded retry limit. Giving up.");
466 return -1;
467 }
468 } catch (DatabaseException ae) {
469 System.err.println("txn abort failed: " + ae.toString());
470 return -1;
471 }
472 } catch (DatabaseException e) {
473 try {
474 // Abort the transaction.
475 txn.abort();
476 } catch (DatabaseException ae) {
477 System.err.println("txn abort failed: " + ae.toString());
478 return -1;
479 }
480 }
481 } </pre>
482 </div>
483 <div class="sect2" lang="en" xml:lang="en">
484 <div class="titlepage">
485 <div>
486 <div>
487 <h3 class="title"><a id="setpriority"></a>Setting Transaction Priorities</h3>
488 </div>
489 </div>
490 </div>
491 <p>
492 Normally when a thread of control must be selected to
493 resolve a deadlock, DB decides which thread will
494 perform the resolution; you have no way of knowing in
495 advance which thread will be selected to resolve the
496 deadlock.
497 </p>
498 <p>
499 However, there may be situations where you know it is
500 better for one thread to resolve a deadlock over another
501 thread. As an example, if you have a background thread
502 running data management activities, and another thread
503 responding to user requests, you might want deadlock
504 resolution to occur in the background thread because you
505 can better afford the throughput costs there. Under these
506 circumstances, you can identify which thread of control
507 will be selected for resolved deadlocks by setting a
508 transaction priorities.
509 </p>
510 <p>
511 When two transactions are deadlocked, DB will abort the
512 transaction with the lowest priority. By default, every
513 transaction is given a priority of 100. However, you can
514 set a different priority on a transaction-by-transaction
515 basis by using the
516
517
518 <code class="methodname">Transaction.setPriority()</code>
519 method.
520 </p>
521 <p>
522 When two or more transactions are tied for the lowest
523 priority, the tie is broken based on the policy provided to
524
525 <span>
526 the <code class="classname">LockDetectMode</code> class.
527 You provide this configuration object to the
528 environment using the
529 <code class="methodname">EnvironmentConfig.setLockDetectMode()</code>
530 method.
531 </span>
532 </p>
533 <p>
534 A transaction's priority can be changed at any time after
535 the transaction handle has been created and before the
536 transaction has been resolved (committed or aborted).
537 For example:
538 </p>
539 <pre class="programlisting">...
540
541 try {
542
543 ...
544
545 Transaction txn = myEnv.beginTransaction(null, null);
546 <strong class="userinput"><code>txn.setPriority(200);</code></strong>
547
548 try {
549 myDatabase.put(txn, key, data);
550 txn.commit();
551 } catch (Exception e) {
552 if (txn != null) {
553 txn.abort();
554 txn = null;
555 }
556 }
557
558 ...
559
560 } </pre>
561 </div>
562 </div>
563 <div class="navfooter">
564 <hr />
565 <table width="100%" summary="Navigation footer">
566 <tr>
567 <td width="40%" align="left"><a accesskey="p" href="blocking_deadlocks.html">Prev</a> </td>
568 <td width="20%" align="center">
569 <a accesskey="u" href="txnconcurrency.html">Up</a>
570 </td>
571 <td width="40%" align="right"> <a accesskey="n" href="isolation.html">Next</a></td>
572 </tr>
573 <tr>
574 <td width="40%" align="left" valign="top">Locks, Blocks, and Deadlocks </td>
575 <td width="20%" align="center">
576 <a accesskey="h" href="index.html">Home</a>
577 </td>
578 <td width="40%" align="right" valign="top"> Isolation</td>
579 </tr>
580 </table>
581 </div>
582 </body>
583 </html>