每次转换后休眠提交都会使其变慢 [英] Hibernate commit after each transation makes it slow

查看:64
本文介绍了每次转换后休眠提交都会使其变慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个应用程序,其中当数组带有请求时,我必须从表中选择数据,如果找不到,则生成新值并插入表中,然后再次循环统计检查下一个值等等.对于新请求,此操作将完成约50000次.

Hi i have an application in which a when request comes with an array i have to select data from the table and if not found generate new value and insert into the table,and then again the loops stats over check for the next value and so on. This will be done around 50000 times for a new request.

我正在使用

  public void save(Object obj,Session session)  {       

        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(obj);
            //session.saveOrUpdate(obj);
            //session.flush();

            session.clear();
            tx.commit();
        }
        catch (HibernateException e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);            

            if(tx!=null){
                tx.rollback();
            }
        } 
}

提交方法,对于每个生成的新值都会调用它,就像每个请求一样,它被调用50000次. 我必须为每个值提交事务,因为我不希望在表中生成重复的值,因此,如果新请求包含相同的数据,并且前一个正在运行,则应从表中获取该值.所以我认为我将不得不提交每个事务..但是,如果我使用经典的JDBC而不休眠的话,这会变得非常快吗?与冬眠相比,我做错什么了吗?因为我认为大部分时间都花在了一次又一次的提交事务中.

method to commit and it is called for each new value generated.like for each request it is called 50000 times. I have to commit my transactions for each value cause i dont want a duplicate value to be generated in the table,so if a new request comes with the same data and the previous is running , the value should be fetched from the table. so i think i will have to commit each transaction.. But if i use classic JDBC and not hibernate this becomes quite fast y so? am i doing something wrong of is hibernate slow as compared to it? cause i think most of the time is consumed in begin and commit transaction again and again.

推荐答案

您正在做的是批处理.

很少有想法

  • 使交易更大(跨度超过1个插入)
  • 使用乐观锁定:如果发生冲突,请重试
  • 配置JDBC批处理大小

为避免重复插入,请添加一个额外的列,该列保留自然键并具有唯一的约束.

To avoid duplicate insert, add an extra column that holds the natural key and has a unique constraint.

这篇关于每次转换后休眠提交都会使其变慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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