【问题标题】:Spring HibernateTemplate autocommit falseSpring HibernateTemplate 自动提交 false
【发布时间】:2013-07-13 07:35:56
【问题描述】:

我需要从一个表中获取 10 万条记录并以批处理方式插入到另一个表中。我正在使用 Spring3 hibernate 模板进行插入。我只需要在每 100 条记录后提交,但 hibernate 模板在插入时提交每条记录.如何使用hibernate模板进行提交后批量插入或如何在hibernate模板中禁用自动提交。

for(everey iteration){
count = count+1;
getHibernateTemplate().setAlwaysNewSession(true);
CurrentPlan plan = new currentplan();
Rate rate = new rate();
plan.setName("aa");
rate.setRate(2);

getHibernateTemplate.saveorUpdate(plan )
getHibernateTemplate.saveorUpdate(rate )
if(count==100){
getHibernateTemplate.flush();
}
}

谢谢

【问题讨论】:

  • HibernateTemplate 不会在每次插入后提交。它根本不承诺。向我们展示您的代码。
  • 你读过setAlwaysUseNewSession()的javadoc吗?如果是这样,你为什么将它设置为 true,因为它正是你不想做的事情?

标签: spring hibernate spring-jdbc


【解决方案1】:

在休眠中禁用自动提交:

<property name="hibernate.connection.autocommit">false</property> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-16
    • 2014-10-01
    • 2018-11-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    相关资源
    最近更新 更多