@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"); } }
public static void main(String[] args) throws IOException, SQLException { Supplier<Connection> connectionSuplier = new MySqlTestConnectionProvider(); try (Connection connection = connectionSuplier.get()){ TableHelper.createTable(connection); CitiBikeReader<TripFlatEntity> reader = new CitiBikeReader<>("/tmp", str -> new FlatTripParser(str)); JdbcConfiguration config = JdbcConfigurationBuilder.from(TripFlatEntity.class) .batchSize(100).build(); BulkInsert<TripFlatEntity> bulkInsert = new JdbcBulkInsert<>(config); reader.forEachCsvInZip(trips -> { try { bulkInsert.insertAll(connection, trips); } catch (JFleetException | SQLException e) { e.printStackTrace(); } }); } } }
public static JdbcConfigurationBuilder from(Class<?> clazz) { return new JdbcConfigurationBuilder(clazz); }
public static JdbcConfigurationBuilder from(EntityInfo entityInfo) { return new JdbcConfigurationBuilder(entityInfo); }