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
|
<?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>Base API In-Memory Transaction Example</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="wrapup.html" title="Chapter 6. Summary and Examples" />
<link rel="prev" href="txnexample_dpl.html" title="DPL Transaction Example" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 12.1.6.1</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Base API In-Memory Transaction Example</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="txnexample_dpl.html">Prev</a> </td>
<th width="60%" align="center">Chapter 6. Summary and Examples</th>
<td width="20%" align="right"> </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="inmem_txnexample_java"></a>Base API In-Memory Transaction Example</h2>
</div>
</div>
</div>
<p>
DB is sometimes used for applications that simply need to cache
data retrieved from some other location (such as a remote database
server). DB is also often used in embedded systems.
</p>
<p>
In both cases, applications may still want to use transactions for
atomicity, consistency, and isolation guarantees, but they may want
to forgo the durability guarantee entirely. That is, they may want
their DB environment and databases kept entirely in-memory so
as to avoid the performance impact of unneeded disk I/O.
</p>
<p>
To do this:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
Refrain from specifying a home directory when you open your
environment. The exception to this is if you are using the
<code class="literal">DB_CONFIG</code> configuration file — in
that case you must identify the environment's home
directory so that the configuration file can be found.
</p>
</li>
<li>
<p>
Configure your environment to back your regions from
system memory instead of the filesystem.
</p>
</li>
<li>
<p>
Configure your logging subsystem such that log files are kept
entirely in-memory.
</p>
</li>
<li>
<p>
Increase the size of your in-memory log buffer so that it
is large enough to hold the largest set of concurrent write operations.
</p>
</li>
<li>
<p>
Increase the size of your in-memory cache so that it can
hold your entire data set. You do not want your cache to
page to disk.
</p>
</li>
<li>
<p>
Do not specify a file name when you open your database(s).
</p>
</li>
</ul>
</div>
<p>
As an example, this section takes the transaction example provided
in <a class="xref" href="txnexample_java.html" title="Base API Transaction Example">Base API Transaction Example</a>
and it updates that example so that the environment, database, log
files, and regions are all kept entirely in-memory.
</p>
<p>
For illustration purposes, we also modify this example so that
uncommitted reads are no longer used to enable the <code class="methodname">countRecords()</code>
method. Instead, we simply provide a transaction handle to
<code class="methodname">countRecords()</code> so as to avoid the
self-deadlock.
</p>
<p>
The majority of the modifications to the original example are performed in the <code class="classname">TxnGuide</code>
example class (see <a class="xref" href="txnexample_java.html#txnguideexample" title="TxnGuide.java">TxnGuide.java</a>).
This is because the majority of the work that we need to do is performed when the environment and
databases are opened.
</p>
<p>
To begin, we simplify the beginning of the class a bit. We eliminate some variables that the example no longer
needs — specifically variables having to do with the location of the environment and the names of the
database files.
We can also remove our <code class="function">usage()</code> method because we no
longer require any command line arguments.
</p>
<pre class="programlisting">// File TxnGuideInMemory.java
package db.txn;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.DatabaseType;
import com.sleepycat.db.LockDetectMode;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import java.io.File;
import java.io.FileNotFoundException;
<strong class="userinput"><code>public class TxnGuideInMemory {</code></strong>
// DB handles
private static Database myDb = null;
private static Database myClassDb = null;
private static Environment myEnv = null;
private static final int NUMTHREADS = 5; </pre>
<p>
Next, in our <code class="function">main()</code> method, we
remove the call to <code class="methodname">parseArgs()</code> because that only existed in the previous example for
collecting the environment home location. Everything else is essentially the same.
</p>
<pre class="programlisting"> public static void main(String args[]) {
try {
// Open the environment and databases
openEnv();
// Get our class catalog (used to serialize objects)
StoredClassCatalog classCatalog =
new StoredClassCatalog(myClassDb);
// Start the threads
DBWriter[] threadArray;
threadArray = new DBWriter[NUMTHREADS];
for (int i = 0; i < NUMTHREADS; i++) {
threadArray[i] = new DBWriter(myEnv, myDb, classCatalog);
threadArray[i].start();
}
for (int i = 0; i < NUMTHREADS; i++) {
threadArray[i].join();
}
} catch (Exception e) {
System.err.println("<strong class="userinput"><code>TxnGuideInMemory</code></strong>: " + e.toString());
e.printStackTrace();
} finally {
closeEnv();
}
System.out.println("All done.");
} </pre>
<p>
Next we open our environment as always. However, in doing so we:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
Set <code class="methodname">EnvironmentConfig.setPrivate()</code>
to <code class="literal">true</code>.
This causes our environment to back regions using our
application's heap memory rather than by using the filesystem.
This is the first important step to keeping our DB data
entirely in-memory.
</p>
</li>
<li>
<p>
Remove <code class="methodname">runRecovery()</code>
from the environment configuration. Because all our data will be held entirely in memory, recovery is a
non-issue. Note that if we had left the call to <code class="methodname">runRecovery()</code>
in, it would be silently ignored.
</p>
</li>
</ul>
</div>
<pre class="programlisting"> private static void openEnv() throws DatabaseException {
System.out.println("opening env");
// Set up the environment.
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
<strong class="userinput"><code>// Region files are not backed by the filesystem, they are
// backed by heap memory.
myEnvConfig.setPrivate(true);</code></strong>
myEnvConfig.setAllowCreate(true);
myEnvConfig.setInitializeCache(true);
myEnvConfig.setInitializeLocking(true);
myEnvConfig.setInitializeLogging(true);
myEnvConfig.setTransactional(true);
// EnvironmentConfig.setThreaded(true) is the default behavior
// in Java, so we do not have to do anything to cause the
// environment handle to be free-threaded.
// Indicate that we want db to internally perform deadlock
// detection. Also indicate that the transaction that has
// performed the least amount of write activity to
// receive the deadlock notification, if any.
myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE); </pre>
<p>
Now we configure our environment to keep the log files in memory,
increase the log buffer size to 10 MB, and increase our in-memory
cache to 10 MB. These values should be more than enough for our
application's workload.
</p>
<pre class="programlisting">
<strong class="userinput">
<code> // Specify in-memory logging
myEnvConfig.setLogInMemory(true);
// Specify the size of the in-memory log buffer
// Must be large enough to handle the log data created by
// the largest transaction.
myEnvConfig.setLogBufferSize(10 * 1024 * 1024);
// Specify the size of the in-memory cache
// Set it large enough so that it won't page.
myEnvConfig.setCacheSize(10 * 1024 * 1024); </code>
</strong>
</pre>
<p>
Our database configuration is identical to the original example, except that we do not specify
<code class="methodname">setReadUncomitted()</code> here. We will be causing our <code class="methodname">countRecords()</code>
method to join the transaction rather than perform uncommitted reads, so we do not need our database to support them.
</p>
<pre class="programlisting"> // Set up the database
DatabaseConfig myDbConfig = new DatabaseConfig();
myDbConfig.setType(DatabaseType.BTREE);
myDbConfig.setAllowCreate(true);
myDbConfig.setTransactional(true);
myDbConfig.setSortedDuplicates(true);
// no DatabaseConfig.setThreaded() method available.
// db handles in java are free-threaded so long as the
// env is also free-threaded. </pre>
<p>
Next, we open the environment. This is
identical to how the example previously worked, except that we do not
provide a location for the environment's home directory.
</p>
<pre class="programlisting"> try {
// Open the environment
myEnv = new Environment(<strong class="userinput"><code>null</code></strong>, // Env home
myEnvConfig); </pre>
<p>
When we open our databases, we also specify <code class="literal">null</code> for the file names. The causes the database
to not be backed by the filesystem; that is, the databases are held entirely in memory.
</p>
<pre class="programlisting"> // Open the database. Do not provide a txn handle. This open
// is auto committed because DatabaseConfig.setTransactional()
// is true.
myDb = myEnv.openDatabase(null, // txn handle
<strong class="userinput"><code>null</code></strong>, // Database file name
null, // Database name
myDbConfig);
// Used by the bind API for serializing objects
// Class database must not support duplicates
myDbConfig.setSortedDuplicates(false);
myClassDb = myEnv.openDatabase(null, // txn handle
<strong class="userinput"><code>null</code></strong>, // Database file name
null, // Database name,
myDbConfig);
} catch (FileNotFoundException fnfe) {
System.err.println("openEnv: " + fnfe.toString());
System.exit(-1);
}
} </pre>
<p>
After that, our class is unchanged, except for some very minor modifications.
Most notably, we remove the <code class="methodname">parseArgs()</code>
method from the application, because we no longer need it.
</p>
<pre class="programlisting"> private static void closeEnv() {
System.out.println("Closing env");
if (myDb != null ) {
try {
myDb.close();
} catch (DatabaseException e) {
System.err.println("closeEnv: myDb: " +
e.toString());
e.printStackTrace();
}
}
if (myClassDb != null ) {
try {
myClassDb.close();
} catch (DatabaseException e) {
System.err.println("closeEnv: myClassDb: " +
e.toString());
e.printStackTrace();
}
}
if (myEnv != null ) {
try {
myEnv.close();
} catch (DatabaseException e) {
System.err.println("closeEnv: " + e.toString());
e.printStackTrace();
}
}
}
<strong class="userinput"><code>private TxnGuideInMemory() {}</code></strong>
} </pre>
<p>
That completes our modifications to this class.
We now turn our attention to our <code class="classname">DBWriter</code>
class (see <a class="xref" href="txnexample_java.html#dbwriter" title="DBWriter.java">DBWriter.java</a>).
It is unchanged, except for one small modification. In the
<code class="methodname">run()</code> method, we call <code class="methodname">countRecords()</code>
with a transaction handle, rather than configuring our entire
application for uncommitted reads. Both mechanisms work well-enough
for preventing a self-deadlock. However, the individual count
in this example will tend to be lower than the counts seen in
the previous transaction example, because
<code class="function">countRecords()</code> can no longer see records
created but not yet committed by other threads.
Additionally, the usage of the transaction handle here will
probably cause more deadlocks than using read-uncommitted does, because more locking is being performed in
this case.
</p>
<pre class="programlisting">package db.txn;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.tuple.StringBinding;
import com.sleepycat.db.Cursor;
import com.sleepycat.db.CursorConfig;
import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.DeadlockException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.LockMode;
import com.sleepycat.db.OperationStatus;
import com.sleepycat.db.Transaction;
import java.io.UnsupportedEncodingException;
import java.util.Random;
public class DBWriter extends Thread
{
private Database myDb = null;
private Environment myEnv = null;
private EntryBinding dataBinding = null;
private Random generator = new Random();
private static final int MAX_RETRY = 20;
private static String[] keys = {"key 1", "key 2", "key 3",
"key 4", "key 5", "key 6",
"key 7", "key 8", "key 9",
"key 10"};
// Constructor. Get our DB handles from here
DBWriter(Environment env, Database db, StoredClassCatalog scc)
throws DatabaseException {
myDb = db;
myEnv = env;
dataBinding = new SerialBinding(scc, PayloadData.class);
}
// Thread method that writes a series of records
// to the database using transaction protection.
// Deadlock handling is demonstrated here.
public void run () {
Transaction txn = null;
// Perform 50 transactions
for (int i=0; i<50; i++) {
boolean retry = true;
int retry_count = 0;
// while loop is used for deadlock retries
while (retry) {
// try block used for deadlock detection and
// general db exception handling
try {
// Get a transaction
txn = myEnv.beginTransaction(null, null);
// Write 10 records to the db
// for each transaction
for (int j = 0; j < 10; j++) {
// Get the key
DatabaseEntry key = new DatabaseEntry();
StringBinding.stringToEntry(keys[j], key);
// Get the data
PayloadData pd = new PayloadData(i+j, getName(),
generator.nextDouble());
DatabaseEntry data = new DatabaseEntry();
dataBinding.objectToEntry(pd, data);
// Do the put
myDb.put(txn, key, data);
}
// commit
System.out.println(getName() +
" : committing txn : " + i);
System.out.println(getName() + " : Found " +
countRecords(<strong class="userinput"><code>txn</code></strong>) + " records in the database.");
try {
txn.commit();
txn = null;
} catch (DatabaseException e) {
System.err.println("Error on txn commit: " +
e.toString());
}
retry = false;
} catch (DeadlockException de) {
System.out.println("################# " + getName() +
" : caught deadlock");
// retry if necessary
if (retry_count < MAX_RETRY) {
System.err.println(getName() +
" : Retrying operation.");
retry = true;
retry_count++;
} else {
System.err.println(getName() +
" : out of retries. Giving up.");
retry = false;
}
} catch (DatabaseException e) {
// abort and don't retry
retry = false;
System.err.println(getName() +
" : caught exception: " + e.toString());
System.err.println(getName() +
" : errno: " + e.getErrno());
e.printStackTrace();
} finally {
if (txn != null) {
try {
txn.abort();
} catch (Exception e) {
System.err.println(
"Error aborting transaction: " +
e.toString());
e.printStackTrace();
}
}
}
}
}
} </pre>
<p>
Next we update <code class="methodname">countRecords()</code>. The only difference
here is that we no longer specify <code class="methodname">CursorConfig.setReadUncomitted()</code> when
we open our cursor. Note that even this minor change is not required.
If we do not configure our database to support uncommitted reads,
<code class="methodname">CursorConfig.setReadUncomitted()</code> is silently
ignored. However, we remove the property anyway from the cursor open so as to
avoid confusion.
</p>
<pre class="programlisting"> // This simply counts the number of records contained in the
// database and returns the result. You can use this method
// in three ways:
//
// First call it with an active txn handle.
// Secondly, configure the cursor for uncommitted reads
// Third, call count_records AFTER the writer has committed
// its transaction.
//
// If you do none of these things, the writer thread will
// self-deadlock.
//
// Note that this method exists only for illustrative purposes.
// A more straight-forward way to count the number of records in
// a database is to use the Database.getStats() method.
private int countRecords(Transaction txn) throws DatabaseException {
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
int count = 0;
Cursor cursor = null;
try {
// Get the cursor
CursorConfig cc = new CursorConfig();
cc.setReadUncomitted(true);
cursor = myDb.openCursor(txn, cc);
while (cursor.getNext(key, data, LockMode.DEFAULT) ==
OperationStatus.SUCCESS) {
count++;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return count;
}
} </pre>
<p>
This completes our in-memory transactional example. If you would like to
experiment with this code, you can find the example in the following
location in your DB distribution:
</p>
<pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples/java/src/db/txn</pre>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="txnexample_dpl.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="wrapup.html">Up</a>
</td>
<td width="40%" align="right"> </td>
</tr>
<tr>
<td width="40%" align="left" valign="top">DPL Transaction Example </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> </td>
</tr>
</table>
</div>
</body>
</html>
|