Skip to content

Commit 96adb6b

Browse files
committed
Running the add_snippets tool to insert snippets
1 parent bd8a016 commit 96adb6b

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

google-cloud-datastore/src/main/java/com/google/cloud/datastore/Transaction.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,75 @@ interface Response {
105105
/**
106106
* Commit the transaction.
107107
*
108+
* <p>Example of committing a transaction
109+
* <pre> {@code
110+
* // create an entity
111+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind");
112+
* Key key = datastore.allocateId(keyFactory.newKey());
113+
* Entity entity = Entity.builder(key).set("description", "calling commit()").build();
114+
*
115+
* // add the entity and commit
116+
* Transaction txn = datastore.newTransaction();
117+
* try {
118+
* txn.put(entity);
119+
* txn.commit();
120+
* } catch (DatastoreException ex) {
121+
* // handle exception
122+
* }
123+
* }</pre>
124+
*
108125
* @throws DatastoreException if could not commit the transaction or if no longer active
109126
*/
110127
Response commit();
111128

112129
/**
113130
* Rollback the transaction.
114131
*
132+
* <p>Example of rolling back a Transaction
133+
* <pre> {@code
134+
* // create an entity
135+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind");
136+
* Key key = datastore.allocateId(keyFactory.newKey());
137+
* Entity entity = Entity.builder(key).set("description", "calling active()").build();
138+
*
139+
* // add the entity and rollback
140+
* Transaction txn = datastore.newTransaction();
141+
* txn.put(entity);
142+
* txn.rollback();
143+
* // calling txn.commit() now would fail
144+
* }</pre>
145+
*
115146
* @throws DatastoreException if transaction was already committed
116147
*/
117148
void rollback();
118149

119150
/**
120151
* Returns {@code true} if the transaction is still active (was not committed or rolledback).
152+
*
153+
* <p>Example of verifying if a Transaction is active
154+
* <pre> {@code
155+
* // create an entity
156+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind");
157+
* Key key = datastore.allocateId(keyFactory.newKey());
158+
* Entity entity = Entity.builder(key).set("description", "calling active()").build();
159+
*
160+
* // create a transaction
161+
* Transaction txn = datastore.newTransaction();
162+
* // calling txn.active() now would return true
163+
* try {
164+
* // add the entity and commit
165+
* txn.put(entity);
166+
* txn.commit();
167+
* } finally {
168+
* // if committing succeeded
169+
* // then txn.active() will be false
170+
* if (txn.active()) {
171+
* // otherwise it's true and we need to rollback
172+
* txn.rollback();
173+
* }
174+
* }
175+
* }</pre>
176+
*
121177
*/
122178
@Override
123179
boolean active();

0 commit comments

Comments
 (0)