Skip to content

Instantly share code, notes, and snippets.

View jerolba's full-sized avatar

Jeronimo López jerolba

View GitHub Profile
@jerolba
jerolba / JpaSimpleInsert.java
Last active August 20, 2018 21:56
A register each time
EntityTransaction tx = entityManager.getTransaction();
Iterator<TripEntity> iterator = trips.iterator();
while (iterator.hasNext()) {
tx.begin();
entityManager.persist(iterator.next());
tx.commit();
}
@jerolba
jerolba / JpaSimpleInsertInBlocks.java
Created August 20, 2018 22:06
A register each time in transactions of 1000 elements
EntityTransaction tx = entityManager.getTransaction();
Iterator<TripEntity> iterator = trips.iterator();
tx.begin();
int cont = 0;
while (iterator.hasNext()) {
entityManager.persist(iterator.next());
cont++;
if (cont % batchSize == 0) {
tx.commit();
tx.begin();
@jerolba
jerolba / JpaSimpleInsertInBlocksFlush.java
Created August 20, 2018 22:11
Transactions of 1000 elements with flush
EntityTransaction tx = entityManager.getTransaction();
Iterator<TripEntity> iterator = trips.iterator();
tx.begin();
int cont = 0;
while (iterator.hasNext()) {
entityManager.persist(iterator.next());
cont++;
if (cont % batchSize == 0) {
entityManager.flush();
entityManager.clear();
@jerolba
jerolba / JpaBatchBatchSize.java
Last active August 21, 2018 21:23
In batches of 1000 records
EntityManagerFactoryFactory factory = new EntityManagerFactoryFactory(dsFactory, TripEntity.class) {
@Override
public Properties properties() {
Properties properties = super.properties();
properties.put("hibernate.jdbc.batch_size", batchSize);
return properties;
}
};
@jerolba
jerolba / TripEntity.java
Created August 21, 2018 21:15
Trip Entity
@Entity
@Table(name = "bike_trip")
public class TripEntity implements Trip {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
EntityTransaction tx = entityManager.getTransaction();
Iterator<TripEntityJpa> iterator = trips.iterator();
tx.begin();
int cont = 0;
int idSeq = 1;
while (iterator.hasNext()) {
TripEntityJpa trip = iterator.next();
trip.setId(idSeq++);
entityManager.persist(trip);
cont++;
@jerolba
jerolba / JdbcSimpleInsert.java
Created August 26, 2018 15:26
JDBC, a register each time
connection.setAutoCommit(true);
try (PreparedStatement pstmt = connection.prepareStatement(TripEntityInsert.INSERT)) {
Iterator<TripEntity> iterator = trips.iterator();
while (iterator.hasNext()) {
tripInsert.setParameters(pstmt, iterator.next());
pstmt.executeUpdate();
}
}
@jerolba
jerolba / JdbcSimpleInsertInBlocks.java
Created August 26, 2018 15:34
JDBC a register each time in transactions of 1000 elements
connection.setAutoCommit(false);
try (PreparedStatement pstmt = connection.prepareStatement(TripEntityInsert.INSERT)) {
int cont = 0;
Iterator<TripEntity> iterator = trips.iterator();
while (iterator.hasNext()) {
tripInsert.setParameters(pstmt, iterator.next());
pstmt.executeUpdate();
cont++;
if (cont % batchSize == 0) {
connection.commit();
@jerolba
jerolba / JdbcBatchInsert.java
Created August 26, 2018 15:35
JDBC in batches of 1000 records
connection.setAutoCommit(false);
try (PreparedStatement pstmt = connection.prepareStatement(TripEntityInsert.INSERT)) {
int cont = 0;
Iterator<TripEntity> iterator = trips.iterator();
while (iterator.hasNext()) {
TripEntity entity = iterator.next();
tripInsert.setParameters(pstmt, entity);
pstmt.addBatch();
cont++;
if (cont % batchSize == 0) {
@jerolba
jerolba / myfile
Last active January 7, 2019 14:47
23 2016-01-01 00:00:00 2016-01-01 00:16:00 268 Howard St & Centre St 40.71910537 -73.99973337 3002 South End Ave & Liberty St 40.711512 -74.015756 22285 Subscriber 1958 1
379 2016-01-01 00:00:00 2016-01-01 00:07:00 476 E 31 St & 3 Ave 40.74394314 -73.97966069 498 Broadway & W 32 St 40.74854862 -73.98808416 17827 Subscriber 1969 1
589 2016-01-01 00:00:00 2016-01-01 00:10:00 489 10 Ave & W 28 St 40.75066386 -74.00176802 284 Greenwich Ave & 8 Ave 40.7390169121 -74.0026376103 21997 Subscriber 1982 2
889 2016-01-01 00:01:00 2016-01-01 00:15:00 268 Howard St & Centre St 40.71910537 -73.99973337 3002 South End Ave & Liberty St 40.711512 -74.015756 22794 Subscriber 1961 2
1480 2016-01-01 00:01:00 2016-01-01 00:25:00 2006 Central Park S & 6 Ave 40.76590936 -73.97634151 2006 Central Park S & 6 Ave 40.76590936 -73.97634151 14562 Subscriber \N 1