`
温柔一刀
  • 浏览: 860615 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

执行几次查询之后就不能查询了,页面不动了

阅读更多
每执行依次查询,控制台就有这样的警告

java代码如下:
public class Employee_sortDAOImpl extends HibernateDaoSupport implements Employee_sortDAO {
   
   public int getEmployee_sortCount() throws HibernateException {		
		String querySentence = "SELECT count(*) FROM Employee_sort_info";
		List list = this.getHibernateTemplate().find(querySentence);
		Integer count = (Integer) list.get(0);
		return count;
	}
    public List getEmployee_sortByPage(Page page) throws HibernateException {
        String querySentence = "FROM Employee_sort_info";
        Query query = getSession().createQuery(querySentence);
        query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage());
        return query.list();
    }

}


警告: finalizing with closed connection
2006-9-18 14:38:26 org.hibernate.jdbc.ConnectionManager finalize

执行几次查询之后就不能查询了,页面不动了,是因为connection没有关闭吗

问题已经解决了,谢谢哈
修改如下:
public class Employee_sortDAOImpl extends HibernateDaoSupport implements
		Employee_sortDAO {	
	public int getEmployee_sortCount() throws HibernateException {		
		String querySentence = "SELECT count(*) FROM Employee_sort_info";
		List list = this.getHibernateTemplate().find(querySentence);
		Integer count = (Integer) list.get(0);
		return count;
	}
	public List getEmployee_sortByPage(Page page) throws HibernateException {
		String querySentence = "FROM Employee_sort_info";
		Session session=this.getHibernateTemplate().getSessionFactory()	.openSession();
		Query query = session.createQuery(querySentence);
		query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage());		
		List list = query.list();
		session.close();
		return list;
	}
}


还有个问题:如果要实现如下功能,但是让它自己管理session,如何解决,我没有找到HibernateTemplate里面有类似setFirstResult和setMaxResults的方法啊

public List getEmployee_sortByPage(Page page) throws HibernateException {
        String querySentence = "FROM Employee_sort_info";   
        Query query = this.getHibernateTemplate().getSessionFactory().openSession().createQuery(querySentence);
        query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage());      
        return query.list();        
    }



上面的问题都已经解决,谢谢各位,代码如下:

public List getEmployee_sortByPage(final Page page) throws HibernateException {			
		return (List)getHibernateTemplate().execute(
			    new HibernateCallback() {
			        public Object doInHibernate(Session session) throws HibernateException {
			        	String querySentence = "FROM Employee_sort_info";	
			        	Query query = session.createQuery(querySentence);
			    		query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage());
			    		return query.list();
			        }
			    }
			);		
	}


上面的虽然解决了问题,但是太难看了,加了OpenSessionInViewInterceptor方便啊,session可以不管了,也不用使用难看的callback了,呵呵

<!-- SimpleUrlHandlerMapping -->	
	<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="interceptors" ref="openSessionInViewInterceptor"/>		
	    <property name="mappings">
	      <props>	   
	    	 。。。	      	 
	      </props>    
	    </property>
	</bean>	
	
    <bean id="openSessionInViewInterceptor"
          class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>


public class Employee_sortDAOImpl extends HibernateDaoSupport implements
		Employee_sortDAO {	
	public int getEmployee_sortCount() throws HibernateException {
		String querySentence = "SELECT count(*) FROM Employee_sort_info";
		List list = this.getHibernateTemplate().find(querySentence);
		Integer count = (Integer) list.get(0);
		return count;
	}
	public List getEmployee_sortByPage(Page page) throws HibernateException {
		String querySentence = "FROM Employee_sort_info";				
		Query query = this.getSession().createQuery(querySentence);
		query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage());		
		return query.list();	
	}
分享到:
评论
39 楼 belivexiaoqi 2007-04-05  
有学到点东西。
38 楼 liucjj 2006-10-30  
为什么不使用spring提供的回调呢?
37 楼 大地之子 2006-10-27  
回调比较方便,个人感觉。
36 楼 dragonsoar 2006-09-21  
我就一个我发的那个配置文件!
35 楼 温柔一刀 2006-09-21  
那就不知道了
我的是不能直接关闭的象你那样的配置
而且几次调用带有getsession()方法的方法后就不能运行了

你的SimpleUrlHandlerMapping 配置????


<!-- SimpleUrlHandlerMapping -->	
	<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="interceptors" ref="openSessionInViewInterceptor"/>		
	    <property name="mappings">
	      <props>	   
	    	 。。。	      	 
	      </props>    
	    </property>
	</bean>	
	
    <bean id="openSessionInViewInterceptor"
          class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
34 楼 dragonsoar 2006-09-21  
没有警告,而且我查了端口了,都正常的关闭了!
没有问题都!
33 楼 温柔一刀 2006-09-21  
dragonsoar 写道
代码在这里!

我现在的这个肯定能关,我测试过很多次的!


控制台没有警告?

或者您在web.xml有这样的配置?

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
 
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.shtml</url-pattern>
</filter-mapping>
32 楼 dragonsoar 2006-09-21  
代码在这里!

package com.dao;

import java.util.List;

import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.type.Type;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.idao.IObjectDao;

public class ObjectDao extends HibernateDaoSupport implements IObjectDao
{
	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#inserOrUpdateObject(java.lang.Object)
	 */
	public void inserOrUpdateObject(Object object) throws Exception
	{
		getHibernateTemplate().saveOrUpdate(object);
	}

	public void insert(Object object) throws Exception
	{
		getHibernateTemplate().save(object);
	}

	public void updateObject(Object object) throws Exception
	{
		getHibernateTemplate().update(object);
	}

	public Object getObject(Class clzss, Integer integer) throws Exception
	{
		Session session = getSession();
		Object obj = session.load(clzss, integer);
		return obj;
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#deleteObject(java.lang.Object)
	 */
	public void deleteObject(Object object) throws Exception
	{
		getHibernateTemplate().delete(object);
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#deleteObjectByType(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type)
	 */
	public int deleteObjectByType(String tableName, String columName,
			Object columValue, Type type) throws Exception
	{
		return getHibernateTemplate().delete(
			"from " + tableName + " where " + columName + "=? ", columValue,
			type);
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#deleteObjectByType(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type)
	 */
	public int deleteObjectByType(String tableName, String columName1,
			Object columValue1, Type type1, String columName2,
			Object columValue2, Type type2) throws Exception
	{
		return getHibernateTemplate().delete(
			"from " + tableName + " where " + columName1 + "=? and "
					+ columName2 + "=? ",
			new Object[] { columValue1, columValue2 },
			new Type[] { type1, type2 });
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjects(java.lang.String)
	 */
	public List getObjects(String tableName) throws Exception
	{
		return getHibernateTemplate().find("from " + tableName);
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjects(java.lang.String, int, int)
	 */
	public List getObjects(String tableName, int pageCount, int pageSize)
			throws Exception
	{
		return getSession().createQuery("from " + tableName).setFirstResult(
			(pageCount - 1) * pageSize).setMaxResults(pageSize).list();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsPages(java.lang.String)
	 */
	public int getObjectsPages(String tableName) throws Exception
	{
		return ((Integer) getSession().createQuery(
			"select count(*) from " + tableName).list().iterator().next())
			.intValue();

	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsByColumValue(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type, int,
	 *      int)
	 */
	public List getObjectsByColumValue(String tableName, String columName,
			Object columValue, Type type, int pageCount, int pageSize)
			throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("from " + tableName + " where "
				+ columName + "=:columValue order by " + columName + " desc");
		return query.setParameter("columValue", columValue, type)
			.setFirstResult((pageCount - 1) * pageSize).setMaxResults(pageSize)
			.list();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsByColumValuePages(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type)
	 */
	public int getObjectsByColumValuePages(String tableName, String columName,
			Object columValue, Type type) throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("select count(*) from " + tableName
				+ " where " + columName + "=:columValue ");
		return ((Integer) query.setParameter("columValue", columValue, type)
			.list().iterator().next()).intValue();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsByColumIgnoreValue(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type, int,
	 *      int)
	 */
	public List getObjectsByColumIgnoreValue(String tableName,
			String columName, Object columValue, Type type, int pageCount,
			int pageSize) throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("from " + tableName + " where "
				+ columName + " like :columValue order by " + columName
				+ " desc");
		query.setParameter("columValue", "%" + columValue + "%", type)
			.setFirstResult((pageCount - 1) * pageSize).setMaxResults(pageSize);
		return query.list();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsByColumIgnoreValuePages(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type)
	 */
	public int getObjectsByColumIgnoreValuePages(String tableName,
			String columName, Object columValue, Type type) throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("select count(*) from " + tableName
				+ " where " + columName + " like :columValue ");
		query.setParameter("columValue", "%" + columValue + "%", type);
		return ((Integer) query.list().iterator().next()).intValue();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsByColumValue(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type, int,
	 *      int)
	 */
	public List getObjectsByColumValue(String tableName, String columName1,
			Object columValue1, Type type1, String columName2,
			Object columValue2, Type type2, int pageCount, int pageSize)
			throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("from " + tableName + " where "
				+ columName1 + "=:columValue1 and " + columName2
				+ "=:columValue2 order by " + columName1 + " desc");
		return query.setParameter("columValue1", columValue1, type1)
			.setParameter("columValue2", columValue2, type2).setFirstResult(
				(pageCount - 1) * pageSize).setMaxResults(pageSize).list();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsByColumValuePages(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type)
	 */
	public int getObjectsByColumValuePages(String tableName, String columName1,
			Object columValue1, Type type1, String columName2,
			Object columValue2, Type type2) throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("select count(*) from " + tableName
				+ " where " + columName1 + "=:columValue1 and " + columName2
				+ "=:columValue2");
		return ((Integer) query.setParameter("columValue1", columValue1, type1)
			.setParameter("columValue2", columValue2, type2).list().iterator()
			.next()).intValue();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsByColumValue(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type, int,
	 *      int)
	 */
	public List getObjectsByColumValue(String tableName, String columName1,
			Object columValue1, Type type1, String columName2,
			Object columValue2, Type type2, String columName3,
			Object columValue3, Type type3, int pageCount, int pageSize)
			throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("from " + tableName + " where "
				+ columName1 + "=:columValue1 and " + columName2
				+ "=:columValue2 and " + columName3 + "=:columValue3 order by "
				+ columName1 + " desc");
		return query.setParameter("columValue1", columValue1, type1)
			.setParameter("columValue2", columValue2, type2).setParameter(
				"columValue3", columValue3, type3).setFirstResult(
				(pageCount - 1) * pageSize).setMaxResults(pageSize).list();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsByColumValuePages(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type)
	 */
	public int getObjectsByColumValuePages(String tableName, String columName1,
			Object columValue1, Type type1, String columName2,
			Object columValue2, Type type2, String columName3,
			Object columValue3, Type type3) throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("select count(*) from " + tableName
				+ " where " + columName1 + "=:columValue1 and " + columName2
				+ "=:columValue2 and " + columName3 + "=:columValue3");
		return ((Integer) query.setParameter("columValue1", columValue1, type1)
			.setParameter("columValue2", columValue2, type2).setParameter(
				"columValue3", columValue3, type3).list().iterator().next())
			.intValue();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsBeforeDateOrTime(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type, int,
	 *      int)
	 */
	public List getObjectsBeforeDateOrTime(String tableName, String columName,
			Object columValue, Type type, int pageCount, int pageSize)
			throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("from " + tableName + " where "
				+ columName + "<=:columValue order by " + columName + " desc");
		return query.setParameter("columValue", columValue, type)
			.setFirstResult((pageCount - 1) * pageSize).setMaxResults(pageSize)
			.list();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsBeforeDateOrTimePages(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type)
	 */
	public int getObjectsBeforeDateOrTimePages(String tableName,
			String columName, Object columValue, Type type) throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("select count(*) from " + tableName
				+ " where " + columName + "<=:columValue ");
		return ((Integer) query.setParameter("columValue", columValue, type)
			.list().iterator().next()).intValue();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsAfterDateOrTime(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type, int,
	 *      int)
	 */
	public List getObjectsAfterDateOrTime(String tableName, String columName,
			Object columValue, Type type, int pageCount, int pageSize)
			throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("from " + tableName + " where "
				+ columName + ">=:columValue order by " + columName + " desc");
		return query.setParameter("columValue", columValue, type)
			.setFirstResult((pageCount - 1) * pageSize).setMaxResults(pageSize)
			.list();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsAfterDateOrTimePages(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type)
	 */
	public int getObjectsAfterDateOrTimePages(String tableName,
			String columName, Object columValue, Type type) throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("select count(*) from " + tableName
				+ " where " + columName + ">=:columValue ");
		return ((Integer) query.setParameter("columValue", columValue, type)
			.list().iterator().next()).intValue();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsBetweenDateOrTime(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type, int,
	 *      int)
	 */
	public List getObjectsBetweenDateOrTime(String tableName,
			String columName1, Object columValue1, Type type1,
			String columName2, Object columValue2, Type type2, int pageCount,
			int pageSize) throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("from " + tableName + " where "
				+ columName1 + ">=:columValue1 and " + columName2
				+ "<=:columValue2 order by " + columName1 + " desc");
		return query.setParameter("columValue1", columValue1, type1)
			.setParameter("columValue2", columValue2, type2).setFirstResult(
				(pageCount - 1) * pageSize).setMaxResults(pageSize).list();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see com.dao.IObjectDao#getObjectsBetweenDateOrTimePages(java.lang.String,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type,
	 *      java.lang.String, java.lang.Object, net.sf.hibernate.type.Type)
	 */
	public int getObjectsBetweenDateOrTimePages(String tableName,
			String columName1, Object columValue1, Type type1,
			String columName2, Object columValue2, Type type2) throws Exception
	{
		Session session = getSession();
		Query query = session.createQuery("select count(*) from " + tableName
				+ " where " + columName1 + ">=:columValue1 and " + columName2
				+ "<=:columValue2");
		return ((Integer) query.setParameter("columValue1", columValue1, type1)
			.setParameter("columValue2", columValue2, type2).list().iterator()
			.next()).intValue();
	}
}



我现在的这个肯定能关,我测试过很多次的!
31 楼 温柔一刀 2006-09-21  
dragonsoar 写道
Session session = getSession();


这样取出来的session是自动关闭的,我做过测试的,不知道楼主是怎么搞的!

我看了你的配置,几乎和我的一样!我用的是Hibernate2


我用的是Hibernate3,如果不用OpenSessionInView模式,如果您extends HibernateDaoSupport ,再getSession()是不能自动关闭的,我试了好多次的,Hibernate2应该也一样吧,我没有试过Hibernate2
30 楼 dragonsoar 2006-09-20  
下面是我的配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">-->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/bysj</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>12345678</value>
		</property>
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="mappingResources">
			<list>
				<value>model/javabean/User.hbm.xml</value>
				<value>model/javabean/Leaveword.hbm.xml</value>
				<value>model/javabean/Score.hbm.xml</value>
				<value>model/javabean/Student.hbm.xml</value>
				<value>model/javabean/Teacher.hbm.xml</value>
				<value>model/javabean/Tts.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<!--<prop key="hibernate.connection.pool_size">5</prop>-->
			</props>
		</property>
	</bean>

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

	<bean id="objectDAO" class="com.dao.ObjectDao">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>

	<bean id="objectDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>

		<property name="target">
			<ref local="objectDAO" />
		</property>

		<property name="transactionAttributes">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>
</beans>
29 楼 dragonsoar 2006-09-20  
Session session = getSession();


这样取出来的session是自动关闭的,我做过测试的,不知道楼主是怎么搞的!

我看了你的配置,几乎和我的一样!我用的是Hibernate2
28 楼 温柔一刀 2006-09-20  
downpour 写道
查了一下源码。解释一下这个问题吧。
如果你没有使用OpenSessionInView模式,那么在HibernateDaoSupport中使用getSession()获得的Session对象是需要你自己去管理的。在HibernateDaoSupport中,有两个配对的方法,分别是getSession()和releaseSession(Session session)。所以还是可以不用使用难看的callback就解决问题的。在你query.list()之后,调用一句releaseSession(Session session)即可。


谢谢您
我没有用OpenSessionInView模式,releaseSession确实可以关闭session,但是不比close先进,还是要手动管理session。
还是加了OpenSessionInViewInterceptor方便啊,session可以不管了,也不用使用难看的callback了,非常感谢您啊
<!-- SimpleUrlHandlerMapping -->	
	<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="interceptors" ref="openSessionInViewInterceptor"/>		
	    <property name="mappings">
	      <props>	   
	    	 。。。	      	 
	      </props>    
	    </property>
	</bean>	
	
    <bean id="openSessionInViewInterceptor"
          class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
27 楼 downpour 2006-09-20  
查了一下源码。解释一下这个问题吧。

如果你使用OpenSessionInView模式,那么你在DAO中使用getSession()后,不用做任何处理。只要拿过来用就好了,因为此时,Spring拿到的是由OpenSessionInViewFilter打开的线程安全的Session对象。在Filter中最终会帮你管理这个对象。

引用

SessionFactory sessionFactory = lookupSessionFactory(request);
Session session = null;
boolean participate = false;

if (isSingleSession()) {
// single session mode
if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
// Do not modify the Session: just set the participate flag.
participate = true;
}
else {
logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");
session = getSession(sessionFactory);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
}
else {
// deferred close mode
if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
// Do not modify deferred close: just set the participate flag.
participate = true;
}
else {
SessionFactoryUtils.initDeferredClose(sessionFactory);
}
}

try {
filterChain.doFilter(request, response);
}

finally {
if (!participate) {
if (isSingleSession()) {
// single session mode
TransactionSynchronizationManager.unbindResource(sessionFactory);
logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter");
try {
closeSession(session, sessionFactory);
}
catch (RuntimeException ex) {
logger.error("Unexpected exception on closing Hibernate Session", ex);
}
}
else {
// deferred close mode
SessionFactoryUtils.processDeferredClose(sessionFactory);
}
}
}


以上是OpenSessionInViewFilter中的代码,可以看到finally代码段中会调用closeSession()方法。

如果你没有使用OpenSessionInView模式,那么在HibernateDaoSupport中使用getSession()获得的Session对象是需要你自己去管理的。在HibernateDaoSupport中,有两个配对的方法,分别是getSession()和releaseSession(Session session)。所以还是可以不用使用难看的callback就解决问题的。在你query.list()之后,调用一句releaseSession(Session session)即可。
26 楼 downpour 2006-09-20  
可能是由于你没有用OpenSessionInView模式吧。所以getSession()出来的Session是受事务管理,但是不被Spring控制,需要自己关闭。如果你用OpenSessionInView,那么Filter会帮助你关闭的。
25 楼 抛出异常的爱 2006-09-19  
在记忆中的方法没这么复杂
我记得好像在service层
用spring进行控制session
提交还是回滚
(如果测试好似会不回滚用来测试)
24 楼 温柔一刀 2006-09-19  

问题已经解决,非常感谢各位。

温柔一刀 写道
每执行依次查询,控制台就有这样的警告

java代码如下:
public class Employee_sortDAOImpl extends HibernateDaoSupport implements Employee_sortDAO {
   
   public int getEmployee_sortCount() throws HibernateException {		
		String querySentence = "SELECT count(*) FROM Employee_sort_info";
		List list = this.getHibernateTemplate().find(querySentence);
		Integer count = (Integer) list.get(0);
		return count;
	}
    public List getEmployee_sortByPage(Page page) throws HibernateException {
        String querySentence = "FROM Employee_sort_info";
        Query query = getSession().createQuery(querySentence);
        query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage());
        return query.list();
    }

}


警告: finalizing with closed connection
2006-9-18 14:38:26 org.hibernate.jdbc.ConnectionManager finalize

执行几次查询之后就不能查询了,页面不动了,是因为connection没有关闭吗

问题已经解决了,谢谢哈
修改如下:
public class Employee_sortDAOImpl extends HibernateDaoSupport implements
		Employee_sortDAO {	
	public int getEmployee_sortCount() throws HibernateException {		
		String querySentence = "SELECT count(*) FROM Employee_sort_info";
		List list = this.getHibernateTemplate().find(querySentence);
		Integer count = (Integer) list.get(0);
		return count;
	}
	public List getEmployee_sortByPage(Page page) throws HibernateException {
		String querySentence = "FROM Employee_sort_info";
		Session session=this.getHibernateTemplate().getSessionFactory()	.openSession();
		Query query = session.createQuery(querySentence);
		query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage());		
		List list = query.list();
		session.close();
		return list;
	}
}


还有个问题:如果要实现如下功能,但是让它自己管理session,如何解决,我没有找到HibernateTemplate里面有类似setFirstResult和setMaxResults的方法啊

public List getEmployee_sortByPage(Page page) throws HibernateException {
        String querySentence = "FROM Employee_sort_info";   
        Query query = this.getHibernateTemplate().getSessionFactory().openSession().createQuery(querySentence);
        query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage());      
        return query.list();        
    }



上面的问题都已经解决,谢谢各位,代码如下:

public List getEmployee_sortByPage(final Page page) throws HibernateException {			
		return (List)getHibernateTemplate().execute(
			    new HibernateCallback() {
			        public Object doInHibernate(Session session) throws HibernateException {
			        	String querySentence = "FROM Employee_sort_info";	
			        	Query query = session.createQuery(querySentence);
			    		query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage());
			    		return query.list();
			        }
			    }
			);		
	}
23 楼 温柔一刀 2006-09-19  
Allen 写道
downpour 写道
都在搞什么搞啊,HibernateTemplate里面的getSession()方法是受到Spring管理的,拿到的是当前线程安全的Session,无需手动管理。你可以先把Spring和Hibernate的日志打成DEBUG,看一下Transaction和Session的日志。再不明白可以跟踪一下Spring的源码看一下。其实文档里面都有写。查阅一下吧。

根据你提供的情况,应该是你的Transaction的配置有问题,先修改Transaction的配置再试试。


温柔一刀提供的初始代码里面调用的 getSession() 方法是继承自 HibernateDaoSupport 的……

22 楼 Allen 2006-09-19  
downpour 写道
都在搞什么搞啊,HibernateTemplate里面的getSession()方法是受到Spring管理的,拿到的是当前线程安全的Session,无需手动管理。你可以先把Spring和Hibernate的日志打成DEBUG,看一下Transaction和Session的日志。再不明白可以跟踪一下Spring的源码看一下。其实文档里面都有写。查阅一下吧。

根据你提供的情况,应该是你的Transaction的配置有问题,先修改Transaction的配置再试试。


温柔一刀提供的初始代码里面调用的 getSession() 方法是继承自 HibernateDaoSupport 的……
21 楼 温柔一刀 2006-09-19  
dada 写道
温柔一刀 写道

能说详细点么?我看了下源码,execute方法好象实现不了

getHibernateTemplate.execute(
    new HibernateCallBack () {
        public Object doInHibernate(Session session) throws HibernateException {
            // do something
        }
    }
);



非常感谢!修改如下:
public List getEmployee_sortByPage(final Page page) throws HibernateException {			
		return (List)getHibernateTemplate().execute(
			    new HibernateCallback() {
			        public Object doInHibernate(Session session) throws HibernateException {
			        	String querySentence = "FROM Employee_sort_info";	
			        	Query query = session.createQuery(querySentence);
			    		query.setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage());
			    		return query.list();
			        }
			    }
			);		
	}
20 楼 dada 2006-09-19  
温柔一刀 写道

能说详细点么?我看了下源码,execute方法好象实现不了

getHibernateTemplate.execute(
    new HibernateCallBack () {
        public Object doInHibernate(Session session) throws HibernateException {
            // do something
        }
    }
);

相关推荐

    javascript页面加载完执行事件代码

    本文将深入探讨如何在页面加载完成后执行特定的JavaScript事件代码,以及在实现此功能时需要注意的几个关键点。 首先,介绍页面加载状态的判断方法。在JavaScript中,可以通过监听document对象的readyState属性变化...

    屏幕滚动到相应位置,执行css动画

    "屏幕滚动到相应位置,执行css动画"这一技术主要涉及到CSS的定位、媒体查询(Media Queries)以及CSS动画(CSS Animation)等相关知识。下面我们将详细探讨这些关键点。 首先,CSS定位是实现这一效果的基础。在HTML...

    aspx页面事件执行顺序

    ASPX页面事件执行顺序是网页开发中至关重要的概念,它涉及到.NET Framework下的ASP.NET应用程序的生命周期管理。在ASP.NET中,每一个.aspx页面经历一系列的事件,这些事件在页面的加载和处理过程中依次触发,允许...

    Ajax 动态载入html页面后不能执行其中的js快速解决方法

    有一个公用页面需要在多个页面调用,其中涉及到部分js已经写在了公用页面中,通过ajax加载该页面后无法执行其中的js。 解决思路 1. 采用附加一个iframe的方法去执行js,为我等代码洁癖者所不齿。 2. 使用[removed]...

    JSP中把动态页面转换为静态页面.doc

    然而,动态页面每次请求都需要执行服务器上的代码,这可能导致数据库查询的频繁发生。将动态页面转换为静态页面可以显著减少数据库的负载,提高网站的响应速度。 动态页面转换为静态页面的主要思路是利用Filter...

    网页模板——vue实现动态表格数据查询筛选代码.zip

    在本资源中,“网页模板——vue实现动态表格数据查询筛选代码.zip”是一个包含Vue.js实现的动态表格数据查询和筛选功能的代码示例。Vue.js是目前非常流行的前端JavaScript框架,它简化了网页开发,尤其是处理用户...

    查询数据库,由JSP生成静态页面

    在IT行业中,动态网站开发是常见的应用场景,而“查询数据库,由JSP生成静态页面”是一种优化网站性能的策略。这种技术旨在通过将频繁访问的数据转换为静态HTML页面,从而减轻服务器的压力,提高用户访问速度,提升...

    asp.net 动态页面静态化

    ASP.NET动态页面静态化是一种优化网站性能的技术,它将原本由服务器实时生成的动态网页转换为HTML静态页面,从而减少服务器处理和数据库查询的工作量,加快网页加载速度,提高用户体验。这种技术尤其适用于内容更新...

    单页面的js文件动态加载组件

    这样可以减少初始页面加载时间,提高用户体验,因为不是所有的代码都在页面加载时就需要执行。有几种常见的方法可以实现动态加载: 1. **`&lt;script&gt;`标签的async和defer属性**:这两个属性可以在不阻塞页面渲染的...

    JSP页面中模糊查询

    这种技术在软件系统中非常常见,比如搜索引擎Google、Baidu等,在用户输入一个字或词后就能在下拉列表框中列出数据库中的匹配项供用户选择,极大地提高了用户的输入效率。 #### 二、传统C/S结构中的模糊查询实现 ...

    动态Jsp页面转换成静态Html页面

    5. 链接更新:如果动态页面包含其他动态链接,需要修改为静态页面的URL,确保页面间的导航正常。 三、实现步骤 1. 创建Servlet:编写一个Servlet,配置在web.xml中,设定URL映射规则,使得特定的JSP请求会被这个...

    js页面输入查询组件

    在本主题"js页面输入查询组件"中,我们主要讨论的是如何利用JavaScript实现在网页上实时搜索的功能,即用户在输入框中输入文字时,页面能即时展示与输入相匹配的结果。 这种功能在现代Web应用中非常常见,例如搜索...

    jsp动态页面转化静态化--实例

    1. 预生成:在用户请求之前就生成静态页面,适用于内容较少且更新不频繁的网站。 2. 后生成:在用户首次请求时生成静态页面,之后的相同请求直接返回静态HTML。 3. 定期刷新:设定定时任务,定期检查并重新生成静态...

    ASP的多条件动态查询

    在构建好查询字符串后,可以使用ADO的`Command`对象来执行SQL语句,并通过`Recordset`对象来获取查询结果。这通常涉及到以下步骤: 1. 创建`ADODB.Command`对象。 2. 设置命令文本(即SQL查询语句)。 3. 设置命令...

    asp做的成绩查询系统动态网站

    登录成功后,用户可能被导向不同的页面,如个人成绩查询页、班级平均分页等。 3. 查询功能:系统的核心在于查询功能。这可能涉及编写ASP脚本来接收用户的查询参数(如学号、姓名或学期),然后通过SQL查询语句从...

    C# 获取js执行之后的网页源代码(使用线程并设置超时功能)_20200712_140337.rar

    在C#编程中,有时我们需要获取网页的源代码,但有些网页的内容是通过JavaScript动态生成的,单纯使用HttpClient或WebClient等方法无法获取到完整的HTML,因为它们不会执行页面上的JavaScript代码。在这种情况下,...

    不动产学习文档

    - **信息查询**: 查询不动产登记信息。 - **主页**: 提供系统的主要功能入口。 #### 三、实例操作流程 以“集体建设地使用权/房屋所有权首次登记”为例,具体操作步骤如下: 1. **权籍调查**: 首先在权籍调查系统中...

    看一遍就会明白的动态生成静态页面(C#)

    动态生成是指服务器根据用户的请求实时生成网页内容,而静态页面则是提前生成并存储为HTML文件,用户请求时直接返回这些文件,无需再次执行数据库查询或服务器端逻辑。在ASP.NET框架下,我们可以利用C#语言来实现这...

    JSP中把动态页面转换为静态页面

    - 静态页面不能依赖于Session,因为静态文件无法存储会话信息。 - 用户个性化内容或需要登录验证的页面不适合此方法,因为这些页面的内容与用户状态相关。 - 要注意防止递归生成静态页面,即避免Filter生成静态...

Global site tag (gtag.js) - Google Analytics