javahibernatesessionfactory

Can you overwrite the batch size set in SessionFactoryOptions?


Consider the code below, would this overwrite the batch_size configured for the sessionFactory to 25 or will there be some conflict because the batch_size configuration doesn't match the code?

//this prints: "Config batch_size: 100"
System.out.println("Config batch_size: "+sessionFactory.getSessionFactoryOptions().getJdbcBatchSize());
int batchSize = 25;
int batchCount = 0;
Session session = sessionFactory.getCurrentSession();
for (Users user : users) {
    session.update(user);
    batchCount++;
    if (batchCount % batchSize == 0) {
        session.flush();
        session.clear();
    }
}

Solution

  • Your implemented loop and hibernate.jdbc.batch_size operates on different levels.

    Your loop operates on entities (of your choice). But a single User might need multiple inserts (for example, when a User has multiple Adresses). All these inserts get executed as (25 insertions) single inserts with your loop approach. The flush and clear just prevent the session to grow without limit.

    hibernate.jdbc.batch_size on the other hand will bundle (insertion)statements that are identical and only differ in in the parameter values into a single statement, with a list of parameter sets. The execution of such a batched statement should be processed much more efficient by the database then the equivalent single statements.

    it will send 25 queries at a time (going by the code) and also send 100 queries at a time (going by the configuration) but at different level. I hope Makes sense.