hibernate 4 在迁移后有性能问题

hibernate 4 has performance issue after migration

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

我试图获取的 table 有 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 instead of query.list but issue remains same.

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

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>

修改lazy works out,减少了将近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>

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