SSH整合 通过页面反复执行方法查询数据出现停滞 无异常 不报错

Sakura_ZHJ 中威 初级程序猿  2015-09-29 05:13:59
这个是有关数据源的配置

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


这个是连接信息

hibernate.show_sql=true
hibernate.format_sql=false
#hibernate.hbm2ddl.auto=create
#hibernate.hbm2ddl.auto=update
hibernate.cache=org.hibernate.cache.EhCacheProvider
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.driverClassName=com.mysql.jdbc.Driver

hibernate.connection.url=jdbc:mysql://localhost:3306/car

hibernate.connection.username=root
hibernate.connection.password=123456

hibernate.c3p0.minPoolSize=5
hibernate.c3p0.maxPoolSize=50
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statement=200


一个用到的jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/header.jsp"%>
<head>
<meta charset="utf-8" />
<title>欢迎来到德莱联盟</title>
<meta name="description"
content="998买不了吃亏,998买不了上当,只要998,汽车带回家" />
<meta name="keywords" content="汽车" />
<meta name="generator" content="MetInfo 5.1.7" />
<link href="favicon.ico" rel="shortcut icon" />
<link rel="stylesheet" type="text/css" href="${ctx }/skinner/css/metinfo_ui.css" id="metuimodule" data-module="10001" />
<link rel="stylesheet" type="text/css" href="${ctx }/skinner/css/metinfo.css" />
<style type="text/css">
#iframe{
width:760px; padding-left:20px; margin-bottom:20px;float:right;height:350px;
}

</style>
<script src="${ctx}/skinner/js/jQuery1.7.2.js" type="text/javascript"></script>
<script src="${ctx}/skinner/js/metinfo_ui.js" type="text/javascript"></script>
<script src="${ctx }/skinner/js/ch.js" type="text/javascript"></script>

</head>
<body>
<%@include file='/common/header_menu.jsp' %>
<div class="BAN--bj-2">
<div class="wwwww BAN--flash">
<div class="flash">
<img src='201312/1386552558.jpg' width='1000' alt='内页banner'
height='265'>
</div>
</div>
</div>
<div class="sidebar width">
<div class="sb_nav fl sb-DG">
<h3 class='title myCorner'>新闻中心</h3>
<div class="sb_active" id="sidebar" data-csnow="12" data-class3="0"
data-jsok="2">
<dl class="list-none navnow">
<dt id='part2_19'>
<a href='javascript:void(0)' onclick="jumpCJ();" title='车界资讯' class="zm"><span>车界资讯</span></a>
</dt>
</dl>
<dl class="list-none navnow">
<dt id='part2_20'>
<a href='javascript:void(0)' onclick="alert("123");" title='优惠来袭' class="zm"><span>优惠来袭</span></a>
</dt>
</dl>
<div class="clear"></div>
</div>

<h3 class='title line myCorner' data-corner='top 5px'>联系方式</h3>
<div class="sb--nav-bot editor">
<span style="color: rgb(51, 51, 51); line-height: 2;"><strong>地址:</strong>${company.address }</span>
<div style="color: rgb(51, 51, 51);">
<strong>电话:</strong>${company.tel }
</div>
<div style="color: rgb(51, 51, 51);">
<strong style="line-height: 2;">邮政编号:</strong><span
style="line-height: 2;">${company.youbian }</span>
</div>
<div style="color: rgb(51, 51, 51);">
<strong>邮箱:</strong>${company.mailbox }
</div>
<div class="clear"></div>
</div>

</div>

<iframe frameborder =0 name="iframe" id="iframe" style="background-color:transparent" scrolling="no" src="${ctx }/newsAction_getNewsByParam.action">
</iframe>


<div class="clear"></div>
</div>
<div class="bottom--link"></div>
<footer class="footer">
<div class="width">
<div class="foot-nav">
<a href='../gsgk/' title='公司概况'>公司概况</a><span>|</span><a
href='../jtcp/' title='产品展厅'>产品展厅</a><span>|</span><a
href='../xwzx/' title='新闻中心'>新闻中心</a><span>|</span><a
href='../gzcj/' title='走进工厂'>走进工厂</a><span>|</span><a
href='../gsgk/gsgk17.html' title='联系我们'>联系我们</a><span>|</span><a
href='../job/' title='人力资源'>人力资源</a>
</div>
<div class="foot-text">
<p>
汽车集团 版权所有 2008-2012 湘ICP备8888888
<script src="stat.php" language="JavaScript"></script>
<script src="metinfo/metinfo.js"></script>
</p>
<p>电话:0731-12345678 12345678 QQ:88888888 999999
<div id="metinfo_91mb_Powered"></div>
</p>

</div>
</div>
</footer>
<script src="js/fun.inc.js" type="text/javascript"></script>
</body>
<script type="text/javascript">
function jumpCJ(){
${"#iframe"}.attr("src","${ctx }/newsAction_getNewsByParam.action");

};
function jumpYH(){
alert("优惠");
};
</script>
</html>


在这个方法中发现了问题一

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



出现的问题:
1.当我通过页面反复执行Dao方法(包括相同和不相同的方法)查询数据,只是查询,数次过程序就停滞了,不报错就是在执行HQL语句上停了,等多久过没反应,要重启tomcat后又能查询数次,然后又停了。
2.不知道为什么,我的jsp里所有js都不执行,alert也没反应,eclipse在js语句上有红线。
3.对于1的问题,我想是不是因为session的问题?我用

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

这么写,是将事务交给了Spring吗?应该怎么操作?
...全文
203 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Sakura_ZHJ 2015-10-13
反复查阅资料,参考
http://blog.csdn.net/zljjava/article/details/7535710
发现session没有自己关闭的真正原因是没有交给spring管理。
回复
Sakura_ZHJ 2015-10-09
引用 1 楼 u011031689 的回复:
将Hibernate事务交给spring处理时,会有一个flushMode状态转换。向数据库修改,查找,插入数据时,OpenSessionViewFilte过滤器会将flushMode设置会NEVER,这时事务变成了readOnly状态, 解决办法:在执行CRUD前 添加 Session session = getSessionFactory().openSession(); session.setFlushMode(FlushMode.AUTO); session.save(users); session.flush(); 或者在配置OpenSessionViewFilter时配置 <init-param>flushMode</init-param> <param-value>auto</param-value>
我在web.xml对hibernate的配置
	<!-- hibernate配置 -->
	<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>*.action</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>hibernateFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
没有配置你说的OpenSessionViewFilter这个啊。。我参考的项目里也没有写这东西,不理解的就是怎么自动关闭session??
回复
halking 2015-09-30
将Hibernate事务交给spring处理时,会有一个flushMode状态转换。向数据库修改,查找,插入数据时,OpenSessionViewFilte过滤器会将flushMode设置会NEVER,这时事务变成了readOnly状态, 解决办法:在执行CRUD前 添加 Session session = getSessionFactory().openSession(); session.setFlushMode(FlushMode.AUTO); session.save(users); session.flush(); 或者在配置OpenSessionViewFilter时配置 <init-param>flushMode</init-param> <param-value>auto</param-value>
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-09-29 05:13
社区公告
暂无公告