2 * Copyright 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.server.rest;
21 import java.util.concurrent.Callable;
23 import javax.ejb.EJBTransactionRolledbackException;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
29 * A helper class that provides a method for repeatedly trying a transaction
30 * that is rolled back due to optimistic locking exceptions.
34 public class TransactionHelper<T> {
38 private static Log logger = LogFactory.getLog(TransactionHelper.class);
41 * Number of times to retry a transaction that was rolled back due to
44 private static final int TRANSACTION_RETRIES = 10;
47 * Execute the supplied command until it completes, ignoring
48 * EJBTransactionRolledbackException. Try at least TRANSACTION_RETRIES
49 * times before giving up.
51 * @param command the command to execute
52 * @return the value returned by the command
53 * @throws Exception any other exception thrown by the command
55 public T tryExecute(Callable<T> command) throws Exception {
57 for (int i = 0; i < TRANSACTION_RETRIES; i++) {
59 returnValue = command.call();
61 } catch(EJBTransactionRolledbackException trbe) {
62 if (i == TRANSACTION_RETRIES - 1)
64 } catch (Exception e) {
67 logger.debug("Transaction retry: " + (i+1));