论坛首页 Java企业应用论坛

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

浏览 40643 次
精华帖 (1) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间: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>
0 请登录后投票
   发表时间:2006-09-21  
dragonsoar 写道
Session session = getSession();


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

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


我用的是Hibernate3,如果不用OpenSessionInView模式,如果您extends HibernateDaoSupport ,再getSession()是不能自动关闭的,我试了好多次的,Hibernate2应该也一样吧,我没有试过Hibernate2
0 请登录后投票
   发表时间: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();
	}
}



我现在的这个肯定能关,我测试过很多次的!
0 请登录后投票
   发表时间: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>
0 请登录后投票
   发表时间:2006-09-21  
没有警告,而且我查了端口了,都正常的关闭了!
没有问题都!
0 请登录后投票
   发表时间: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>
0 请登录后投票
   发表时间:2006-09-21  
我就一个我发的那个配置文件!
0 请登录后投票
   发表时间:2006-10-27  
回调比较方便,个人感觉。
0 请登录后投票
   发表时间:2006-10-30  
为什么不使用spring提供的回调呢?
0 请登录后投票
   发表时间:2007-04-05  
有学到点东西。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics