2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2002, 2012 Oracle and/or its affiliates. All rights reserved.
9 package com.sleepycat.db;
11 import com.sleepycat.db.internal.DbConstants;
12 import com.sleepycat.db.internal.DbEnv;
13 import com.sleepycat.db.internal.DbTxn;
16 Specifies the attributes of a database environment transaction.
18 public class TransactionConfig implements Cloneable {
20 * For internal use, to allow null as a valid value for
21 * the config parameter.
24 Default configuration used if null is passed to methods that create a
27 public static final TransactionConfig DEFAULT = new TransactionConfig();
30 static TransactionConfig checkNull(TransactionConfig config) {
31 return (config == null) ? DEFAULT : config;
34 private boolean bulk = false;
35 private boolean readUncommitted = false;
36 private boolean readCommitted = false;
37 private boolean noSync = false;
38 private boolean noWait = false;
39 private boolean snapshot = false;
40 private boolean sync = false;
41 private boolean writeNoSync = false;
42 private boolean wait = false;
45 An instance created using the default constructor is initialized
46 with the system's default settings.
48 public TransactionConfig() {
52 Configure the transaction for read committed isolation.
54 This ensures the stability of the current data item read by the
55 cursor but permits data read by this transaction to be modified or
56 deleted prior to the commit of the transaction.
59 If true, configure the transaction for read committed isolation.
61 public void setReadCommitted(final boolean readCommitted) {
62 this.readCommitted = readCommitted;
66 Return if the transaction is configured for read committed isolation.
69 If the transaction is configured for read committed isolation.
71 public boolean getReadCommitted() {
76 Configure the transaction for read committed isolation.
78 This ensures the stability of the current data item read by the
79 cursor but permits data read by this transaction to be modified or
80 deleted prior to the commit of the transaction.
83 If true, configure the transaction for read committed isolation.
85 @deprecated This has been replaced by {@link #setReadCommitted} to conform to ANSI
86 database isolation terminology.
88 public void setDegree2(final boolean degree2) {
89 setReadCommitted(degree2);
93 Return if the transaction is configured for read committed isolation.
96 If the transaction is configured for read committed isolation.
98 @deprecated This has been replaced by {@link #getReadCommitted} to conform to ANSI
99 database isolation terminology.
101 public boolean getDegree2() {
102 return getReadCommitted();
106 Configure read operations performed by the transaction to return modified
107 but not yet committed data.
109 @param readUncommitted
110 If true, configure read operations performed by the transaction to return
111 modified but not yet committed data.
113 public void setReadUncommitted(final boolean readUncommitted) {
114 this.readUncommitted = readUncommitted;
118 Return if read operations performed by the transaction are configured to
119 return modified but not yet committed data.
122 If read operations performed by the transaction are configured to return
123 modified but not yet committed data.
125 public boolean getReadUncommitted() {
126 return readUncommitted;
130 Configure read operations performed by the transaction to return modified
131 but not yet committed data.
134 If true, configure read operations performed by the transaction to return
135 modified but not yet committed data.
137 @deprecated This has been replaced by {@link #setReadUncommitted} to conform to ANSI
138 database isolation terminology.
140 public void setDirtyRead(final boolean dirtyRead) {
141 setReadUncommitted(dirtyRead);
145 Return if read operations performed by the transaction are configured to
146 return modified but not yet committed data.
149 If read operations performed by the transaction are configured to return
150 modified but not yet committed data.
152 @deprecated This has been replaced by {@link #getReadUncommitted} to conform to ANSI
153 database isolation terminology.
155 public boolean getDirtyRead() {
156 return getReadUncommitted();
160 Configure the transaction to not write or synchronously flush the log
163 This behavior may be set for a database environment using the
164 Environment.setMutableConfig method. Any value specified to this method
165 overrides that setting.
167 The default is false for this class and the database environment.
170 If true, transactions exhibit the ACI (atomicity, consistency, and
171 isolation) properties, but not D (durability); that is, database
172 integrity will be maintained, but if the application or system
173 fails, it is possible some number of the most recently committed
174 transactions may be undone during recovery. The number of
175 transactions at risk is governed by how many log updates can fit
176 into the log buffer, how often the operating system flushes dirty
177 buffers to disk, and how often the log is checkpointed.
179 public void setNoSync(final boolean noSync) {
180 this.noSync = noSync;
184 Return if the transaction is configured to not write or synchronously
185 flush the log it when commits.
188 If the transaction is configured to not write or synchronously flush
189 the log it when commits.
191 public boolean getNoSync() {
196 Configure the transaction to not wait if a lock request cannot be
199 The default is false for this class and the database environment.
202 If true, transactions will not wait if a lock request cannot be
203 immediately granted, instead {@link com.sleepycat.db.DeadlockException DeadlockException} will be thrown.
205 public void setNoWait(final boolean noWait) {
206 this.noWait = noWait;
210 Return if the transaction is configured to not wait if a lock
211 request cannot be immediately granted.
214 If the transaction is configured to not wait if a lock request
215 cannot be immediately granted.
217 public boolean getNoWait() {
222 This transaction will execute with snapshot isolation. For databases
223 configured with {@link DatabaseConfig#setMultiversion}, data values
224 will be read as they are when the transaction begins, without taking
227 Updates operations performed in the transaction will cause a
228 {@link DeadlockException} to be thrown if data is modified
229 between reading and writing it.
231 public void setSnapshot(final boolean snapshot) {
232 this.snapshot = snapshot;
236 Return true if the transaction is configured for Snapshot Isolation.
238 This method may be called at any time during the life of the application.
241 True if the transaction is configured for Snapshot Isolation.
243 public boolean getSnapshot() {
248 Configure the transaction to write and synchronously flush the log
251 This behavior may be set for a database environment using the
252 Environment.setMutableConfig method. Any value specified to this
253 method overrides that setting.
255 The default is false for this class and true for the database
258 If true is passed to both setSync and setNoSync, setSync will take
262 If true, transactions exhibit all the ACID (atomicity, consistency,
263 isolation, and durability) properties.
265 public void setSync(final boolean sync) {
270 Return if the transaction is configured to write and synchronously
271 flush the log it when commits.
274 If the transaction is configured to write and synchronously flush
275 the log it when commits.
277 public boolean getSync() {
282 Configure the transaction to wait if a lock request cannot be
285 The default is true unless {@link EnvironmentConfig#setTxnNoWait} is called.
288 If true, transactions will wait if a lock request cannot be
289 immediately granted, instead {@link com.sleepycat.db.DeadlockException DeadlockException} will be thrown.
291 public void setWait(final boolean wait) {
296 Return if the transaction is configured to wait if a lock
297 request cannot be immediately granted.
300 If the transaction is configured to wait if a lock request
301 cannot be immediately granted.
303 public boolean getWait() {
308 Configure the transaction to write but not synchronously flush the log
311 This behavior may be set for a database environment using the
312 Environment.setMutableConfig method. Any value specified to this method
313 overrides that setting.
315 The default is false for this class and the database environment.
318 If true, transactions exhibit the ACI (atomicity, consistency, and
319 isolation) properties, but not D (durability); that is, database
320 integrity will be maintained, but if the operating system
321 fails, it is possible some number of the most recently committed
322 transactions may be undone during recovery. The number of
323 transactions at risk is governed by how often the operating system
324 flushes dirty buffers to disk, and how often the log is
327 public void setWriteNoSync(final boolean writeNoSync) {
328 this.writeNoSync = writeNoSync;
332 Return if the transaction is configured to write but not synchronously
333 flush the log it when commits.
336 If the transaction is configured to not write or synchronously flush
337 the log it when commits.
339 public boolean getWriteNoSync() {
344 Configures the transaction to enable the transactional bulk insert
345 optimization. When this attribute is set, the transaction will avoid
346 logging the contents of insertions on newly allocated database pages.
347 In a transaction that inserts a large number of new records, the I/O
348 savings of choosing this option can be significant. Users of this
349 option should be aware of several issues. When the optimization is in
350 effect, page allocations that extend the database file are logged as
351 usual; this allows transaction aborts to work correctly, both online
352 and during recovery. At commit time, the database's pages are flushed
353 to disk, eliminating the need to roll-forward the transaction during
354 normal recovery. However, there are other recovery operations that
355 depend on roll-forward, and care must be taken when Bulk-enabled
356 transactions interact with them. In particular, Bulk is
357 incompatible with replication, and is simply ignored when replication
358 is enabled. Also, hot backup procedures must follow a particular
359 protocol, introduced in 11gr2.5.1, to set a flag in the environment
360 before starting to copy files. It is especially important to note
361 that incremental hot backups can be invalidated by use of the bulk
362 insert optimization. Please see the hot backup description in the
363 <i>Getting Started with Transactions Guide</i>, and the description of the
364 HotbackupInProgress attribute in
365 {@link com.sleepycat.db.EnvironmentConfig EnvironmentConfig}
366 for further information.
368 The bulk insert optimization is effective only for
369 top-level transactions.
372 If true, configure the transaction to enable the bulk optimization.
374 public void setBulk(final boolean bulk) {
379 Return true if the Bulk attribute is set.
382 The current setting of the Bulk attribute.
384 public boolean getBulk() {
388 DbTxn beginTransaction(final DbEnv dbenv, final DbTxn parent)
389 throws DatabaseException {
392 flags |= readCommitted ? DbConstants.DB_READ_COMMITTED : 0;
393 flags |= readUncommitted ? DbConstants.DB_READ_UNCOMMITTED : 0;
394 flags |= noSync ? DbConstants.DB_TXN_NOSYNC : 0;
395 flags |= noWait ? DbConstants.DB_TXN_NOWAIT : 0;
396 flags |= snapshot ? DbConstants.DB_TXN_SNAPSHOT : 0;
397 flags |= sync ? DbConstants.DB_TXN_SYNC : 0;
398 flags |= wait ? DbConstants.DB_TXN_WAIT : 0;
399 flags |= writeNoSync ? DbConstants.DB_TXN_WRITE_NOSYNC : 0;
400 flags |= bulk ? DbConstants.DB_TXN_BULK : 0;
402 return dbenv.txn_begin(parent, flags);