FileDocCategorySizeDatePackage
SynchronizationManager.javaAPI DocGlassfish v2 API8242Fri May 04 22:34:56 BST 2007com.sun.jdo.api.persistence.support

SynchronizationManager

public class SynchronizationManager extends Object implements Synchronization
This class allows for multiple instances to be called at transaction completion, which JDO does not currently provide. JDO only provides for a single instance to be registered. This service exploits the JDO capability by registering an instance of SynchronizationManager with JDO and then calling each instance registered with itself.
author
Craig Russell
version
1.0

Fields Summary
protected static int
defaultCapacity
The default capacity of the List of Synchronizations.
protected final List
synchronizations
The list of instances to synchronize. Duplicate registrations will result in the instance being called multiple times. Since we cannot depend on the caller implementing hashCode and equals, we cannot use a Set implementaion.
Constructors Summary
public SynchronizationManager(int initialCapacity)
Creates new SynchronizationManager instance specifying the initial capacity of the list of Synchronization instances.

param
initialCapacity the initial capacity of the List of Synchronization instances

        synchronizations = new ArrayList(initialCapacity);
    
public SynchronizationManager()
Creates new SynchronizationManager instance with a default capacity of the List of Synchronization instances.

        this(defaultCapacity);
    
protected SynchronizationManager(PersistenceManager pm)
Creates new SynchronizationManager instance and registers it with the persistence manager.

param
pm the persistence manager managing this transaction

    
                            
       
        this();
        Transaction tx = pm.currentTransaction();
        tx.setSynchronization((Synchronization)this);
    
Methods Summary
public voidafterCompletion(int status)
This method will be called during transaction completion. No resource access is allowed. This method in turn calls each registered instance afterCompletion method. After this method completes, instances must register again in the new transaction, but the synchronization manager remains bound to the persistence manager transaction instance.

param
status the completion status of the transaction

        int size = synchronizations.size();
        StringBuffer sb = null;
        for (int i = 0; i < size; ++i) {
            Synchronization instance = (Synchronization) synchronizations.get(i);
            try {
                instance.afterCompletion(status);
            } catch (Exception e) {
                if (sb == null) {
                    sb = new StringBuffer();
                }
                sb.append(e.getMessage()).append('\n"); // NOI18N
            }
        }
        synchronizations.clear();
        if (sb != null) {
            throw new JDOUserException(sb.toString());
        }
    
public voidbeforeCompletion()
This method will be called during transaction completion. Resource access is allowed. This method in turn calls each registered instance beforeCompletion method.

        int size = synchronizations.size();
        for (int i = 0; i < size; ++i) {
            Synchronization instance = (Synchronization) synchronizations.get(i);
            instance.beforeCompletion();
        }
    
protected static com.sun.jdo.api.persistence.support.SynchronizationManagergetSynchronizationManager(PersistenceManager pm)
Get the synchronization manager already registered with this persistence manager. If the synchronization instance is not of the proper class, then replace it with a new instance of the synchronization manager, and register the previous synchronization with the newly created synchronization manager.

param
pm the persistence manager
return
the synchronization manager

        Transaction tx = pm.currentTransaction();
        Synchronization oldsync = tx.getSynchronization();
        if (oldsync instanceof SynchronizationManager) {
            // This is the one we want.
            return (SynchronizationManager) oldsync;
        } else {
            // We need a new one.  The constructor automatically registers it 
            // with the persistence manager.
            SynchronizationManager newsync = new SynchronizationManager(pm);
            if (oldsync != null) {
                // There is an existing Synchronization to register with the new one
                newsync.registerSynchronization(oldsync);
            }
            return newsync;
        }
    
public static voidregisterSynchronization(javax.transaction.Synchronization instance, PersistenceManager pm)
Register a new Synchronization with the current transaction.

param
instance the instance to be registered
param
pm the persistence manager which manages this transaction

        SynchronizationManager synchronizationManager = getSynchronizationManager(pm);
        synchronizationManager.registerSynchronization(instance);
    
protected voidregisterSynchronization(javax.transaction.Synchronization instance)
Register an instance with this synchronization manager. Note that this is not thread-safe. If multiple threads call this method at the same time, the synchronizations List might become corrupt. The correct way to fix this is to ask the PersistenceManager for the Multithreaded flag and perform a synchronized add if the flag is true. We currently do not have the Multithreaded flag implemented.

param
instance the instance to be registered

        synchronizations.add(instance);
    
public static voidsetDefaultCapacity(int capacity)
Specify the default capacity of the list of Synchronizations.

param
capacity the default capacity of the List of Synchronizations

        defaultCapacity = capacity;