use of org.apache.cayenne.access.OptimisticLockException in project cayenne by apache.
the class BatchActionLockingIT method testRunAsIndividualQueriesOptimisticLockingFailure.
@Test
public void testRunAsIndividualQueriesOptimisticLockingFailure() throws Exception {
EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
// test with adapter that supports keys...
JdbcAdapter adapter = buildAdapter(true);
DbEntity dbEntity = resolver.getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
List<DbAttribute> qualifierAttributes = Arrays.asList(dbEntity.getAttribute("LOCKING_TEST_ID"), dbEntity.getAttribute("NAME"));
Collection<String> nullAttributeNames = Collections.singleton("NAME");
Map<String, Object> qualifierSnapshot = new HashMap<>();
qualifierSnapshot.put("LOCKING_TEST_ID", 1);
DeleteBatchQuery batchQuery = new DeleteBatchQuery(dbEntity, qualifierAttributes, nullAttributeNames, 5);
batchQuery.setUsingOptimisticLocking(true);
batchQuery.add(qualifierSnapshot);
DeleteBatchTranslator batchQueryBuilder = new DeleteBatchTranslator(batchQuery, adapter);
MockConnection mockConnection = new MockConnection();
PreparedStatementResultSetHandler preparedStatementResultSetHandler = mockConnection.getPreparedStatementResultSetHandler();
preparedStatementResultSetHandler.setExactMatch(false);
preparedStatementResultSetHandler.setCaseSensitive(false);
preparedStatementResultSetHandler.prepareUpdateCount("DELETE", 0);
boolean generatesKeys = false;
DataNode node = new DataNode();
node.setAdapter(adapter);
node.setEntityResolver(resolver);
node.setRowReaderFactory(mock(RowReaderFactory.class));
BatchAction action = new BatchAction(batchQuery, node, false);
try {
action.runAsIndividualQueries(mockConnection, batchQueryBuilder, new MockOperationObserver(), generatesKeys);
fail("No OptimisticLockingFailureException thrown.");
} catch (OptimisticLockException ignore) {
}
assertEquals(0, mockConnection.getNumberCommits());
assertEquals(0, mockConnection.getNumberRollbacks());
}
use of org.apache.cayenne.access.OptimisticLockException in project cayenne by apache.
the class BatchAction method runAsIndividualQueries.
/**
* Executes batch as individual queries over the same prepared statement.
*/
protected void runAsIndividualQueries(Connection connection, BatchTranslator translator, OperationObserver delegate, boolean generatesKeys) throws SQLException, Exception {
if (query.getRows().isEmpty()) {
return;
}
JdbcEventLogger logger = dataNode.getJdbcEventLogger();
boolean useOptimisticLock = query.isUsingOptimisticLocking();
String queryStr = translator.getSql();
// log batch SQL execution
logger.log(queryStr);
// run batch queries one by one
DbAdapter adapter = dataNode.getAdapter();
try (PreparedStatement statement = prepareStatement(connection, queryStr, adapter, generatesKeys)) {
for (BatchQueryRow row : query.getRows()) {
DbAttributeBinding[] bindings = translator.updateBindings(row);
logger.logQueryParameters("bind", bindings);
bind(adapter, statement, bindings);
int updated = statement.executeUpdate();
if (useOptimisticLock && updated != 1) {
throw new OptimisticLockException(row.getObjectId(), query.getDbEntity(), queryStr, row.getQualifier());
}
delegate.nextCount(query, updated);
if (generatesKeys) {
processGeneratedKeys(statement, delegate, row);
}
logger.logUpdateCount(updated);
}
}
}
Aggregations