public class JdbcTransaction extends Object implements SpiTransaction
| Modifier and Type | Field and Description |
|---|---|
protected TransactionManager |
manager
The associated TransactionManager.
|
READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, SERIALIZABLE| Constructor and Description |
|---|
JdbcTransaction(String id,
boolean explicit,
Connection connection,
TransactionManager manager)
Create a new JdbcTransaction.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addModification(String tableName,
boolean inserts,
boolean updates,
boolean deletes)
Add table modification information to the TransactionEvent.
|
void |
batchFlush()
Deprecated in favour of
Transaction.flushBatch(). |
void |
close()
Alias for end(), which enables this class to be used in try-with-resources.
|
void |
commit()
Commit the transaction.
|
protected void |
deactivate() |
int |
depth(int diff)
Return the depth of the current persist request plus the diff.
|
void |
end()
If the transaction is active then perform rollback.
|
void |
flushBatch()
Flush any queued persist requests.
|
BatchControl |
getBatchControl()
Return the queue used to batch up persist requests.
|
int |
getBatchSize()
Return the batchSize specifically set for this transaction or 0.
|
Connection |
getConnection()
Return the underlying connection for public use.
|
List<DerivedRelationshipData> |
getDerivedRelationship(Object bean)
Return the list of "Derived Relationships" that must be maintained after
insert.
|
TransactionEvent |
getEvent()
Return the underlying TransactionEvent.
|
String |
getId()
Return the transaction id.
|
Connection |
getInternalConnection()
Return the underlying connection for internal use.
|
String |
getLogPrefix()
Return the string prefix with the transactin id and label used in logging.
|
PersistenceContext |
getPersistenceContext()
Return the persistence context associated with this transaction.
|
TransactionManager |
getTransactionManger() |
Object |
getUserObject(String name)
Get an object added with
Transaction.putUserObject(String, 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 |
isBatchThisRequest()
Return true if this request should be batched.
|
boolean |
isExplicit()
Return true if this was an explicitly created transaction.
|
boolean |
isLogSql()
Return true if generated SQL and Bind values should be logged to the
transaction log.
|
boolean |
isLogSummary()
Return true if summary level events should be logged to the transaction
log.
|
boolean |
isPersistCascade()
Whether persistCascade is on for save and delete.
|
boolean |
isReadOnly()
Return true if this transaction is read only.
|
boolean |
isRegisteredBean(Object bean)
Return true if this is a bean that has already been saved.
|
boolean |
isRegisteredDeleteBean(Integer persistingBean)
Return true if this is a bean that has already been saved/deleted.
|
boolean |
isSaveAssocManyIntersection(String intersectionTable,
String beanName)
Return true if the m2m intersection save is allowed from a given bean direction.
|
void |
logSql(String msg)
Log a message to the SQL logger.
|
void |
logSummary(String msg)
Log a message to the SUMMARY logger.
|
protected void |
notifyCommit()
Notify the transaction manager.
|
protected void |
notifyRollback(Throwable cause)
Notify the transaction manager.
|
void |
putUserObject(String name,
Object value)
Add an arbitrary user object to the transaction.
|
void |
registerDeleteBean(Integer persistingBean)
Add a bean to the registed list.
|
void |
registerDerivedRelationship(DerivedRelationshipData derivedRelationship)
Register a "Derived Relationship" (that requires an additional update).
|
void |
rollback()
Rollback the transaction.
|
void |
rollback(Throwable cause)
Rollback the transaction.
|
void |
setBatchControl(BatchControl batchControl)
Set the BatchControl to the transaction.
|
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 batchMode)
Turn on or off statement batching.
|
void |
setBatchSize(int batchSize)
Specify the number of statements before a batch is flushed automatically.
|
void |
setLabel(String label) |
void |
setLoggingOn(boolean loggingOn)
Set whether transaction logging is on for this transaction.
|
void |
setPersistCascade(boolean persistCascade)
Explicitly turn off or on the cascading nature of save() and delete().
|
void |
setPersistenceContext(PersistenceContext context)
Set the persistence context to this transaction.
|
void |
setReadOnly(boolean readOnly)
Set whether this transaction should be readOnly.
|
String |
toString() |
void |
unregisterBean(Object bean)
Unregister the persisted bean.
|
void |
unregisterDeleteBean(Integer persistedBean)
Unregister the persisted bean.
|
protected final TransactionManager manager
public JdbcTransaction(String id, boolean explicit, Connection connection, TransactionManager manager)
public void setLabel(String label)
public String getLogPrefix()
SpiTransactiongetLogPrefix in interface SpiTransactionpublic List<DerivedRelationshipData> getDerivedRelationship(Object bean)
SpiTransactiongetDerivedRelationship in interface SpiTransactionpublic void registerDerivedRelationship(DerivedRelationshipData derivedRelationship)
SpiTransactionregisterDerivedRelationship in interface SpiTransactionpublic void registerDeleteBean(Integer persistingBean)
This is to handle bi-directional relationships where both sides Cascade.
registerDeleteBean in interface SpiTransactionpublic void unregisterDeleteBean(Integer persistedBean)
unregisterDeleteBean in interface SpiTransactionpublic boolean isRegisteredDeleteBean(Integer persistingBean)
isRegisteredDeleteBean in interface SpiTransactionpublic void unregisterBean(Object bean)
unregisterBean in interface SpiTransactionpublic boolean isRegisteredBean(Object bean)
isRegisteredBean in interface SpiTransactionpublic boolean isSaveAssocManyIntersection(String intersectionTable, String beanName)
isSaveAssocManyIntersection in interface SpiTransactionpublic int depth(int diff)
The depth of 0 is for the initial persist request. It is modified as the cascading of the save or delete traverses to the the associated Ones (-1) and associated Manys (+1).
The depth is used to help the ordering of batched statements.
depth in interface SpiTransactiondiff - the amount to add or subtract from the depth.public boolean isReadOnly()
TransactionisReadOnly in interface Transactionpublic void setReadOnly(boolean readOnly)
TransactionsetReadOnly in interface Transactionpublic void setBatchMode(boolean batchMode)
Transaction
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();
}
setBatchMode in interface Transactionpublic void setBatchGetGeneratedKeys(boolean getGeneratedKeys)
TransactionBy default batched inserts will try to use getGeneratedKeys if it is supported by the underlying jdbc driver and database.
You may want to turn getGeneratedKeys off when you are inserting a large number of objects and you don't care about getting back the ids.
setBatchGetGeneratedKeys in interface Transactionpublic void setBatchFlushOnMixed(boolean batchFlushOnMixed)
TransactionIf 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).
setBatchFlushOnMixed in interface Transactionpublic int getBatchSize()
Returning 0 implies to use the system wide default batch size.
getBatchSize in interface SpiTransactionpublic void setBatchSize(int batchSize)
TransactionsetBatchSize in interface Transactionpublic boolean isBatchFlushOnQuery()
TransactionThe default is for this to be true.
isBatchFlushOnQuery in interface Transactionpublic void setBatchFlushOnQuery(boolean batchFlushOnQuery)
TransactionCalling this method with batchFlushOnQuery = false means that you can execute a query and the batch will not be automatically flushed.
setBatchFlushOnQuery in interface Transactionpublic boolean isBatchThisRequest()
isBatchThisRequest in interface SpiTransactionpublic BatchControl getBatchControl()
SpiTransactiongetBatchControl in interface SpiTransactionpublic void setBatchControl(BatchControl batchControl)
setBatchControl in interface SpiTransactionpublic void flushBatch()
This is general will result in a number of batched PreparedStatements executing.
flushBatch in interface Transactionpublic void batchFlush()
TransactionTransaction.flushBatch().
Exactly the same as flushBatch. Deprecated as a name change.
batchFlush in interface Transactionpublic PersistenceContext getPersistenceContext()
getPersistenceContext in interface SpiTransactionpublic void setPersistenceContext(PersistenceContext context)
This could be considered similar to EJB3 Extended PersistanceContext. In that you get the PersistanceContext from a transaction, hold onto it, and then set it back later to a second transaction.
setPersistenceContext in interface SpiTransactionpublic TransactionEvent getEvent()
getEvent in interface SpiTransactionpublic void setLoggingOn(boolean loggingOn)
public boolean isExplicit()
isExplicit in interface SpiTransactionpublic boolean isLogSql()
SpiTransactionisLogSql in interface SpiTransactionpublic boolean isLogSummary()
SpiTransactionisLogSummary in interface SpiTransactionpublic void logSql(String msg)
SpiTransactionlogSql in interface SpiTransactionpublic void logSummary(String msg)
SpiTransactionlogSummary in interface SpiTransactionpublic String getId()
getId in interface SpiTransactionpublic Connection getInternalConnection()
getInternalConnection in interface SpiTransactionpublic Connection getConnection()
getConnection in interface Transactionprotected void deactivate()
protected void notifyCommit()
public void commit()
throws javax.persistence.RollbackException
commit in interface Transactionjavax.persistence.RollbackExceptionprotected void notifyRollback(Throwable cause)
public void rollback()
throws javax.persistence.PersistenceException
rollback in interface Transactionjavax.persistence.PersistenceExceptionpublic void rollback(Throwable cause) throws javax.persistence.PersistenceException
rollback in interface Transactionjavax.persistence.PersistenceExceptionpublic void end()
throws javax.persistence.PersistenceException
end in interface Transactionjavax.persistence.PersistenceExceptionpublic boolean isActive()
isActive in interface Transactionpublic boolean isPersistCascade()
SpiTransactionisPersistCascade in interface SpiTransactionpublic void setPersistCascade(boolean persistCascade)
TransactionThis 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.
setPersistCascade in interface Transactionpublic void addModification(String tableName, boolean inserts, boolean updates, boolean deletes)
TransactionUse 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 text indexes.
addModification in interface Transactionpublic void putUserObject(String name, Object value)
TransactionTransactionEventListener.putUserObject in interface Transactionpublic Object getUserObject(String name)
TransactionTransaction.putUserObject(String, Object).getUserObject in interface Transactionpublic final TransactionManager getTransactionManger()
public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionCopyright © 2014. All Rights Reserved.