//查找记录
public List getAllGuestBooks(int firstResult){
Session session = this.getSession();
return session.createQuery("from GuestBook order by id desc").setFirstResult(0).setMaxResults(10).list(); }
-->>>
//查找记录
public List getAllGuestBooks(int firstResult){
return this.bookDAO.getAllGuestBooks(firstResult);
}
action:
private GuestBookBiz biz = new GuestBookBiz();
public GuestBookBiz getBiz() {
return biz;
}
public void setBiz(GuestBookBiz biz) {
this.biz = biz;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm showForm = (DynaActionForm) form;// TODO Auto-generated method stub
System.out.println("11111111111111111111"); System.out.println("2222222222222222");
List list = this.biz.getAllGuestBooks(0);//刷新七八次以上控制台就不输出3333333...了,why?
System.out.println("33333333333333333333");
request.setAttribute("list", list);
return mapping.findForward("list");
}
<table border="3">
<logic:iterate id="guestBook" name="list" length="10" offset="${pageNum*10}">
<tr>
<td>${guestBook.id } </td>
<td>${guestBook.name } </td>
<td>${guestBook.email } </td>
<td>${guestBook.url } </td>
<td>${guestBook.title } </td>
<td>${guestBook.context }</td>
<td>${guestBook.time } </td>
</tr>
</logic:iterate>
</table>
页面数据可以显示,但页面刷新七八次以上就没反应了,很奇怪!有人知道怎么回事吗?
public List getAllGuestBooks(int firstResult){
Session session = this.getSession();
return session.createQuery("from GuestBook order by id desc").setFirstResult(0).setMaxResults(10).list(); }
-->>>
//查找记录
public List getAllGuestBooks(int firstResult){
return this.bookDAO.getAllGuestBooks(firstResult);
}
action:
private GuestBookBiz biz = new GuestBookBiz();
public GuestBookBiz getBiz() {
return biz;
}
public void setBiz(GuestBookBiz biz) {
this.biz = biz;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm showForm = (DynaActionForm) form;// TODO Auto-generated method stub
System.out.println("11111111111111111111"); System.out.println("2222222222222222");
List list = this.biz.getAllGuestBooks(0);//刷新七八次以上控制台就不输出3333333...了,why?
System.out.println("33333333333333333333");
request.setAttribute("list", list);
return mapping.findForward("list");
}
<table border="3">
<logic:iterate id="guestBook" name="list" length="10" offset="${pageNum*10}">
<tr>
<td>${guestBook.id } </td>
<td>${guestBook.name } </td>
<td>${guestBook.email } </td>
<td>${guestBook.url } </td>
<td>${guestBook.title } </td>
<td>${guestBook.context }</td>
<td>${guestBook.time } </td>
</tr>
</logic:iterate>
</table>
页面数据可以显示,但页面刷新七八次以上就没反应了,很奇怪!有人知道怎么回事吗?
List list = this.biz.getAllGuestBooks(0);//这里没有0,写错了,已经在fristResult设置了
Session session = this.getSession();
session.clear();
return session.createQuery("from GuestBook order by id desc").setFirstResult(0).setMaxResults(10).list(); }
试试lazy="false"
http://blog.csdn.net/AWUSOFT/archive/2008/12/28/3626279.aspx
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
看看什么有问题
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/form/show.jsp</welcome-file>
</welcome-file-list>
</web-app><?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="DataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=guestBook">
</property>
<property name="username" value="sa"></property>
</bean>
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="DataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/entity/GuestBook.hbm.xml</value></list>
</property></bean>
<bean id="GuestBookDAO" class="com.entity.GuestBookDAO">
<property name="sessionFactory">
<ref bean="SessionFactory" />
</property>
</bean>
<bean id="GuestBookBiz" class="com.biz.GuestBookBiz">
<property name="bookDAO">
<ref bean="GuestBookDAO"/>
</property>
</bean>
<bean name="/show" class="com.web.struts.action.ShowAction">
<property name="biz" ref="GuestBookBiz"></property>
</bean> </beans><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.entity.GuestBook" table="guestBook" schema="dbo" catalog="guestBook">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<property name="email" type="java.lang.String">
<column name="email" length="50" not-null="true" />
</property>
<property name="url" type="java.lang.String">
<column name="url" length="50" not-null="true" />
</property>
<property name="title" type="java.lang.String">
<column name="title" length="50" not-null="true" />
</property>
<property name="context" type="java.lang.String">
<column name="context" length="200" not-null="true" />
</property>
<property name="time" type="java.lang.String">
<column name="time" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping>
按照你提供的思路我这样写:
//查找记录
public List getAllGuestBooks(){
return (List)getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session) throws HibernateException
{
Query query = session.createQuery("from GuestBook order by id desc");
query.setFirstResult(0);
query.setMaxResults(10);
return query.list();
} },true);
}
还是不行
return session.createQuery("from GuestBook order by id desc").setFirstResult(0).setMaxResults(10).list();这个不用关闭的?session.close()
public List getAllGuestBooks(){
Session session = this.getSession();
session.close();//这里关闭是错误的!session.clear();也没用
return session.createQuery("from GuestBook").setFirstResult(0).setMaxResults(10).list(); }
session.close();//这里关闭是错误的!session.clear();也没用
return session.createQuery("from GuestBook").setFirstResult(0).setMaxResults(10).list()
相当无语
Session session = this.getSession();
list ls=session.createQuery("from GuestBook").setFirstResult(0).setMaxResults(10).list()
sesseion.close();
return ls;
Query query = session.createQuery("from GuestBook order by id desc");
List list = query.list();
session.clear();
return list;//这样也没用!我仔细到网上找了一下,原来是applicationContext.xml配置少了一个设置数据库最大连接数<!-- 数据库的最大连接数 -->
<property name="maxActive" value="200"> </property>
只要加上这一句就把问题解决了!汗啊~~!!
#17楼 ,我也蛮喜欢这个女的!应该是演皇太子的初恋的那个女的!
只要在applicationContext.xml加上这句<!-- 数据库的最大连接数 -->
<property name="maxActive" value="200"> </property>就解决了!
好象只要有设置数据库最大连接数,
session.close()没有同样也可以查询,没有任何影响!
应该是spring管理自动关闭的吧!就象我们不需要考虑数据库连接关闭一样的道理吧!
Session session = getSession();
try {
String queryString = "from Table T where ...";
Query query = session.createQuery(queryString);
query.setFirstResult(...).setMaxResults(...);
resultList = query.list();
}catch(Exception e) {
e.printStackTrace();
}finally {
session.close();
}
return resultList;
session.close()
再加上<property name="maxActive" value="200"> </property> 就搞定了!<property name="maxActive" value="200"> </property> 这句不能少!
ssh整合,Spring,Struts,Hibernate持久层,不需要关闭Session session=this.getSession();
Query query= session.createQuery("from GuestBook order by id desc");
List list= query.list();
session.close();
return list;如果不加<property name="maxActive" value="200">还是会没反应
到底是什么原因呢?
this.getHibernateTemplate().executeFind(new HibernateCallback(){ public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
// TODO Auto-generated method stub
return null;
}
});
WEB.xml中配置<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>public boolean deleteObject(final Object o) {
getHibernateTemplate().execute(new HibernateCallback()//这里使用了匿名内部类
{
public Object doInHibernate(Session session)//Spring进行事务维护 省去每次创建session和关闭session
throws HibernateException
{
if(o!=null)
{
session.delete(o);
session.flush();
}
return null;
}
}, true);
return true;
}
不需要去管开启和关闭