Database.com Java SDK

Transactions

A transaction is a set of operations that either succeeds or fails completely. This allows recovery from error conditions to a known state and avoids cleanup necessary from a partially successful operation where some records are persisted, while others are not persisted due to errors.

The Database.com JPA provider supports transactions with a read-committed isolation level. This means that data modified in a transaction is not visible to a query until commit() is called on the transaction.

Before using transactions in your code, you must configure transaction properties in your application's persistence.xml file. See Transaction Properties.

In JPA, a transaction is started by calling the begin() method and committed by calling commit(). The following snippet of code assumes that you've already established an EntityManager object, em, and populated a new student record.

// Instantiate a transaction
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(student);
tx.commit();

This code snippet is intended to show the basic transaction syntax. If you are using Spring, you can use the @Transactional annotation on a method instead to delineate a transaction for the method.

If you are deleting a record, you should execute find() and remove() in the same transaction.

Note: The Database.com JPA provider doesn't do anything when flush() is called on an EntityManager. Use the @Transactional annotation in Spring or commit() instead for transactions.