Search in sources :

Example 1 with StateManagerImpl

use of org.datanucleus.state.StateManagerImpl in project motech by motech.

the class StateManagerUtil method setTransactionVersion.

/**
 * Sets the given transaction version to the instance state manager.
 *
 * @param instance the instance from which state manager will be retrieved
 * @param version the transaction version
 * @param versionFieldName the name of the version field
 */
public static void setTransactionVersion(Object instance, Object version, String versionFieldName) {
    try {
        StateManagerImpl stateManager = getStateManager(instance);
        stateManager.setVersion(version);
        AbstractClassMetaData cmd = stateManager.getClassMetaData();
        int fieldPosition = cmd.getAbsolutePositionOfMember(versionFieldName);
        boolean[] dirtyFields = getDirtyFields(stateManager);
        // we must mark version field as non dirty
        dirtyFields[fieldPosition] = false;
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new MdsException("Cannot set proper transaction version", e);
    }
}
Also used : MdsException(org.motechproject.mds.exception.MdsException) StateManagerImpl(org.datanucleus.state.StateManagerImpl) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 2 with StateManagerImpl

use of org.datanucleus.state.StateManagerImpl in project motech by motech.

the class StateManagerUtil method getStateManager.

private static StateManagerImpl getStateManager(Object instance) throws IllegalAccessException {
    java.lang.reflect.Field dnStateManagerField;
    Class clazz = instance.getClass();
    do {
        dnStateManagerField = getFieldSafe(clazz, "dnStateManager");
        clazz = clazz.getSuperclass();
    } while (clazz.getSuperclass() != null || dnStateManagerField == null);
    dnStateManagerField.setAccessible(true);
    StateManagerImpl stateManager = (StateManagerImpl) dnStateManagerField.get(instance);
    dnStateManagerField.setAccessible(false);
    return stateManager;
}
Also used : StateManagerImpl(org.datanucleus.state.StateManagerImpl)

Aggregations

StateManagerImpl (org.datanucleus.state.StateManagerImpl)2 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 MdsException (org.motechproject.mds.exception.MdsException)1