com.avaje.ebean
Interface Transaction


public interface Transaction

The Transaction object. Typically representing a JDBC or JTA transaction.


Field Summary
static int READ_COMMITTED
          Read Committed transaction isolation.
static int READ_UNCOMMITTED
          Read Uncommitted transaction isolation.
static int REPEATABLE_READ
          Repeatable read transaction isolation.
static int SERIALIZABLE
          Serializable transaction isolation.
 
Method Summary
 void addModification(String tableName, boolean inserts, boolean updates, boolean deletes)
          Add table modification information to the TransactionEvent.
 void batchFlush()
          Deprecated. Please use flushBatch
 void commit()
          Commit the transaction.
 void end()
          If the transaction is active then perform rollback.
 void flushBatch()
          The batch will be flushing automatically but you can use this to explicitly flush the batch if you like.
 Connection getConnection()
          Return the underlying Connection object.
 boolean isActive()
          Return true if the transaction is active.
 boolean isBatchFlushOnQuery()
          Return true if the batch (of persisted beans or executed UpdateSql etc) should be flushed prior to executing a query.
 boolean isReadOnly()
          Return true if this transaction is read only.
 void log(String msg)
          Log a comment to the transaction log.
 void rollback()
          Rollback the transaction.
 void rollback(Throwable e)
          Rollback the transaction specifying a throwable that caused the rollback to occur.
 void setBatchFlushOnMixed(boolean batchFlushOnMixed)
          By default when mixing UpdateSql (or CallableSql) with Beans the batch is automatically flushed when you change (between persisting beans and executing UpdateSql or CallableSql).
 void setBatchFlushOnQuery(boolean batchFlushOnQuery)
          By default executing a query will automatically flush any batched statements (persisted beans, executed UpdateSql etc).
 void setBatchGetGeneratedKeys(boolean getGeneratedKeys)
          Specify if you want batched inserts to use getGeneratedKeys.
 void setBatchMode(boolean useBatch)
          Turn on or off statement batching.
 void setBatchSize(int batchSize)
          Specify the number of statements before a batch is flushed automatically.
 void setLoggingOn(boolean isLoggingOn)
          Set this to false to disable logging for this transaction.
 void setPersistCascade(boolean persistCascade)
          Explicitly turn off or on the cascading nature of save() and delete().
 void setReadOnly(boolean readOnly)
          Set whether this transaction should be readOnly.
 

Field Detail

READ_COMMITTED

static final int READ_COMMITTED
Read Committed transaction isolation. Same as java.sql.Connection.TRANSACTION_READ_COMMITTED.

See Also:
Constant Field Values

READ_UNCOMMITTED

static final int READ_UNCOMMITTED
Read Uncommitted transaction isolation. Same as java.sql.Connection.TRANSACTION_READ_UNCOMMITTED.

See Also:
Constant Field Values

REPEATABLE_READ

static final int REPEATABLE_READ
Repeatable read transaction isolation. Same as java.sql.Connection.TRANSACTION_REPEATABLE_READ.

See Also:
Constant Field Values

SERIALIZABLE

static final int SERIALIZABLE
Serializable transaction isolation. Same as java.sql.Connection.TRANSACTION_SERIALIZABLE.

See Also:
Constant Field Values
Method Detail

isReadOnly

boolean isReadOnly()
Return true if this transaction is read only.


setReadOnly

void setReadOnly(boolean readOnly)
Set whether this transaction should be readOnly.


log

void log(String msg)
Log a comment to the transaction log.


setLoggingOn

void setLoggingOn(boolean isLoggingOn)
Set this to false to disable logging for this transaction.


commit

void commit()
            throws javax.persistence.RollbackException
Commit the transaction.

Throws:
javax.persistence.RollbackException

rollback

void rollback()
              throws javax.persistence.PersistenceException
Rollback the transaction.

Throws:
javax.persistence.PersistenceException

rollback

void rollback(Throwable e)
              throws javax.persistence.PersistenceException
Rollback the transaction specifying a throwable that caused the rollback to occur.

If you are using transaction logging this will log the throwable in the transaction logs.

Throws:
javax.persistence.PersistenceException

end

void end()
         throws javax.persistence.PersistenceException
If the transaction is active then perform rollback. Otherwise do nothing.

Throws:
javax.persistence.PersistenceException

isActive

boolean isActive()
Return true if the transaction is active.


setPersistCascade

void setPersistCascade(boolean persistCascade)
Explicitly turn off or on the cascading nature of save() and delete(). This gives the developer exact control over what beans are saved and deleted rather than Ebean cascading detecting 'dirty/modified' beans etc.

This is useful if you can getting back entity beans from a layer of code (potentially remote) and you prefer to have exact control.

This may also be useful if you are using jdbc batching with jdbc drivers that do not support getGeneratedKeys.


setBatchMode

void setBatchMode(boolean useBatch)
Turn on or off statement batching. Statement batching can be transparent for drivers and databases that support getGeneratedKeys. Otherwise you may wish to specifically control when batching is used via this method.

