问题描述
- hibernate count查询三次后,不报错,无异常,停止了
-
刚接触hibernate,写了个查询public int CountNews() { try { String sql="select count(*) from News n"; Query q=getSession().createQuery(sql); List<?> list=q.list(); if (list!=null&&list.size()>0) { return Integer.parseInt(list.get(0).toString()); }else { return 0; } } catch (Throwable e) { e.printStackTrace(); } return 0; }
我用sql,hql,都会在查询的那一步停止,断点打在上面没问题,就是再往下一步,程序就不动了,什么反应都没有,很奇怪。。
我反复执行这个方法,三次没关系,但是第四次就会出现这个问题,网上找了半天,似乎是session的问题?不是很懂,我现在还要关闭session吗??怎么关闭?
解决方案
查完一次就关闭session
解决方案二:
这是我相关的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName" default-lazy-init="true">
<!-- 属性文件读入 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:hibernate.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${hibernate.connection.driverClassName}"/>
<property name="url" value="${hibernate.connection.url}"/>
<property name="username" value="${hibernate.connection.username}"/>
<property name="password" value="${hibernate.connection.password}"/>
</bean>
<!--Hibernate SessionFatory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>cn.com.car.base.entity.Car</value>
<value>cn.com.car.base.entity.XcImage</value>
<value>cn.com.car.base.entity.Company</value>
<value>cn.com.car.base.entity.News</value>
</list>
</property>
<property name="hibernateProperties">
<value>classpath:hibernate.properties</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
时间: 2024-09-12 16:42:52