File: lockingsubsystem.html

package info (click to toggle)
db5.3 5.3.28%2Bdfsg1-0.5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 158,360 kB
  • sloc: ansic: 448,411; java: 111,824; tcl: 80,544; sh: 44,326; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,077; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (583 lines) | stat: -rw-r--r-- 24,411 bytes parent folder | download | duplicates (8)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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/html; charset=UTF-8" />
    <title>The Locking Subsystem</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
    <link rel="start" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
    <link rel="up" href="txnconcurrency.html" title="Chapter 4. Concurrency" />
    <link rel="prev" href="blocking_deadlocks.html" title="Locks, Blocks, and Deadlocks" />
    <link rel="next" href="isolation.html" title="Isolation" />
  </head>
  <body>
    <div xmlns="" class="navheader">
      <div class="libver">
        <p>Library Version 11.2.5.3</p>
      </div>
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">The Locking Subsystem</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="blocking_deadlocks.html">Prev</a> </td>
          <th width="60%" align="center">Chapter 4. Concurrency</th>
          <td width="20%" align="right"> <a accesskey="n" href="isolation.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="sect1" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a id="lockingsubsystem"></a>The Locking Subsystem</h2>
          </div>
        </div>
      </div>
      <div class="toc">
        <dl>
          <dt>
            <span class="sect2">
              <a href="lockingsubsystem.html#configuringlock">Configuring the Locking Subsystem</a>
            </span>
          </dt>
          <dt>
            <span class="sect2">
              <a href="lockingsubsystem.html#configdeadlkdetect">Configuring Deadlock Detection</a>
            </span>
          </dt>
          <dt>
            <span class="sect2">
              <a href="lockingsubsystem.html#deadlockresolve">Resolving Deadlocks</a>
            </span>
          </dt>
          <dt>
            <span class="sect2">
              <a href="lockingsubsystem.html#setpriority">Setting Transaction Priorities</a>
            </span>
          </dt>
        </dl>
      </div>
      <p>
            In order to allow concurrent operations, DB provides the locking
            subsystem. This subsystem provides inter- and intra- process
            concurrency mechanisms. It is extensively used by DB concurrent
            applications, but it can also be generally used for non-DB
            resources.
        </p>
      <p>
            This section describes the locking subsystem as it is used to
            protect DB resources. In particular, issues on configuration are
            examined here. For information on using the locking subsystem to
            manage non-DB resources, see the
            <em class="citetitle">Berkeley DB Programmer's Reference Guide</em>.
        </p>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="configuringlock"></a>Configuring the Locking Subsystem</h3>
            </div>
          </div>
        </div>
        <p>
                You initialize the locking subsystem by specifying
                    
                    <span>
                        <code class="literal">true</code> to the 
                            <code class="methodname">EnvironmentConfig.setInitializeLocking()</code>
                        method.
                    </span>
            </p>
        <p>
                Before opening your environment, you can configure various
                values for your locking subsystem. Note that these
                limits can only be configured before the environment is
                opened. Also, these methods configure the entire environment,
                not just a specific environment handle.
            </p>
        <p>
                Finally, each bullet below identifies the 
                <code class="filename">DB_CONFIG</code> file parameter that can be used
                to specify the specific locking limit. If used, these
                <code class="filename">DB_CONFIG</code> file parameters override any
                value that you might specify using the environment handle. 
            </p>
        <p>
                The limits that you can configure are as follows:
            </p>
        <div class="itemizedlist">
          <ul type="disc">
            <li>
              <p>
                        The number of lockers supported by the environment.
                        This value is used by the environment when it is
                        opened to estimate the amount of space that it
                        should allocate for various internal data
                        structures. By default, 1,000 lockers are
                        supported.
                    </p>
              <p>
                        To configure this value, use the 
                        
                        
                        <span>
                            <code class="methodname">EnvironmentConfig.setMaxLockers()</code>
                         method.
                        </span>
                    </p>
              <p>
                        As an alternative to this method, you can configure this
                        value using the <code class="filename">DB_CONFIG</code> file's
                        <code class="literal">set_lk_max_lockers</code> parameter.
                    </p>
            </li>
            <li>
              <p>
                        The number of locks supported by the environment.
                        By default, 1,000 locks are supported.
                    </p>
              <p>
                        To configure this value, use the 
                        
                        <span>
                            <code class="methodname">EnvironmentConfig.setMaxLocks()</code>
                         method.
                        </span>
                    </p>
              <p>
                        As an alternative to this method, you can configure this
                        value using the <code class="filename">DB_CONFIG</code> file's
                        <code class="literal">set_lk_max_locks</code> parameter.
                    </p>
            </li>
            <li>
              <p>
                        The number of locked objects supported by the
                        environment.  By default, 1,000 objects can be
                        locked.
                    </p>
              <p>
                        To configure this value, use the 
                        
                        <span>
                            <code class="methodname">EnvironmentConfig.setMaxLockObjects()</code>
                         method.
                        </span>
                    </p>
              <p>
                        As an alternative to this method, you can configure this
                        value using the <code class="filename">DB_CONFIG</code> file's
                        <code class="literal">set_lk_max_objects</code> parameter.
                    </p>
            </li>
          </ul>
        </div>
        <p>
                For a definition of lockers, locks, and locked objects, see
                <a class="xref" href="blocking_deadlocks.html#lockresources" title="Lock Resources">Lock Resources</a>.
            </p>
        <p>
                For example, to configure the number of locks that your
                environment can use:
            </p>
        <pre class="programlisting">package db.txn;