Refer to java.sql.PreparedStatement.addBatch();

Note that you may also wish to use the setPersistCascade method to stop save and delete cascade behaviour. You may do this to have full control over the order of execution rather than the normal cascading fashion.

Note that the execution order in batch mode may be different from non batch mode execution order. Also note that insert behaviour may be different depending on the JDBC driver and its support for getGeneratedKeys. That is, for JDBC drivers that do not support getGeneratedKeys you may not get back the generated IDs (used for inserting associated detail beans etc).

Calls to save(), delete(), insert() and execute() all support batch processing. This includes normal beans, MapBean, CallableSql and UpdateSql.

The flushing of the batched statements is automatic but you can call batchFlush when you like. Note that flushing occurs when a query is executed or when you mix UpdateSql and CallableSql with save and delete of beans.

Example: batch processing executing every 3 rows

 String data = "This is a simple test of the batch processing"
                + " mode and the transaction execute batch method";
 
 String[] da = data.split(" ");
 
 String sql = "{call sp_t3(?,?)}";
 
 CallableSql cs = new CallableSql(sql);
 cs.registerOut(2, Types.INTEGER);
 
 // (optional) inform eBean this stored procedure
 // inserts into a table called sp_test
 cs.addModification("sp_test", true, false, false);
 
 Transaction t = Ebean.beginTransaction();
 t.setBatchMode(true);
 t.setBatchSize(3);
 try {
        for (int i = 0; i < da.length;) {
 
                cs.setParameter(1, da[i]);
                Ebean.execute(cs);
        }
 
        // NB: commit implicitly flushes  
        Ebean.commitTransaction();
 
 } finally {
        Ebean.endTransaction();
 }
 


setBatchSize

void setBatchSize(int batchSize)
Specify the number of statements before a batch is flushed automatically.


setBatchGetGeneratedKeys

void setBatchGetGeneratedKeys(boolean getGeneratedKeys)
Specify if you want batched inserts to use getGeneratedKeys.

By default batched inserts will try to use getGeneratedKeys if it is supported by the underlying jdbc driver and database.

You want wish to turn this off for inserting very large numbers of objects when you don't care about getting back the ids.


setBatchFlushOnMixed

void setBatchFlushOnMixed(boolean batchFlushOnMixed)
By default when mixing UpdateSql (or CallableSql) with Beans the batch is automatically flushed when you change (between persisting beans and executing UpdateSql or CallableSql).

If you want to execute both WITHOUT having the batch automatically flush you need to call this with batchFlushOnMixed = false.

Note that UpdateSql and CallableSql are ALWAYS executed first (before the beans are executed). This is because the UpdateSql and CallableSql have already been bound to their PreparedStatements. The beans on the other hand have a 2 step process (delayed binding).


setBatchFlushOnQuery

void setBatchFlushOnQuery(boolean batchFlushOnQuery)
By default executing a query will automatically flush any batched statements (persisted beans, executed UpdateSql etc).

Calling this method with batchFlushOnQuery = false means that you can execute a query and the batch will not be automatically flushed.


isBatchFlushOnQuery

boolean isBatchFlushOnQuery()
Return true if the batch (of persisted beans or executed UpdateSql etc) should be flushed prior to executing a query.

The default is for this to be true.


flushBatch

void flushBatch()
                throws javax.persistence.PersistenceException,
                       javax.persistence.OptimisticLockException
The batch will be flushing automatically but you can use this to explicitly flush the batch if you like.

Flushing occurs automatically when:

Throws:
javax.persistence.PersistenceException
javax.persistence.OptimisticLockException

batchFlush

void batchFlush()
                throws javax.persistence.PersistenceException,
                       javax.persistence.OptimisticLockException
Deprecated. Please use flushBatch

Deprecated in favour of flushBatch().

Exactly the same as flushBatch. Deprecated as a name change.

Throws:
javax.persistence.PersistenceException
javax.persistence.OptimisticLockException

getConnection

Connection getConnection()
Return the underlying Connection object.

Useful where a Developer wishes to use the JDBC API directly. Note that the commit() rollback() and end() methods on the Transaction should still be used. Calling these methods on the Connection would be a big no no unless you know what you are doing.

Examples of when a developer may wish to use the connection directly are: Savepoints, advanced CLOB BLOB use and advanced stored procedure calls.


addModification

void addModification(String tableName,
                     boolean inserts,
                     boolean updates,
                     boolean deletes)
Add table modification information to the TransactionEvent.

Use this in conjunction with getConnection() and raw JDBC.

This effectively informs Ebean of the data that has been changed by the transaction and this information is normally automatically handled by Ebean when you save entity beans or use UpdateSql etc.

If you use raw JDBC then you can use this method to inform Ebean for the tables that have been modified. Ebean uses this information to keep its caches in synch and maintain lucene text indexes.



Copyright © 2010. All Rights Reserved.