【问题标题】:hibernate 4 has performance issue after migrationhibernate 4 迁移后出现性能问题
【发布时间】:2015-05-15 13:27:36
【问题描述】:

我们的项目已经从 JSF1.1/Hibernate3x 迁移到 JSF2.1.7/Hibernate4,同时添加了 primefaces。从数据库中获取列表/记录时,迁移前需要 30 秒,但现在迁移后需要大约 3 到 4 分钟。这是关键问题之一。

我试图获取的表有 101 列,它有 11000 条记录。我们正在使用 SQL Server 2008 作为数据库

开发环境是Eclipse Kepler

请帮帮我!!

hibernate.cfg.xml

<hibernate-configuration>

<session-factory>
    <property name="dialect">
        org.hibernate.dialect.SQLServerDialect      
    </property>
    <property name="myeclipse.connection.profile">
        com.microsoft.sqlserver.jdbc.SQLServerDriver
    </property>

    <property name="connection.driver_class">
        com.microsoft.sqlserver.jdbc.SQLServerDriver
    </property>
    <property name="hibernate.show_sql">false</property>
    <property name="connection.autocommit">false</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- connection pooling properties -->
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.min_size">3</property>
    <property name="hibernate.c3p0.timeout">100</property>
    <property name="hibernate.c3p0.max_statement">50</property>
    <property name="hibernate.c3p0.idle_test_period">300</property>
    <property name="hibernate.c3p0.acquire_increment">3</property>
    <property name="transaction.factory_class">
        org.hibernate.transaction.JDBCTransactionFactory
    </property>

    <!-- For Hibernate caching -->
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.ehcache.EhCacheProvider</property>
    <property
        name="hibernate.cache.provider_configuration_file_resource_path">
        com/src/hibernate/ehcache.xml
    </property>

</session-factory>

实施

public List search(){
    Transaction tr=null;
    session=HibernateSessionFactory.getSession();
    StringBuilder qry = new StringBuilder();
    List list=new ArrayList();
    try{
        tr=session.beginTransaction();
        qry.append("select locn,(select locationTypeName from TblLocationTypes where locationTypeIdPk = "
                + " locn.trackLocationInfoLocationTypeIdFk) as locType,"
                + "(select locationDivisionName from TblLocationDivision "
                + "where locationDivisionIdPk=locn.trackLocationInfoDivision) as divis "
                + "from TblTrackLocationinformation locn where 1=1 ");

        Query query = session.createQuery(qry.toString());
        list = query.list();
        tr.commit();
    }
    catch(Exception e){
        if(tr != null){
            tr.rollback();
        }
        e.printStackTrace();
    }finally{
        session.flush();
        session.clear();
        session.clear();
    }
    return list;

}

已经试过了

query.scroll 而不是 query.list 但问题仍然相同。

我已经参考并尝试了以下所有链接,但没有解决我的问题

Migration from Hibernate 3 to 4 slows down startup

Why is the Hibernate query.list() slow?

Simple hibernate query returning very slowly

我可以看到 .hbm.xml 文件的一些提示

.hbm.xml

<set cascade="delete" inverse="true" lazy="false" name="tblTrackLocationrelations" sort="unsorted" table="tbl_track_locationrelations"> <key column="track_LocationRelations_location_id_fk" /> <one-to-many class="com.src.hibernate.TblTrackLocationrelations" /> </set>

【问题讨论】:

  • 还请回答您正在测量的问题,屏幕的渲染或方法的执行。这是两件不同的事情。此外,当在 xhtml 中使用它时(并且做错了)可能会导致对该方法的 3 次或更多调用。获取由于新组件库而呈现的额外信息,您的时间呈指数增长。在屏幕上放置 11000 行和大量列通常不是一个好主意。尤其是当您添加各种行为时(或者在这种情况下 PrimeFaces 正在添加该行为)。
  • @RadhamaniMuthusamy 直接在 sql 中测试过查询吗?消除诸如缺少索引之类的数据库问题...?
  • 那你能不能激活登录hibernate生成的sql来看看查询的样子
  • 听起来 hibernate 正在以一种懒惰的方式获取一些详细信息。
  • 问题是非懒惰的关系,你可以尝试让它变得懒惰(不应该对删除产生影响,但应该对读取产生影响)。无论如何,由于数据量很大,而且主要面孔正在执行内存排序这一事实,您仍然需要 3 分钟的渲染时间,您还需要修复它。

标签: java performance hibernate migration


【解决方案1】:

修改懒人作品,缩短了近1分钟。从 lazy=false 更改为 lazy=true

<set cascade="delete" inverse="true" lazy="true" name="tblTrackLocationrelations" sort="unsorted" table="tbl_track_locationrelations"> <key column="track_LocationRelations_location_id_fk" /> <one-to-many class="com.src.hibernate.TblTrackLocationrelations" /> </set>

此外,很少有代码通过构造函数继续调用,因此清理代码将有助于摆脱性能问题。

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多