@TestDBs @ValueSource(strings = { "JdbcMySql", "JdbcPosgres" }) public void longTransactionWithConstraintExceptionIsRollbacked(@WithDB JdbcDatabase database) throws Exception { try (Connection connection = database.getConnection()) { setupDatabase(connection); connection.setAutoCommit(false); JdbcConfiguration config = JdbcConfigurationBuilder.from(Employee.class) .batchSize(TWO_ROW_BATCH_SIZE) .autocommit(false) .build(); BulkInsert<Employee> bulkInsert = database.getBulkInsert(config); try { bulkInsert.insertAll(connection, employeesWithConstraintError()); connection.commit(); } catch (SQLException e) { logger.info("Expected error on missed FK"); connection.rollback(); assertEquals(0, numberOfRowsInEmployeeTable(connection)); return; } assertTrue(false, "Expected SQLException exception"); } }
@TestDBs @ValueSource(strings = { "JdbcMySql", "JdbcPosgres" }) public void longTransactionExecuteMultipleLoadDataOperationsTransactionaly(@WithDB JdbcDatabase database) throws Exception { try (Connection connection = database.getConnection()) { setupDatabase(connection); connection.setAutoCommit(false); JdbcConfiguration config = JdbcConfigurationBuilder.from(Employee.class) .batchSize(TWO_ROW_BATCH_SIZE) .autocommit(false) .build(); BulkInsert<Employee> bulkInsert = database.getBulkInsert(config); bulkInsert.insertAll(connection, employeesWithOutErrors()); // We don't know how many load data operations were executed, but with // low batch size, multiple load data are executed with few records. assertEquals(7, numberOfRowsInEmployeeTable(connection)); connection.rollback(); assertEquals(0, numberOfRowsInEmployeeTable(connection)); } }
@TestDBs @ValueSource(strings = { "JdbcMySql", "JdbcPosgres" }) public void multipleBatchOperationsExecuteMultipleLoadDataOperationsWithHisOwnTransaction( @WithDB JdbcDatabase database) throws Exception { try (Connection connection = database.getConnection()) { setupDatabase(connection); JdbcConfiguration config = JdbcConfigurationBuilder.from(Employee.class) .batchSize(TWO_ROW_BATCH_SIZE) .autocommit(true) .build(); BulkInsert<Employee> bulkInsert = database.getBulkInsert(config); try { bulkInsert.insertAll(connection, employeesWithConstraintError()); } catch (SQLException e) { logger.info("Expected error on missed FK"); assertTrue(numberOfRowsInEmployeeTable(connection) > 0); return; } assertTrue(false, "Expected SQLException exception"); } }