Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2004 - 2011 Brian McCallister
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.skife.jdbi.v2.tweak.transactions;
 
 
Handler designed to behave properly in a J2EE CMT environment. It will never explicitely begin or commit a transaction, and will throw a runtime exception when rollback is called to force rollback.
 
 public class CMTTransactionHandler implements TransactionHandler
 {
    
Called when a transaction is started
 
     public void begin(Handle handle)
     {
         // noop
     }

    
Called when a transaction is committed
 
     public void commit(Handle handle)
     {
         // noop
     }

    
Called when a transaction is rolled back Will throw a RuntimeException to force transactional rollback
 
     public void rollback(Handle handle)
     {
         throw new TransactionException("Rollback called, this runtime exception thrown to halt the transaction");
     }

    
Roll back to a named checkpoint

Parameters:
handle the handle the rollback is being performed on
name the name of the checkpoint to rollback to
 
     public void rollback(Handle handleString name)
     {
         throw new UnsupportedOperationException("Checkpoints not implemented");
     }

    
Called to test if a handle is in a transaction
 
     public boolean isInTransaction(Handle handle)
     {
         try
         {
             return ! handle.getConnection().getAutoCommit();
         }
         catch (SQLException e)
         {
             throw new TransactionException("Failed to check status of transaction"e);
         }
     }

    
Create a new checkpoint (savepoint in JDBC terminology)

Parameters:
handle the handle on which the transaction is being checkpointed
name The name of the chckpoint, used to rollback to or release late
 
     public void checkpoint(Handle handleString name)
     {
         throw new UnsupportedOperationException("Checkpoints not implemented");
     }

    
Release a previously created checkpoint

Parameters:
handle the handle on which the checkpoint is being released
checkpointName the checkpoint to release
    public void release(Handle handleString checkpointName)
    {
        throw new TransactionException("Rollback called, this runtime exception thrown to halt the transaction");
    }
    private class ExplodingTransactionStatus implements TransactionStatus
    {
        private final Handle handle;
        ExplodingTransactionStatus(Handle handle)
        {
            this. = handle;
        }
        @Override
        public void setRollbackOnly()
        {
            rollback();
        }
    }
    @Override
    public <ReturnType> ReturnType inTransaction(final Handle handleTransactionCallback<ReturnType> callback)
    {
        try
        {
            return callback.inTransaction(handlenew ExplodingTransactionStatus(handle));
        } catch (Exception e)
        {
            if (e instanceof RuntimeException)
            {
                throw (RuntimeExceptione;
            }
            throw new TransactionException(e);
        }
    }
    @Override
    public <ReturnType> ReturnType inTransaction(Handle handleTransactionIsolationLevel level,
            TransactionCallback<ReturnType> callback)
    {
        return inTransaction(handlecallback);
    }
New to GrepCode? Check out our FAQ X