Hibernate Dao类太慢

时间:2015-09-07 11:32:23

标签: java performance hibernate dao

在我的宁静应用程序中,我注意到了dao类速度的问题。对于所选择的约10个小记录,每个呼叫事务大约需要0.12-0.18秒。但是我需要在一次请求中多次调用不同的dao类。

public class ShellDao {

public ShellDao(){}

public List<Object[]> getBackgroundImages() {

    long startTime = System.nanoTime();
    Session session = null;
    Transaction tx = null;

    try {
        session = HibernateAnnotationUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        String hql = "SELECT1";
        Query query = session.createSQLQuery(hql);
        List<Object[]> result = query.list();
        tx.commit();
        if (result.size() != 0) {
            return result;
        } else {
            return null;
        }

    } catch (Exception e) {
        try {
            tx.rollback();
        } catch (RuntimeException rbe) {
            rbe.printStackTrace();
        }
        e.printStackTrace();
        return null;
    } finally {
        if (session != null) {
            session.close();
            long stopTime = System.nanoTime();
            Double time = (stopTime-startTime)/ 1000000000.0;
            System.out.println(time);
        }
    }
}

是否有任何方法可以避免每次调用beginTransaction?这需要大约0.062275091s。

0 个答案:

没有答案