简体   繁体   中英

How can I verify hibernate.jdbc.batch_size is working?

In my hibernate properties I have <prop key="hibernate.jdbc.batch_size">30</prop>

In my code I do something similar to

final StatelessSession sSession = sessionFactory.openStatelessSession();
try {
    sSession.connection().setAutoCommit(false);
} catch (final SQLException se) {
    // log a message
}
final Transaction tx = sSession.beginTransaction();
try{
    for ( some loop ) {
        Customer customer = new Customer(.....);
        sSession.insert(customer);
        /* Do we need to flush a stateless session? It doesn't have methods for it
        if ( i % 30 == 0 ) { //30, same as the JDBC batch size
            //flush a batch of inserts and release memory:
            sSession.flush();
            sSession.clear();
        }
        */
    } 
    //sSession.flush();// Do we need to flush a stateless session? It doesn't have methods for it
    //sSession.clear();
} finally{
    tx.commit();
    sSession.close();
}

My Pojo has the following

@Id
//@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", nullable = false, unique = true)
private Long id;

However, When I change the batch size it doesn't seem to affect the overall run time. How can I verify that it is in fact working?

Thanks!

comment out session.flush(); and see if anything is inserted after 20 loop interations

Update: to the updated question

commenting //@GeneratedValue(strategy = GenerationType.AUTO) should fall back to default which is AFAIK Identity

try using

@TableGenerator(name="TABLE_GEN", table="SEQUENCE_TABLE", pkColumnName="SEQ_NAME", valueColumnName="SEQ_COUNT", pkColumnValue="EMP_SEQ")
@GeneratedValue(strategy=GenerationType.TABLE, generator="TABLE_GEN")

您可以设置hibernate.generate_statistics = true并查找JDBC批处理上的统计信息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM