public static JdbcConfigurationBuilder from(EntityInfo entityInfo) { return new JdbcConfigurationBuilder(entityInfo); }
public JdbcBulkInsert(JdbcConfiguration config) { this.cfg = config; this.columns = cfg.getEntityInfo().getNotIdentityColumns(); this.insertSql = createInsertQuery(cfg.getEntityInfo().getTableName(), columns); for (ColumnInfo column : columns) { accessors.add(column.getAccessor()); } FieldPreConvert fieldPreConvert = new FieldPreConvert(); for (ColumnInfo column : columns) { preConvert.add(fieldPreConvert.preConvert(column.getFieldType())); } }
@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(); } }); } } }
@Override public void insertAll(Connection conn, Stream<T> stream) throws JFleetException, SQLException { TransactionPolicy txPolicy = TransactionPolicy.getTransactionPolicy(conn, cfg.isAutocommit()); try (PreparedStatement pstmt = conn.prepareStatement(insertSql)) { BatchInsert batchInsert = new BatchInsert(txPolicy, pstmt); Iterator<T> iterator = stream.iterator(); while (iterator.hasNext()) { batchInsert.add(iterator.next()); } batchInsert.finish(); } finally { txPolicy.close(); } }
public <T> BulkInsert<T> getBulkInsert(JdbcConfiguration config) { return new JdbcBulkInsert<>(config); }
public void setObjectValues(PreparedStatement pstmt, T entity) throws SQLException { for (int i = 0; i < columns.size(); i++) { Function<Object, Object> accessor = accessors.get(i); Object value = accessor.apply(entity); Function<Object, Object> f = preConvert.get(i); setParameter(pstmt, i + 1, f.apply(value)); } }
public JdbcConfiguration build() { if (entityInfo == null) { JpaEntityInspector inspector = new JpaEntityInspector(clazz); entityInfo = inspector.inspect(); } JdbcConfiguration conf = new JdbcConfiguration(); conf.autocommit = this.autocommit; conf.batchSize = this.batchSize; conf.entityInfo = this.entityInfo; return conf; } }
@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"); } }
@Override public <T> BulkInsert<T> getBulkInsert(Class<T> clazz) { return new JdbcBulkInsert<>(clazz); }
public static JdbcConfigurationBuilder from(Class<?> clazz) { return new JdbcConfigurationBuilder(clazz); }
@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)); } }