Transactions in Ebean

TxRunnable & TxCallable

TxRunnable & TxCallable are the programmatic equivalent to @Transactional.

You can mix @Transaction with TxRunnable and TxCallable if you like, they will behave correctly together.


public void myMethod() {
  ...
  System.out.println(" Some code in myMethod...");

  // run in Transactional scope... 
  Ebean.execute(new TxRunnable() {
	public void run() {
		
		// code running in "REQUIRED" transactional scope
		// ... as "REQUIRED" is the default TxType
		System.out.println(Ebean.currentTransaction());
		
		// find stuff...
		User user = Ebean.find(User.class, 1);
		...
		
		// save and delete stuff...
		Ebean.save(user);
		Ebean.delete(order);
		...
	}
  });

  System.out.println(" more code in myMethod...");
}

Generally you will use TxRunnable like the above as anonymous inner classes.

The code inside the run() will execute inside a transactional scope with Ebean handling the transaction propagation for you (just like @Transactional).


// programmatic control over the scope such as
// ... isolation level
// ... and to rollback or not for specific exceptions

TxScope txScope = TxScope
			.requiresNew()
			.setIsolation(TxIsolation.SERIALIZABLE)
			.setNoRollbackFor(IOException.class);
		
Ebean.execute(txScope, new TxRunnable() {
	public void run() {
		...
}
Introduction User Guide (pdf) Install/Configure Public JavaDoc Whitepapers
General Database Specific Byte Code Deployment Annotations Features
Top Bugs Top Enhancements
woResponse