// constraints and so that we can take better advantage of batching
try {
int count = 0;
if ( isRowDeleteEnabled() ) {
final Expectation deleteExpectation = Expectations.appropriateExpectation( getDeleteCheckStyle() );
final boolean useBatch = deleteExpectation.canBeBatched();
if ( useBatch && deleteRowBatchKey == null ) {
deleteRowBatchKey = new BasicBatchKey(
getRole() + "#DELETEROW",
deleteExpectation
);
}
final String sql = getSQLDeleteRowString();
PreparedStatement st = null;
// update removed rows fks to null
try {
int i = 0;
Iterator entries = collection.entries( this );
int offset = 1;
while ( entries.hasNext() ) {
Object entry = entries.next();
if ( collection.needsUpdating( entry, i, elementType ) ) { // will still be issued when it used to be null
if ( useBatch ) {
st = session.getTransactionCoordinator()
.getJdbcCoordinator()
.getBatch( deleteRowBatchKey )
.getBatchStatement( sql, isDeleteCallable() );
}
else {
st = session.getTransactionCoordinator()
.getJdbcCoordinator()
.getStatementPreparer()
.prepareStatement( sql, isDeleteCallable() );
}
int loc = writeKey( st, id, offset, session );
writeElementToWhere( st, collection.getSnapshotElement(entry, i), loc, session );
if ( useBatch ) {
session.getTransactionCoordinator()
.getJdbcCoordinator()
.getBatch( deleteRowBatchKey )
.addToBatch();
}
else {
deleteExpectation.verifyOutcome( session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 );
}
count++;
}
i++;
}
}
catch ( SQLException e ) {
if ( useBatch ) {
session.getTransactionCoordinator().getJdbcCoordinator().abortBatch();
}
throw e;
}
finally {
if ( !useBatch ) {
session.getTransactionCoordinator().getJdbcCoordinator().release( st );
}
}
}
if ( isRowInsertEnabled() ) {
final Expectation insertExpectation = Expectations.appropriateExpectation( getInsertCheckStyle() );
boolean useBatch = insertExpectation.canBeBatched();
boolean callable = isInsertCallable();
if ( useBatch && insertRowBatchKey == null ) {
insertRowBatchKey = new BasicBatchKey(
getRole() + "#INSERTROW",
insertExpectation
);
}
final String sql = getSQLInsertRowString();
PreparedStatement st = null;
// now update all changed or added rows fks
try {
int i = 0;
Iterator entries = collection.entries( this );
while ( entries.hasNext() ) {
Object entry = entries.next();
int offset = 1;
if ( collection.needsUpdating( entry, i, elementType ) ) {
if ( useBatch ) {
st = session.getTransactionCoordinator()
.getJdbcCoordinator()
.getBatch( insertRowBatchKey )
.getBatchStatement( sql, callable );
}
else {
st = session.getTransactionCoordinator()
.getJdbcCoordinator()
.getStatementPreparer()
.prepareStatement( sql, callable );
}
offset += insertExpectation.prepare( st );
int loc = writeKey( st, id, offset, session );
if ( hasIndex && !indexContainsFormula ) {
loc = writeIndexToWhere( st, collection.getIndex( entry, i, this ), loc, session );
}
writeElementToWhere( st, collection.getElement( entry ), loc, session );
if ( useBatch ) {
session.getTransactionCoordinator().getJdbcCoordinator().getBatch( insertRowBatchKey ).addToBatch();
}
else {
insertExpectation.verifyOutcome( session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 );
}
count++;
}
i++;
}