Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.skife.jdbi.v2.tweak.transactions;
  
A TransactionHandler that automatically retries transactions that fail due to serialization failures, which can generally be resolved by automatically retrying the transaction. Any TransactionCallback used under this runner should be aware that it may be invoked multiple times.
 
 {
     /* http://www.postgresql.org/docs/9.1/static/errcodes-appendix.html */
     private static final String SQLSTATE_TXN_SERIALIZATION_FAILED = "40001";
 
     private final Configuration configuration;
 
     {
         this(new Configuration(), new LocalTransactionHandler());
     }
 
     public SerializableTransactionRunner(Configuration configurationTransactionHandler delegate)
     {
         super(delegate);
         this. = configuration;
     }
 
     @Override
     public <ReturnType> ReturnType inTransaction(Handle handleTransactionCallback<ReturnType> callback)
     {
         int retriesRemaining = .;
 
         while (true) {
             try
             {
                 return getDelegate().inTransaction(handlecallback);
             } catch (Exception e)
             {
                 if (!isSqlState(.e) || --retriesRemaining <= 0)
                 {
 
                     if (e instanceof RuntimeException)
                     {
                         throw (RuntimeExceptione;
                     }
                     throw new TransactionFailedException(e);
                 }
             }
         }
     }
 
     @Override
     public <ReturnType> ReturnType inTransaction(Handle handleTransactionIsolationLevel level,
             TransactionCallback<ReturnType> callback)
     {
         final TransactionIsolationLevel initial = handle.getTransactionIsolationLevel();
         try
         {
             handle.setTransactionIsolation(level);
             return inTransaction(handlecallback);
         }
         finally
         {
             handle.setTransactionIsolation(initial);
         }
     }

    
Returns true iff the Throwable or one of its causes is an SQLException whose SQLState begins with the passed state.
 
     protected boolean isSqlState(String expectedSqlStateThrowable throwable)
     {
         do
         {
             if (throwable instanceof SQLException)
             {
                 String sqlState = ((SQLExceptionthrowable).getSQLState();
 
                 if (sqlState != null && sqlState.startsWith(expectedSqlState))
                 {
                     return true;
                 }
             }
         } while ( (throwable = throwable.getCause()) != null);
 
         return false;
     }
 
     public static class Configuration
     {
         private final int maxRetries;
         private final String serializationFailureSqlState;
        public Configuration()
        {
            this(5, );
        }
        private Configuration(int maxRetriesString serializationFailureSqlState)
        {
            this. = maxRetries;
            this. = serializationFailureSqlState;
        }
        public Configuration withMaxRetries(int maxRetries)
        {
            return new Configuration(maxRetries);
        }
        public Configuration withSerializationFailureSqlState(String serializationFailureSqlState)
        {
            return new Configuration(serializationFailureSqlState);
        }
    }
New to GrepCode? Check out our FAQ X