import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;

import java.io.File;
import java.io.FileNotFoundException;

...

Environment myEnv = null;
try {
    EnvironmentConfig myEnvConfig = new EnvironmentConfig();
    myEnvConfig.setTransactional(true);
    myEnvConfig.setMaxLocks(5000);

    myEnv = new Environment(new File("/my/env/home"),
                              myEnvConfig);

} catch (DatabaseException de) {
    // Exception handling goes here
} catch (FileNotFoundException fnfe) {
     // Exception handling goes here
}</pre>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="configdeadlkdetect"></a>Configuring Deadlock Detection</h3>
            </div>
          </div>
        </div>
        <p>
                In order for DB to know that a deadlock has occurred,
                some mechanism must be used to perform deadlock
                detection. There are three ways that deadlock detection can
                occur:
            </p>
        <div class="orderedlist">
          <ol type="1">
            <li>
              <p>
                        Allow DB to internally detect deadlocks as they
                        occur.
                    </p>
              <p>
                        To do this, you use
                            
                            
                            <span><code class="methodname">EnvironmentConfig.setLockDetectMode()</code>.</span>
                            This method causes DB to walk its internal lock table 
                            looking for a deadlock whenever a lock request
                            is blocked. This method also identifies how DB decides which lock
                            requests are rejected when deadlocks are detected. For example,
                            DB can decide to reject the lock request for the transaction
                            that has the most number of locks, the least number of locks,
                            holds the oldest lock, holds the most number of write locks, and
                            so forth (see the API reference documentation for a complete
                            list of the lock detection policies).
                    </p>
              <p>
                        You can call this method at any time during your application's
                        lifetime, but typically it is used before you open your environment.
                    </p>
              <p>
                        Note that how you want DB to decide which thread of control should break a deadlock is
                        extremely dependent on the nature of your application. It is not unusual for some performance
                        testing to be required in order to make this determination. That said, a transaction that is
                        holding the most number of locks is usually indicative of the transaction that has performed
                        the most amount of work. Frequently you will not want a transaction that has performed a lot of
                        work to abandon its efforts and start all over again. It is not therefore uncommon for
                        application developers to initially select the transaction with the <span class="emphasis"><em>minimum</em></span>
                        number of write locks to break the deadlock.
                    </p>
              <p>
                        Using this mechanism for deadlock detection means
                        that your application will never have to wait on a
                        lock before discovering that a deadlock has
                        occurred. However, walking the lock table every
                        time a lock request is blocked can be expensive
                        from a performance perspective.
                    </p>
            </li>
            <li>
              <p>
                        Use a dedicated thread or external process to perform
                        deadlock detection. Note that this thread must be
                        performing no other database operations beyond deadlock
                        detection.
                    </p>
              <p>
                        To externally perform lock detection, you can use
                        either the
                            
                            
                            <code class="methodname">Environment.detectDeadlocks()</code>
                           method, or use the
                           <span class="command"><strong>db_deadlock</strong></span> command line
                           utility. This method (or command) causes DB to walk the
                           lock table looking for deadlocks. 
                    </p>
              <p>
                        Note that like
                            
                            
                            <span><code class="methodname">EnvironmentConfig.setLockDetectMode()</code>,</span>
                        you also use this method (or command line utility)
                        to identify which lock requests are rejected in the
                        event that a deadlock is detected.
                    </p>
              <p>
                        Applications that perform deadlock detection in
                        this way typically run deadlock detection between every few
                        seconds and a minute. This means that your
                        application may have to wait to be notified of a
                        deadlock, but you also save the overhead of walking
                        the lock table every time a lock request is blocked.
                    </p>
            </li>
            <li>
              <p>
                        Lock timeouts.
                    </p>
              <p>
                        You can configure your locking subsystem such that
                        it times out any lock that is not released within a
                        specified amount of time. To do this, use the
                            
                            
                            <span><code class="methodname">EnvironmentConfig.setLockTimeout()</code></span>
                        method. 
                            Note that lock timeouts are only checked when a
                            lock request is blocked or when deadlock
                            detection is otherwise performed. Therefore, a lock can have timed out and still be held for
                            some length of time until DB has a reason to examine its locking tables.
                    </p>
              <p>
                        Be aware that extremely long-lived transactions, or
                        operations that hold locks for a long time, may be
                        inappropriately timed out before the transaction or
                        operation has a chance to complete. You should
                        therefore use this mechanism only if you know your
                        application will hold locks for very short periods
                        of time.
                    </p>
            </li>
          </ol>
        </div>
        <p>
                For example, to configure your application such that DB
                checks the lock table for deadlocks every time a lock
                request is blocked:
            </p>
        <pre class="programlisting">package db.txn;

