gpt4 book ai didi

java - 经过几次相同的 hql 查询后,应用程序卡住

转载 作者:行者123 更新时间:2023-12-01 15:49:23 25 4
gpt4 key购买 nike

我使用相同的batchNumber调用下面的函数,它可以毫无问题地工作15次,并且可以毫无问题地从数据库中获取记录,但在16时,应用程序在查询时卡住。 list()行被调用。并且不会写入“查询后”(请参阅​​ System.out 行)它只是失去调试焦点并且不会给出任何异常。这个问题可能与 hql 无关,因为我在不同的 hql 之前见过这个问题,并且我使用了标准而不是 hql,并且我解决了这个问题。但是为此,当我在 criteria(setProjection...) 中使用“group by”时,它不会返回结果,因为 hibernate model(object) 仅返回一个列表。但我需要结果作为模型。

注:大约15次只是为了测试。这是一个网络应用程序,用户可以多次单击调用此函数的按钮来查看从数据库中获取的记录。

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 属性

 <!-- 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>

<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>

用于 daos 的 bean

    <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>

我简单地制作了 hql

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

它在第 16 次查询时不起作用。

我使用了 hql 的 criteria 实例,它与 hql 的工作相同。它有效有什么区别...

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

siteList = getHibernateTemplate().findByCriteria(criteria);

hibernate 版本3.2.0.ga

更新:我添加了一些详细信息。

最佳答案

我通过HibernateCallBack解决了这个问题。

请参阅评论以了解哪个实现有效或无效。特别是通过查看 findByHql()findByHqlQuery() 函数,会更容易看出有什么区别。

findByHql() 是有效的实现。区别似乎在于 session ,因为在 getSitesByBatch() 函数中,当我调用 findByHqlQuery() 时,我正在通过 创建查询this.getSession,但是当我调用 findByHql() 时,我不需要创建查询,因为该函数已经通过从 doInHibernate(Session) 获取的 session 创建了查询session) 函数的 Session 参数。直接调用 query.list() 也不起作用。

查看代码:

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;
}
});
}
}

关于java - 经过几次相同的 hql 查询后,应用程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6426706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com