Search in sources :

Example 1 with OptimisticLockException

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());
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) HashMap(java.util.HashMap) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) RowReaderFactory(org.apache.cayenne.access.jdbc.reader.RowReaderFactory) OptimisticLockException(org.apache.cayenne.access.OptimisticLockException) EntityResolver(org.apache.cayenne.map.EntityResolver) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DbEntity(org.apache.cayenne.map.DbEntity) PreparedStatementResultSetHandler(com.mockrunner.jdbc.PreparedStatementResultSetHandler) DataNode(org.apache.cayenne.access.DataNode) DeleteBatchTranslator(org.apache.cayenne.access.translator.batch.DeleteBatchTranslator) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 2 with OptimisticLockException

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);
        }
    }
}
Also used : BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) DbAdapter(org.apache.cayenne.dba.DbAdapter) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) OptimisticLockException(org.apache.cayenne.access.OptimisticLockException) PreparedStatement(java.sql.PreparedStatement) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

Aggregations

OptimisticLockException (org.apache.cayenne.access.OptimisticLockException)2 PreparedStatementResultSetHandler (com.mockrunner.jdbc.PreparedStatementResultSetHandler)1 MockConnection (com.mockrunner.mock.jdbc.MockConnection)1 PreparedStatement (java.sql.PreparedStatement)1 HashMap (java.util.HashMap)1 DataNode (org.apache.cayenne.access.DataNode)1 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)1 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)1 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)1 DeleteBatchTranslator (org.apache.cayenne.access.translator.batch.DeleteBatchTranslator)1 DbAdapter (org.apache.cayenne.dba.DbAdapter)1 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)1 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 DbEntity (org.apache.cayenne.map.DbEntity)1 EntityResolver (org.apache.cayenne.map.EntityResolver)1 BatchQueryRow (org.apache.cayenne.query.BatchQueryRow)1 DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)1 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)1 Test (org.junit.Test)1