开发者

After a few same hql query the application freezes

开发者 https://www.devze.com 2023-03-14 05:21 出处:网络
Iam calling below function with the same batchNumber and it is working without problem 15 times and takes the records from database without problem but at 16. time the application freezes when the que

I am calling below function with the same batchNumber and it is working without problem 15 times and takes the records from database without problem but at 16. time the application freezes when the query.list() row is called. and not writes "after query" (see System.out row) It just loses debug focus and not give any exception. This problem probably is not about the hql because I've seen this problem before different hql and I used criteria instead of hql and I got pass this problem. But for this when I used "group by" in criteria(setProjection....) it doesn't return the result as hibernate model(object) just returns a list. But I need the results as model.

Note: about 15 times it is just for test. This is a web aplication and user may click the button many times that calls this function to see the taken records from database.

SiteAddressDaoImpl:

public class SiteAddressDaoImpl<T, Id extends Serializable> extends
        GenericDaoHibernateImpl implements SiteAddressDao {
    public List<SiteAddressModel> getSitesByBatch(String batchNumber) {
        try {
            List<SiteAddressModel> siteList;
            MigrationPlanDao migrationPlanDao = ServiceFactory
                    .getO2SiteService().getMigrationPlanDao();
            Query query = this.getSession().createQuery(
                    "from " + persistentClass.getName() + " where "
                            + "siteType =:" + "type and  siteName in "
                            + "(select distinct exchange from "
                            + migrationPlanDao.getPersistentClass().getName()
                            + " where migrationBatchNumber =:" + "batchNumber"
                            + ")");
            query.setString("batchNumber", batchNumber);
            query.setString("type", "LLU/ASN");
            System.out.println("before query");

            siteList = query.list();
            System.out.println("after query");
            return siteList;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    ...
}

GenericDaoHibernateImpl:

public class GenericDaoHibernateImpl<T, Id extends Serializable> extends HibernateDaoSupport implements GenericDao<T, Id> {..........}

Hibernate Properties

 <!-- Hibernate SessionFactory Definition -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      depends-on="annotatedClassRegistrar">
    <property name="dataSource" ref="dataSource"/>

    <property name="annotatedClasses" ref="annotatedClassList"/>

    <property name="hibernateProperties">
        <props>
            <prop key="c3p0.acquire_increment">1</prop>
            <prop key="c3p0.idle_test_period">120</prop>
            <prop key="c3p0.max_size">50</prop>
            <prop key="c3p0.max_statements">0</prop>
            <prop key="c3p0.min_size">20</prop>
            <prop key="c3p0.timeout">0</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
            <prop key="hibernate.connection.autocommit">true</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
        </props>
    </property>
</bean>

<!-- Spring Data Access Exception Translator Defintion -->
<bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
    <property name="dataSource" ref="dataSource"/>
</bean>开发者_JAVA技巧;

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="exceptionTranslator" ref="jdbcExceptionTranslator"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

  <!-- Hibernate Template Defintion -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"/>
    <property name="jdbcExceptionTranslator" ref="jdbcExceptionTranslator"/>
</bean>

beans for a daos

    <bean name="migrationPlanDao"
      class="com.alcatel.lucent.tr.o2ccm.middleware.dao.impl.MigrationPlanDaoImpl">
    <constructor-arg value="com.alcatel.lucent.tr.o2ccm.middleware.model.hibernate.MigrationPlanModel"/>
    <property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>

     <bean name="siteAddressDao" class="com.alcatel.lucent.tr.o2ccm.middleware.dao.impl.SiteAddressDaoImpl">
    <constructor-arg value="com.alcatel.lucent.tr.o2ccm.middleware.model.hibernate.SiteAddressModel"/>
    <property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>

I made the hql simply

 Query query = this.getSession().createQuery("from " + persistentClass.getName() +  " where " +  "siteName='siteName'");

it didn't worked at 16th query.

I used criteria instance of hql, that makes the same job as the hql. It worked what is the difference ...

DetachedCriteria criteria = DetachedCriteria.forClass(persistentClass);
criteria.add(Property.forName("siteName").eq("siteName"));        

siteList = getHibernateTemplate().findByCriteria(criteria);      

Hibernate version 3.2.0.ga

Updated: I added some details.


I solved the problem by HibernateCallBack.

See the comments to learn which implementation is working or not working. Especially by looking at findByHql() and findByHqlQuery() functions it will be easier to see the what is the difference.

findByHql() is the working implementation. The difference seems to be the session because in getSitesByBatch() function when I call the findByHqlQuery() I am creating the query by this.getSession, but when I call findByHql(), I don't need to create the query because this function already creates the query by the session taken from doInHibernate(Session session) function's Session parameter. Direct call to query.list() is also not working.

See the code:

public class SiteAddressDaoImpl<T, Id extends Serializable> extends
        GenericDaoHibernateImpl implements SiteAddressDao {

    public List<SiteAddressModel> getSitesByBatch(String batchNumber) {
        List<SiteAddressModel> siteList;
        MigrationPlanDao migrationPlanDao = ServiceFactory.getO2SiteService()
                .getMigrationPlanDao();
        // working
        String hql = "from " + persistentClass.getName() + " where "
                + "siteType ='LLU/ASN' and  siteName in "
                + "(select distinct exchange from "
                + migrationPlanDao.getPersistentClass().getName()
                + " where migrationBatchNumber =" + "'" + batchNumber + "'"
                + ")";

        siteList = findbyHQL(hql);

        /*
         * // notWorking Query query = this.getSession().createQuery("from " +
         * persistentClass.getName() + " where " + "siteType =:" +
         * "type and  siteName in " + "(select distinct exchange from " +
         * migrationPlanDao.getPersistentClass().getName() +
         * " where migrationBatchNumber =:" + "batchNumber" + ")" );
         * 
         * query.setString("batchNumber", batchNumber); query.setString("type",
         * "LLU/ASN"); siteList = query.list();
         */

        /*
         * //working DetachedCriteria criteria =
         * DetachedCriteria.forClass(persistentClass);
         * criteria.add(Property.forName("siteName").eq("Barnet")); siteList =
         * getHibernateTemplate().findByCriteria(criteria);
         */

        /*
         * //notWorking Query query = this.getSession().createQuery("from " +
         * persistentClass.getName() + " where " + "siteName='Barnet'");
         * siteList = findbyHQLQuery(query);
         */

        /*
         * //working String hql = "from " + getPersistentClass().getName() +
         * " where " + "siteName='Barnet'" ; siteList = findbyHQL(hql);
         */

        return siteList;
    }
}

public class GenericDaoHibernateImpl<T, Id extends Serializable> extends
        HibernateDaoSupport implements GenericDao<T, Id> {
    public List<T> findbyHQL(final String hql) throws DaoException {
        getHibernateTemplate().setAlwaysUseNewSession(true);
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                // String str = "from " + persistentClass.getName() + " o";
                Query query = session.createQuery(hql);
                List<T> list = query.list();
                logger.debug("Find " + list.size() + " records.");
                return list;
            }
        });
    }

    public List<T> findbyHQLQuery(final Query hqlQuery) throws DaoException {
        getHibernateTemplate().setAlwaysUseNewSession(true);
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                List<T> list = hqlQuery.list();
                logger.debug("Find " + list.size() + " records.");
                return list;
            }
        });
    }
}


You give us spectacularly little to go on. The number 15 appears to be too round for the human's eye to ignore. How do you manage transactions and connections? Do you use connection pooling? Do you return connections to the pool? Does your connection pool happen to be sized at exactly 15 connections? Do you have any connections available in the pool when you execute the code for the 16th time? This is the stuff I suggest you check first.

0

精彩评论

暂无评论...
验证码 换一张
取 消