1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
8 #include "rocksdb/db.h"
9 #include "rocksdb/options.h"
10 #include "rocksdb/slice.h"
11 #include "rocksdb/utilities/transaction.h"
12 #include "rocksdb/utilities/transaction_db.h"
14 using namespace ROCKSDB_NAMESPACE
;
17 std::string kDBPath
= "C:\\Windows\\TEMP\\rocksdb_transaction_example";
19 std::string kDBPath
= "/tmp/rocksdb_transaction_example";
25 TransactionDBOptions txn_db_options
;
26 options
.create_if_missing
= true;
27 TransactionDB
* txn_db
;
29 Status s
= TransactionDB::Open(options
, txn_db_options
, kDBPath
, &txn_db
);
32 WriteOptions write_options
;
33 ReadOptions read_options
;
34 TransactionOptions txn_options
;
37 ////////////////////////////////////////////////////////
39 // Simple Transaction Example ("Read Committed")
41 ////////////////////////////////////////////////////////
43 // Start a transaction
44 Transaction
* txn
= txn_db
->BeginTransaction(write_options
);
47 // Read a key in this transaction
48 s
= txn
->Get(read_options
, "abc", &value
);
49 assert(s
.IsNotFound());
51 // Write a key in this transaction
52 s
= txn
->Put("abc", "def");
55 // Read a key OUTSIDE this transaction. Does not affect txn.
56 s
= txn_db
->Get(read_options
, "abc", &value
);
57 assert(s
.IsNotFound());
59 // Write a key OUTSIDE of this transaction.
60 // Does not affect txn since this is an unrelated key.
61 s
= txn_db
->Put(write_options
, "xyz", "zzz");
64 // Write a key OUTSIDE of this transaction.
65 // Fail because the key conflicts with the key written in txn.
66 s
= txn_db
->Put(write_options
, "abc", "def");
67 assert(s
.subcode() == Status::kLockTimeout
);
69 // Value for key "xyz" has been committed, can be read in txn.
70 s
= txn
->Get(read_options
, "xyz", &value
);
72 assert(value
== "zzz");
79 // Value is committed, can be read now.
80 s
= txn_db
->Get(read_options
, "abc", &value
);
82 assert(value
== "def");
84 ////////////////////////////////////////////////////////
86 // "Repeatable Read" (Snapshot Isolation) Example
87 // -- Using a single Snapshot
89 ////////////////////////////////////////////////////////
91 // Set a snapshot at start of transaction by setting set_snapshot=true
92 txn_options
.set_snapshot
= true;
93 txn
= txn_db
->BeginTransaction(write_options
, txn_options
);
95 const Snapshot
* snapshot
= txn
->GetSnapshot();
97 // Write a key OUTSIDE of transaction
98 s
= txn_db
->Put(write_options
, "abc", "xyz");
101 // Read the latest committed value.
102 s
= txn
->Get(read_options
, "abc", &value
);
104 assert(value
== "xyz");
106 // Read the snapshotted value.
107 read_options
.snapshot
= snapshot
;
108 s
= txn
->Get(read_options
, "abc", &value
);
110 assert(value
== "def");
112 // Attempt to read a key using the snapshot. This will fail since
113 // the previous write outside this txn conflicts with this read.
114 s
= txn
->GetForUpdate(read_options
, "abc", &value
);
119 // Snapshot will be released upon deleting the transaction.
121 // Clear snapshot from read options since it is no longer valid
122 read_options
.snapshot
= nullptr;
125 ////////////////////////////////////////////////////////
127 // "Read Committed" (Monotonic Atomic Views) Example
128 // --Using multiple Snapshots
130 ////////////////////////////////////////////////////////
132 // In this example, we set the snapshot multiple times. This is probably
133 // only necessary if you have very strict isolation requirements to
136 // Set a snapshot at start of transaction
137 txn_options
.set_snapshot
= true;
138 txn
= txn_db
->BeginTransaction(write_options
, txn_options
);
140 // Do some reads and writes to key "x"
141 read_options
.snapshot
= txn_db
->GetSnapshot();
142 s
= txn
->Get(read_options
, "x", &value
);
143 assert(s
.IsNotFound());
144 s
= txn
->Put("x", "x");
147 // Do a write outside of the transaction to key "y"
148 s
= txn_db
->Put(write_options
, "y", "y1");
151 // Set a new snapshot in the transaction
154 read_options
.snapshot
= txn_db
->GetSnapshot();
156 // Do some reads and writes to key "y"
157 // Since the snapshot was advanced, the write done outside of the
158 // transaction does not conflict.
159 s
= txn
->GetForUpdate(read_options
, "y", &value
);
161 assert(value
== "y1");
162 s
= txn
->Put("y", "y2");
165 // Decide we want to revert the last write from this transaction.
166 txn
->RollbackToSavePoint();
172 // Clear snapshot from read options since it is no longer valid
173 read_options
.snapshot
= nullptr;
175 // db state is at the save point.
176 s
= txn_db
->Get(read_options
, "x", &value
);
178 assert(value
== "x");
180 s
= txn_db
->Get(read_options
, "y", &value
);
182 assert(value
== "y1");
186 DestroyDB(kDBPath
, options
);
190 #endif // ROCKSDB_LITE