如何验证hibernate.jdbc.batch_size是否正常工作? [英] How can I verify hibernate.jdbc.batch_size is working?

查看:227
本文介绍了如何验证hibernate.jdbc.batch_size是否正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的休眠属性中,我有 < prop key ="hibernate.jdbc.batch_size"> 30</prop>

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();
}

我的Pojo具有以下内容

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?

谢谢!

推荐答案

注释session.flush();,看看在20次循环插入后是否插入了任何东西

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

更新:更新后的问题

注释//@GeneratedValue(strategy = GenerationType.AUTO)应该退回到默认值,即AFAIK身份

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

尝试使用

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

这篇关于如何验证hibernate.jdbc.batch_size是否正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