JDBC Batch updates

As you probably know or guess, doing bulk inserts using JPA is not as fast as it should be, due to the full stack involved. So when using real
bulk inserts, something like that would be terrible:

 Java |  copy |? 
for (String name: listOfNames){
  PersonEntity p = new PersonEntity(name);
  entityManager.persist(p);
}
                

Possible suggestions floating around only tackle parts of the problem (like evicting from 1st Level Cache after persisting). So what we really need with a large number of rows is to get down to JDBC, and remove the necessity to send every single SQL command to the database, but instead prepare only one and parametrize it with a bunch of values.

Serverside-prepared-statements are one (but a rather complex) solution to this. I found another which is kind of basic JDBC, but in fact, i did not know about:

 Java |  copy |? 
Connection c = ...
// make sure the JDBC Driver is capable of using this feature.
// assert c.getMetaData().supportsBatchUpdates();
 
String sql = "INSERT INTO MyTable (col1,col2,col3,col4) VALUES (?,?,?,?)";
PreparedStatement ps = null;
try
{
  ps = c.prepareStatement(sql);
  for (...)
  {
     // populate values for one insert
     ps.setInt(1, ...);
     ps.setInt(2, ... );
     ps.setLong(3, ... );
     ps.setInt(4, ... );
     // and add them to the batch
     ps.addBatch();
  }
  ps.executeBatch();
  c.commit();
}
catch(...
                

As always when using JDBC directly:

Please make absolutely sure to release the Statement as well as the connection reliably, and make sure to use non-autoCommit.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>