Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010. Axon Framework
   *
   * 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.axonframework.commandhandling.interceptors;
 
Abstract implementation of a org.axonframework.commandhandling.CommandHandlerInterceptor that starts a transaction and binds a org.axonframework.unitofwork.UnitOfWork to the current thread.

Upon successful execution of the command, the transaction is committed. If execution fails, the UnitOfWork and the transaction are rolled back.

Parameters:
<T> The type of object representing the transaction
Author(s):
Allard Buijze
Since:
0.6
 
 public abstract class TransactionInterceptor<T> implements CommandHandlerInterceptor {
 
     @Override
     public Object handle(Object commandUnitOfWork unitOfWorkInterceptorChain interceptorChainthrows Throwable {
         T transaction = startTransaction();
         CurrentUnitOfWork.get().registerListener(new TransactionalUnitOfWork(transaction));
         return interceptorChain.proceed();
     }

    
Start a new transaction for a command execution described by the given context. The given unitOfWork is the unitOfWork bound to the current thread.

Returns:
A reference to the current transaction
 
     protected abstract T startTransaction();

    
Commits the transaction for the command execution described by the given context. The given unitOfWork is the unitOfWork bound to the current thread.

Parameters:
transaction The transaction object returned during during startTransaction()
 
     protected abstract void commitTransaction(T transaction);

    
Rolls back a transaction for a command execution described by the given context. The given unitOfWork is the unitOfWork bound to the current thread.

Parameters:
transaction The transaction object returned during during startTransaction()
 
     protected abstract void rollbackTransaction(T transaction);
 
     private final class TransactionalUnitOfWork extends UnitOfWorkListenerAdapter {
 
         private final T transaction;

        
Creates an instance of the listener, tied to the given transaction.

Parameters:
transaction the transaction assigned to the Unit Of Work.
 
         private TransactionalUnitOfWork(T transaction) {
             this. = transaction;
         }

        
This method tries to roll back the transaction assigned to this Unit Of Work.

Parameters:
failureCause The cause of the rollback
 
         @Override
         public void onRollback(Throwable failureCause) {
             rollbackTransaction();
         }

        
This method commits the transaction assigned to this Unit Of Work.
 
         @Override
         public void afterCommit() {
             commitTransaction();
         }
     }
New to GrepCode? Check out our FAQ X