Skip Headers

# Acquiring a Unit of Work

Previous

Next

This example shows how to acquire a unit of work from a client session object.

    Server server =
        (Server) SessionManager.getManager().getSession(
            sessionName, MyServerSession.class.getClassLoader()
        );
    Session session = (Session) server.acquireClientSession();
    UnitOfWork uow = session.acquireUnitOfWork();

You can acquire a unit of work from any session type. For more information about acquiring sessions at run time, see "Acquiring a Session at Run Time With the Session Manager".

Note that you do not need to create a new session and log in before every transaction. The recommended pattern is to acquire a client session per client access (or thread), and then acquire the necessary unit of work from this client session.

The unit of work is valid until the `commit` or `release` method is called. After a commit or release transaction, a unit of work is not valid even if the transaction fails and is rolled back.

A unit of work remains valid after the `commitAndResume` method is called as described in "Resuming a Unit of Work After Commit".

When using a unit of work with JTA, you can also use the advanced API `getActiveUnitOfWork` method as described in "Integrating the Unit of Work With an External Transaction Service".