3.10.4.1. Using Savepoints
OpenJPA’s OpenJPAEntityManager have the following methods to control savepoint behavior. Note that the savepoints work in tandem with the current transaction. This means that savepoints require an open transaction, and that a rollback of the transaction will rollback all of the changes in the transaction regardless of any savepoints set.
void setSavepoint(String name);
void releaseSavepoint(String name);
void rollbackToSavepoint(String name);
To set a savepoint, simply call setSavepoint, passing in a symbolic savepoint name.
This savepoint will define a point at which you can preserve the state of transactional objects for the duration of the current transaction.
Having set a named savepoint, you can rollback changes made after that point by calling rollbackToSavepoint.
This method will keep the current transaction active, while restoring all transactional instances back to their saved state.
Instances that were deleted after the save point will no longer be marked for deletion.
Similarly, transient instances that were made persistent after the savepoint will become transient again.
Savepoints made after this savepoint will be released and no longer valid, although you can still set new savepoints.
Savepoints will also be cleared after the current transaction is committed or rolled back.
If a savepoint is no longer needed, you can release any resources it is consuming resources by calling releaseSavepoint.
This method should not be called for savepoints that have been released automatically through other means, such as commit of a transaction or rollback to a prior savepoint.
While savepoints made after this savepoint will also be released, there are no other effects on the current transaction.
The following simple example illustrates setting, releasing, and rolling back to a savepoint.
import org.apache.openjpa.persistence.*;
...
OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
oem.getTransaction().begin();
Magazine mag = oem.find(Magazine.class, id);
mag.setPageCount(300);
oem.setSavepoint("pages");
mag.setPrice(mag.getPageCount() * pricePerPage);
// we decide to release "pages"...
oem.releaseSavepoint("pages");
// ... and set a new savepoint which includes all changes
oem.setSavepoint("price");
mag.setPrice(testPrice);
// we determine the test price is not good
oem.rollbackToSavepoint("price");
// had we chosen to not release "pages", we might have rolled back to
// "pages" instead
// the price is now restored to mag.getPageCount() * pricePerPage
oem.getTransaction().commit();