Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public Void run() {
*/
@Override
public <R> R transactNew(Work<R> work) {
return this.transactNew(Integer.MAX_VALUE, work);
return this.transactNew(Transactor.DEFAULT_TRY_LIMIT, work);
}

/* (non-Javadoc)
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/googlecode/objectify/impl/Transactor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
abstract public class Transactor<O extends Objectify>
{
// Limit default number of retries to something high but non-infinite
public static final int DEFAULT_TRY_LIMIT = 200;

/** Our session */
protected Session session;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public <R> R execute(ObjectifyImpl<O> parent, TxnType txnType, Work<R> work) {
*/
@Override
public <R> R transact(ObjectifyImpl<O> parent, Work<R> work) {
return this.transactNew(parent, Integer.MAX_VALUE, work);
return this.transactNew(parent, DEFAULT_TRY_LIMIT, work);
}

/* (non-Javadoc)
Expand All @@ -91,6 +91,7 @@ public <R> R transact(ObjectifyImpl<O> parent, Work<R> work) {
@Override
public <R> R transactNew(ObjectifyImpl<O> parent, int limitTries, Work<R> work) {
Preconditions.checkArgument(limitTries >= 1);
final int ORIGINAL_TRIES = limitTries;

while (true) {
try {
Expand All @@ -102,6 +103,12 @@ public <R> R transactNew(ObjectifyImpl<O> parent, int limitTries, Work<R> work)

if (log.isLoggable(Level.FINEST))
log.log(Level.FINEST, "Details of optimistic concurrency failure", ex);
try {
// Do increasing backoffs with randomness
Thread.sleep(Math.min(10000, (long) (0.5 * Math.random() + 0.5) * 200 * (ORIGINAL_TRIES - limitTries + 2)));
} catch (InterruptedException ignored) {
throw ex;
}
} else {
throw ex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public <R> R execute(ObjectifyImpl<O> parent, TxnType txnType, Work<R> work) {
throw new IllegalStateException("MANDATORY transaction but no transaction present");

case REQUIRES_NEW:
return transactNew(parent, Integer.MAX_VALUE, work);
return transactNew(parent, DEFAULT_TRY_LIMIT, work);

default:
throw new IllegalStateException("Impossible, some unknown txn type");
Expand Down