com.avaje.ebean.annotation
Annotation Type Transactional


@Target(value={METHOD,TYPE})
@Retention(value=RUNTIME)
public @interface Transactional

Specify transaction scoping for a method.

This is only supported if "Enhancement" is used via javaagent, ANT task or IDE enhancement plugin etc.

Note: Currently there are 3 known annotations that perform this role.

Spring created their one because the EJB annotation does not support features such as isolation level and specifying rollbackOn, noRollbackOn exceptions. This one exists for Ebean because I agree that the standard one is insufficient and don't want to include a dependency on Spring.

The default behaviour of EJB (and hence Spring) is to NOT ROLLBACK on checked exceptions. I find this very counter-intuitive. Ebean will provide a property to set the default behaviour to rollback on any exception and optionally change the setting to be consistent with EJB/Spring if people wish to do so.

 
  // a normal class
 public class MySimpleUserService {
 
  // this method is transactional automatically handling 
  // transaction begin, commit and rollback etc
  @Transactional
  public void runInTrans() throws IOException {
 
    // tasks performed within the transaction
    ...
    // find some objects
    Customer cust = Ebean.find(Customer.class, 1);
    
    Order order = ...;
    ...
    // save some objects
    Ebean.save(customer);
    Ebean.save(order);
  }
 

During development and testing you can set a debug level which will log the transaction begin, commit and rollback events so that you can easily confirm it is behaving as you would expect.

  ## in ebean.properties file
  
  ## Log transaction begins and ends etc
  ## (0=NoLogging 1=minimal ... 9=logAll)
  ebean.debug.transaction=3
  
 


Optional Element Summary
 TxIsolation isolation
          The transaction isolation level this transaction should have.
 Class<? extends Throwable>[] noRollbackFor
          The throwable's that will explicitly NOT cause a rollback to occur.
 boolean readOnly
          Set this to true if the transaction should be only contain queries.
 Class<? extends Throwable>[] rollbackFor
          The throwable's that will explicitly cause a rollback to occur.
 String serverName
          The name of the server that you want the transaction to be created from.
 TxType type
          The type of transaction scoping.
 

type

public abstract TxType type
The type of transaction scoping. Defaults to REQUIRED.

Default:
com.avaje.ebean.TxType.REQUIRED

isolation

public abstract TxIsolation isolation
The transaction isolation level this transaction should have.

This will only be used if this scope creates the transaction. If the transaction has already started then this will currently be ignored (you could argue that it should throw an exception).

Default:
com.avaje.ebean.TxIsolation.DEFAULT

readOnly

public abstract boolean readOnly
Set this to true if the transaction should be only contain queries.

Default:
false

serverName

public abstract String serverName
The name of the server that you want the transaction to be created from.

If left blank the 'default' server is used.

Default:
""

rollbackFor

public abstract Class<? extends Throwable>[] rollbackFor
The throwable's that will explicitly cause a rollback to occur.

Default:
{}

noRollbackFor

public abstract Class<? extends Throwable>[] noRollbackFor
The throwable's that will explicitly NOT cause a rollback to occur.

Default:
{}


Copyright © 2010. All Rights Reserved.