Skip Headers

# Nested Unit of Work

Previous

Next

To nest units of work, call the `acquireUnitOfWork` method on the parent unit of work. This creates a child unit of work with its own cache. If a child unit of work commits, it updates the parent unit of work rather than the database. If the parent does not commit, the changes made to the child are not written to the database.

TopLink does not update the database or the cache until the outermost unit of work is committed. You must commit or release the child unit of work before you can commit its parent.

Working copy clones from one unit of work are not valid in another units of work: not even between an inner and outer unit of work. You must register objects at all levels of a unit of work where they are used.

Example: Using Nested Units of Work shows how to use nested units of work.

Using Nested Units of Work

    UnitOfWork outerUOW = session.acquireUnitOfWork();
        Pet outerPetClone = (Pet)outerUOW.readObject(Pet.class);

        UnitOfWork innerUOWa = outerUOW.acquireUnitOfWork();
            Pet innerPetCloneA =
                (Pet)innerUOWa.registerObject(outerPetClone);
            innerPetCloneA.setName("Muffy");
        innerUOWa.commit();

        UnitOfWork innerUOWb = outerUOW.acquireUnitOfWork();
            Pet innerPetCloneB =
                (Pet)innerUOWb.registerObject(outerPetClone);
            innerPetCloneB.setName("Duffy");
        innerUOWb.commit();
    outerUOW.commit();