import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.db.LockDetectMode;

import java.io.File;

...

Environment myEnv = null;
try {
    EnvironmentConfig myEnvConfig = new EnvironmentConfig();
    myEnvConfig.setTransactional(true);
    myEnvConfig.setInitializeCache(true);
    myEnvConfig.setInitializeLocking(true);
    myEnvConfig.setInitializeLogging(true);   

    // Configure db to perform deadlock detection internally, and to
    // choose the transaction that has performed the least amount 
    // of writing to break the deadlock in the event that one 
    // is detected.
    envConfig.setLockDetectMode(LockDetectMode.MINWRITE);

    myEnv = new Environment(new File("/my/env/home"),
                              myEnvConfig);

    // From here, you open your databases, proceed with your 
    // database operations, and respond to deadlocks as 
    // is normal (omitted for brevity).

    ...</pre>
        <p>
        Finally, the following command line call causes
        deadlock detection to be run against the
        environment contained in <code class="literal">/export/dbenv</code>. The 
        transaction with the youngest lock is chosen to break the
        deadlock:
    </p>
        <pre class="programlisting">&gt; /usr/local/db_install/bin/db_deadlock -h /export/dbenv -a y</pre>
        <p>
        For more information, see the
        <a class="ulink" href="http://download.oracle.com/docs/cd/E17076_02/html/api_reference/C/db_deadlock.html" target="_top">
            <code class="literal">db_deadlock</code> reference documentation.
        </a>
    </p>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="deadlockresolve"></a>Resolving Deadlocks</h3>
            </div>
          </div>
        </div>
        <p>
                When DB determines that a deadlock has occurred, it will
                select a thread of control to resolve the deadlock and then 
                    
                    
                    <span>
                        throws <code class="literal">DeadlockException</code> in that
                        thread.
                    </span>
                    
                 If a deadlock is detected, the thread must:
            </p>
        <div class="orderedlist">
          <ol type="1">
            <li>
              <p>
                        Cease all read and write operations.
                    </p>
            </li>
            <li>
              <p>
                        Close all open cursors.
                    </p>
            </li>
            <li>
              <p>
                        Abort the transaction.
                    </p>
            </li>
            <li>
              <p>
                        Optionally retry the operation. If your application
                        retries deadlocked operations, the new attempt must
                        be made using a new transaction.
                    </p>
            </li>
          </ol>
        </div>
        <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
          <h3 class="title">Note</h3>
          <p>
                    If a thread has deadlocked, it may not make any
                    additional database calls using the handle that has
                    deadlocked.
                </p>
        </div>
        <p>
                For example:
            </p>
        <pre class="programlisting">// retry_count is a counter used to identify how many times
// we've retried this operation. To avoid the potential for 
// endless looping, we won't retry more than MAX_DEADLOCK_RETRIES 
// times.

// txn is a transaction handle.
// key and data are DatabaseEntry handles. Their usage is not shown here.
while (retry_count &lt; MAX_DEADLOCK_RETRIES) {
    try {
        txn = myEnv.beginTransaction(null, null);
        myDatabase.put(txn, key, data);
        txn.commit();
        return 0;
    } catch (DeadlockException de) {
        try {
            // Abort the transaction and increment the
            // retry counter
            txn.abort();
            retry_count++;
            if (retry_count &gt;= MAX_DEADLOCK_RETRIES) {
                System.err.println("Exceeded retry limit. Giving up.");
                return -1;
            }
        } catch (DatabaseException ae) {
            System.err.println("txn abort failed: " + ae.toString());
            return -1;    
        }
    } catch (DatabaseException e) {
        try {
            // Abort the transaction.
            txn.abort();
        } catch (DatabaseException ae) {
            System.err.println("txn abort failed: " + ae.toString());
            return -1;    
        }
    }
} </pre>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="setpriority"></a>Setting Transaction Priorities</h3>
            </div>
          </div>
        </div>
        <p>
                Normally when a thread of control must be selected to
                resolve a deadlock, DB decides which thread will
                perform the resolution; you have no way of knowing in
                advance which thread will be selected to resolve the
                deadlock.
            </p>
        <p>
                However, there may be situations where you know it is
                better for one thread to resolve a deadlock over another
                thread. As an example, if you have a background thread
                running data management activities, and another thread
                responding to user requests, you might want deadlock
                resolution to occur in the background thread because you
                can better afford the throughput costs there. Under these
                circumstances, you can identify which thread of control
                will be selected for resolved deadlocks by setting a
                transaction priorities.
            </p>
        <p>
                When two transactions are deadlocked, DB will abort the
                transaction with the lowest priority. By default, every
                transaction is given a priority of 100. However, you can
                set a different priority on a transaction-by-transaction
                basis by using the
                
                
                <code class="methodname">Transaction.setPriority()</code>
                method.
            </p>
        <p>
                When two or more transactions are tied for the lowest
                priority, the tie is broken based on the policy provided to
                
                <span>
                    the <code class="classname">LockDetectMode</code> class.
                    You provide this configuration object to the
                    environment using the
                    <code class="methodname">EnvironmentConfig.setLockDetectMode()</code>
                    method.
                </span>
            </p>
        <p>
                A transaction's priority can be changed at any time after
                the transaction handle has been created and before the
                transaction has been resolved (committed or aborted).
                For example:
            </p>
        <pre class="programlisting">...

try {

    ...

    Transaction txn = myEnv.beginTransaction(null, null);
    <strong class="userinput"><code>txn.setPriority(200);</code></strong>
        
    try {
        myDatabase.put(txn, key, data);
        txn.commit();
    } catch (Exception e) {
        if (txn != null) {
            txn.abort();
            txn = null;
        }
    }
    
    ...

} </pre>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="blocking_deadlocks.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="txnconcurrency.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="isolation.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">Locks, Blocks, and Deadlocks </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> Isolation</td>
        </tr>
      </table>
    </div>
  </body>
</html>