Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010-2011. 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.unitofwork;
 
Default entry point to gain access to the current UnitOfWork. Components managing transactional boundaries can register and clear UnitOfWork instances, which components can use.

Author(s):
Allard Buijze
Since:
0.6
 
 public abstract class CurrentUnitOfWork {
 
     private static final ThreadLocal<Deque<UnitOfWork>> CURRENT = new ThreadLocal<Deque<UnitOfWork>>();
 
     private CurrentUnitOfWork() {
     }

    
Indicates whether a unit of work has already been started. This method can be used by interceptors to prevent nesting of UnitOfWork instances.

Returns:
whether a UnitOfWork has already been started.
 
     public static boolean isStarted() {
         return .get() != null && !.get().isEmpty();
     }

    
Gets the UnitOfWork bound to the current thread. If no UnitOfWork has been started, an java.lang.IllegalStateException is thrown.

To verify whether a UnitOfWork is already active, use isStarted().

Returns:
The UnitOfWork bound to the current thread.
Throws:
java.lang.IllegalStateException if no UnitOfWork is active
 
     public static UnitOfWork get() {
         if (isEmpty()) {
             throw new IllegalStateException("No UnitOfWork is currently started for this thread.");
         }
         return .get().peek();
     }
 
     private static boolean isEmpty() {
         Deque<UnitOfWorkunitsOfWork = .get();
         return unitsOfWork == null || unitsOfWork.isEmpty();
     }

    
Commits the current UnitOfWork. If no UnitOfWork was started, an java.lang.IllegalStateException is thrown.

Throws:
java.lang.IllegalStateException if no UnitOfWork is currently started.
See also:
UnitOfWork.commit()
 
     public static void commit() {
         get().commit();
     }

    
Binds the given unitOfWork to the current thread. If other UnitOfWork instances were bound, they will be marked as inactive until the given UnitOfWork is cleared.

Parameters:
unitOfWork The UnitOfWork to bind to the current thread.
 
     public static void set(UnitOfWork unitOfWork) {
         if (.get() == null) {
             .set(new LinkedList<UnitOfWork>());
         }
         .get().push(unitOfWork);
     }

    
Clears the UnitOfWork currently bound to the current thread, if that UnitOfWork is the given unitOfWork. Otherwise, nothing happens.

Parameters:
unitOfWork The UnitOfWork expected to be bound to the current thread.
Throws:
java.lang.IllegalStateException when the given UnitOfWork was not the current active UnitOfWork. This exception indicates a potentially wrong nesting of Units Of Work.
 
     public static void clear(UnitOfWork unitOfWork) {
        if (isStarted() && .get().peek() == unitOfWork) {
            .get().pop();
            if (.get().isEmpty()) {
                .remove();
            }
        } else {
            throw new IllegalStateException("Could not clear this UnitOfWork. It is not the active one.");
        }
    }
New to GrepCode? Check out our FAQ X