Hibernate调优SessionFactory缓存

时间:2013-12-13 01:49:25

标签: performance hibernate caching sessionfactory

我想优化一些Hibernate代码。

我尝试调整Hibernate和Spring, 我想知道如果我只需要添加hibernate缓存属性“true”是否正确:

<prop key="hibernate.cache.use_query_cache">true</prop>

我在Spring中定义了SessionFactory(“aop.xml”):

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.connection.datasource">java:comp/env/jdbc/hibernateCode</prop>
            <prop key="hibernate.connection.pool_size">50</prop>
            <prop key="hibernate.connection.autocommit">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.generate_statistics">false</prop>
            <prop key="hibernate.use_sql_comments">false</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

最后我尝试使用LazyDataModel,实现的方法:

@Override
public List<EnvioMensual> lazyLoad(
        final int index,
        final int count,
        final String sortField,
        final boolean sortOrder,
        final Map<String, String> filters,
        EnvioMensual filtro) {

    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(EnvioMensual.class);

    if (sortField != null && !sortField.isEmpty()) {
        if (sortOrder) {
            criteria = criteria.addOrder(Order.asc(sortField));
        } else {
            criteria = criteria.addOrder(Order.desc(sortField));
        }
    }

    if (!filters.isEmpty()) {
        final Iterator<Entry<String, String>> iterator = filters.entrySet().iterator();
        while (iterator.hasNext()) {
            final Entry<String, String> entry = iterator.next();
            criteria = criteria.add(
                    Restrictions.like(
                            entry.getKey(),
                            entry.getValue(),
                            MatchMode.START
                    )
            );
        }
    }

    criteria = criteria.setFirstResult(index).setMaxResults(count);

    return criteria.list();
}

查询获取正确的参数:

final Criteria criteria = sessionFactory.getCurrentSession()
            .createCriteria(EnvioTraspaso.class);
    if (criteriosFiltrado.getFechaCreacion() != null
            && !criteriosFiltrado.getFechaCreacion().equals("")) {
        final Calendar criterioFecha = criteriosFiltrado.getFechaCreacion();
        final Calendar fechaMinima = CalendarUtils.getFechaMinima(criterioFecha);
        final Calendar fechaMaxima = CalendarUtils.getFechaMaxima(criterioFecha);
        final Conjunction and = Restrictions.conjunction();
        and.add(Restrictions.ge("fechaCreacion", fechaMinima));
        and.add(Restrictions.lt("fechaCreacion", fechaMaxima));
        criteria.add(and);
    }

谢谢阿米尔!我的测试需要很长时间才能响应,然后我需要以这种方式修复我的代码:

@Override
@Transactional
public Set<NotificacionSISNOT> filterBySujeto(final Sujeto sujeto, final int start, final int limit, final String orderBy) {
    final Criteria criteria = sessionFactory.getCurrentSession().createCriteria(entityClass);
    criteria.add(Restrictions....
            ...criteria.setFirstResult(start)
    .setMaxResults(limit)
    .setCacheable(true)
    .setCacheRegion("sisnotpages");

    return new LinkedHashSet<NotificacionSISNOT>(criteria.list());
}

这是对的吗?

0 个答案:

没有答案