hibernate查询/更新多次操作无响应

本文介绍了在使用Hibernate4时遇到的查询操作反复执行的问题及其解决方案。通过调整Spring的dataSource配置,解决了问题,并讨论了使用不同配置方式的优劣。

在用hibernate4的时候,遇到查询操作反复执行,总是在执行到第九次的时候无响应,必须要重启tomcat才可以。


在网上查询了一下,有说session未正常关闭,有说要clear一下,还有说pojo类中的属性名和数据库表的列名不一致,各种说法都有


我也一一试过,然而并没有什么用------------------------------------


然后    。。。。。。。。。。。。。。


然后就在优快云的篇帖子中看到有人回复说是spring的dataSource的配置问题,我的dataSource的class如下(在spring的配置文件:applicationContext.xml中):


class="org.apache.commons.dbcp.BasicDataSource" >

按照帖子中的方法把以上改为了:

class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 


也就是修改了class 。。。


经过测试问题果然解决了。。

但是  
上网大概查了一下,发现此种配置spring的DataSource方式有一个弊端,就是此种配置未使用连接池,这样就给服务器和数据库造成了很大压力,故现在很少在项目中使用了。

具体可以参考:http://blog.youkuaiyun.com/yemou_blog/article/details/50292231


后同事上网查询之后进行了修改,依然使用原来的dataSource配置方式,但是进行了修改,如下:


class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

在后面增加了
destroy-method="close"

还增加了   


	<!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到--> 
     <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean>
     
     <tx:advice id="txAdvice" transaction-manager="txManager">
         <tx:attributes>
             <tx:method name="*" propagation="REQUIRED" />
            <!--  <tx:method name="*" read-only="true"/> -->
         </tx:attributes>
     </tx:advice>
     
     <aop:config proxy-target-class="true">
         <!-- <aop:advisor advice-ref="txAdvice" pointcut="execution(* dao.*.*(..))"/> -->
         <aop:pointcut expression="execution(* com.howin.dao.impl.*.*(..))" id="pointcut"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
     </aop:config>

注意此处用了hibernate5 所以必须要导入hibernate5的jar包。

--------------------------------------------------------------------

附上全部的applicationContext.xml文件内容:


<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
     xmlns:cache="http://www.springframework.org/schema/cache"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.1.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
	
	<!-- Spring注解方式注入 -->
	<context:annotation-config/>
	<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 
	<context:component-scan base-package="com.howin"></context:component-scan>	-->
	<!-- 数据库配置 --><!--org.apache.commons.dbcp.BasicDataSource  -->
	<!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url"
			value="jdbc:mysql://***.***.*.***:3306/***">
		</property>
		<property name="username" value="***"></property>
		<property name="password" value="***"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.use_sql_comments">true</prop>
				<!-- 
				<prop key="hibernate.current_session_context_class">thread</prop>
				
				<prop key="hibernate.cache.use_second_level_cache">true</prop> 
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> -->
			</props>
			
		</property>
		<property name="mappingResources">
			<list>
				<value>com/howin/pojo/Admin.hbm.xml</value>
			</list>
		</property></bean>
		
		<!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到--> 
     <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean>
     
     <tx:advice id="txAdvice" transaction-manager="txManager">
         <tx:attributes>
             <tx:method name="*" propagation="REQUIRED" />
            <!--  <tx:method name="*" read-only="true"/> -->
         </tx:attributes>
     </tx:advice>
     
     <aop:config proxy-target-class="true">
         <!-- <aop:advisor advice-ref="txAdvice" pointcut="execution(* dao.*.*(..))"/> -->
         <aop:pointcut expression="execution(* com.howin.dao.impl.*.*(..))" id="pointcut"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
     </aop:config>

		
		<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
		
		
	<bean id="chatDao" class="com.howin.dao.impl.ChatDaoImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>	
	<bean id="chatBiz" class="com.howin.biz.impl.ChatBizImpl">
		<property name="chatDao" ref="chatDao"/>
	</bean>
	<bean id="callCenterPool" class="com.howin.util.CallCenterPool">
		<property name="chatBiz" ref="chatBiz"/>
	</bean>
</beans>



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值