Transactions in Ebean

@Transactional

You must be using ENHANCEMENT for @Transactional to work. That means you must be enhancing the classes via IDE Plugin, Ant Task or javaagent. Refer to the user guide for more details on enhancement.

Ebean can enhance your pojos to add transactional support. To do so put the @Transaction annotation on a method or class. Ebean enhancement will then add the supporting transactional logic (begin transaction, commit, rollback, suspend and resuming transactions etc).

...
// any old pojo
public class MyService {

	@Transactional
	public void runFirst() throws IOException {

		// run in a Transaction (REQUIRED is the default)

		// find a customer
		Customer customer = Ebean.find(Customer.class, 1);

		// call another "transactional" method
		runInTrans();
	}

	@Transactional(type=TxType.REQUIRES_NEW)
	public void runInTrans() throws IOException {

		// run in its own NEW transaction 
		// ... suspend an existing transaction if required
		// ... and resume it when this method ends

		// find new orders
		List<Order> orders = Ebean.find(Order.class)
									.where().eq("status",OrderStatus.NEW)
									.findList();
		...

Put the @Transactional annotation on you methods and Ebean can enhance the classes adding the Transactional management.

Standard transaction scope types

This supports the standard propagation rules of REQUIRED (the default), REQUIRES_NEW, MANDATORY, SUPPORTS, NOT_SUPPORTS, NEVER. These are an exact match of the EJB TransactionAttributeTypes.

Nested Transactions

Transactional methods can be nested as in the above example where runFirst() calls runInTrans().

Isolation level and specific exception support

@Transactional (like Spring) supports isolation levels and explicit handling of Exceptions (to rollback or not for specific exceptions). Please refer to the User Guide for a more detailed explanation.

Interfaces

You can put @Transactional on interfaces and classes that implement those interfaces will get the transactional enhancement.

TxType Descriptions

TxTypeDescription
REQUIRED Runs in a Transaction. Will use an existing transaction if it exists, otherwise will create a new Transaction.
REQUIRES_NEW Runs in a new Transaction. If a current transaction exists it will be suspended.
MANDATORY Runs in the existing Transaction. If there is no current existing transaction an exception is thrown.
SUPPORTS Use a transaction if it already exists. If it does not then the method runs without a transaction.
NOT_SUPPORTS Always runs without a transaction. If one already exists then it is suspended.
NEVER Always runs without a transaction. If one already exists then it throws an exception.
Introduction User Guide (pdf) Install/Configure Public JavaDoc Whitepapers
General Database Specific Byte Code Deployment Annotations Features
Top Bugs Top Enhancements
woResponse