发新话题
移动帖子 加入精华 加入置顶 加入收藏 关注此帖

Hibernate+Spring 无法释放connection连接!(最后的希望)



Hibernate+Spring 无法释放connection连接!(最后的希望)

my.ini 里面设置了 max_connections = 1000
客户端使用Delphi开线程 模仿1000个用户对数据库(mysql)进行插入数据
正常无错误信息的情况下可以插入数据`我查看数据库有数据了!
但是大概1分钟左右服务器就会暴无法获取数据库连接``证明已经满了
错误信息如下:
Caused by: java.sql.SQLException: Data source rejected establishment of connecti
on, message from server: "Too many connections"

我怀疑没有释放connection
现在不知道如何解决了``以下是所有代码!!!!!!!!!
DAO
package com.xiaomaha.dao;
import java.io.Serializable;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.xiaomaha.po.User;

public class UserDAO extends HibernateDaoSupport {
	public void save(User user){
		this.getHibernateTemplate().save(user);
	}
}


BO
package com.xiaomaha.bo;
import com.xiaomaha.dao.UserDAO;
import com.xiaomaha.po.User;

public class UserBo {
	private UserDAO dao;

	public UserDAO getDao() {
		return dao;
	}

	public void setDao(UserDAO dao) {
		this.dao = dao;
	}
	
	public void save(User user){
		dao.save(user);
	}
}


webservice接口实现类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xiaomaha.tools.Pack;
import com.xiaomaha.bo.UserBo;
import com.xiaomaha.dao.UserDAO;
//Generated by MyEclipse
import com.xiaomaha.po.User;

public class testImpl implements Itest {
	public String addUser(String message){
		try {
			//这个是我们通过反射写的工具类以测试过没问题的!
			User user = (User) Pack.createObject(User.class, message);
			ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");
			UserBo bo = (UserBo) app.getBean("boProxy");
			bo.save(user);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return message;
	}
	public String addUser2(String message) {

		return message;
	}
	
}


applicationContext.xml
<?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="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation"
			value="classpath:hibernate.cfg.xml">
		</property>
	</bean>
	<bean id="t" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="dao" class="com.xiaomaha.dao.UserDAO">
		<property name="hibernateTemplate" ref="t"></property>
	</bean>
	<bean id="bo" class="com.xiaomaha.bo.UserBo">
		<property name="dao" ref="dao"></property>
	</bean>
	<bean id="transaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="boProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
                  <property name="transactionManager" ref="transaction"></property>
		<property name="target" ref="bo"></property>
		<property name="transactionAttributes">
			<props>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
</beans>

hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

	<session-factory>
		<property name="connection.username">root</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/test
		</property>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="myeclipse.connection.profile">mysql</property>
		<property name="connection.password">root</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="show_sql">true</property>
		<property name="hibernate.connection.release_mode">after_transaction</property>
		<mapping resource="com/xiaomaha/po/User.hbm.xml" />
	</session-factory>

</hibernate-configuration>


老紫竹````````和高手们帮帮吧!具体找不到原因了``google,baidu什么都试过!!!!!!1

编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

<property name="hibernate.connection.release_mode">after_transaction</property>

1 我怀疑你的delphi根本没有提交事务
2 这个参数你可以换其它的看看。
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

Delphi 那边只传一个参数给我!

提交是这边处理`

我里面配置了 事物啊!
<prop key="save*">PROPAGATION_REQUIRED</prop>


如果我没提交`那么我数据库里面怎么会有数据呢?`
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

<property name="hibernate.connection.release_mode">after_transaction</property>
还有什么可以换?能说明白点吗?
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

建议你事务先不要用Spring管理,先确认Hibernae程序正确,他自行管理看看。如果没问题,再把Spring挂上事务。
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

改成 after_statement
看看吧!
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

下面是hibernae的原文,你自己看看

hibernate.connection.release_mode
Specify when Hibernate should release JDBC connections. By default, a JDBC connection is held until the session is explicitly closed or disconnected. For an application server JTA datasource, you should use after_statement to aggressively release connections after every JDBC call. For a non-JTA connection, it often makes sense to release the connection at the end of each transaction, by using after_transaction. auto will choose after_statement for the JTA and CMT transaction strategies and after_transaction for the JDBC transaction strategy.

eg. auto (default) | on_close | after_transaction | after_statement

Note that this setting only affects Sessions returned from SessionFactory.openSession. For Sessions obtained through SessionFactory.getCurrentSession, the CurrentSessionContext implementation configured for use controls the connection release mode for those Sessions. See Section 2.5, “Contextual Sessions”
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

谢谢!那么晚了还在帮我解决问题真的很感动!
我一开始就是hibernate手动提交的`没问题!但是因为群里的朋友叫我用spring管理事物`所以我这样做了! 但是还是会出现这样的问题!我就不太懂了!
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

after_statement
一样的问题`````伤心了`
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

cmd中
mysqladmin -uroot -proot status
看到线程只增不减我汗!

是不是客户端```写的1000个线程都是死循环
所以无法端开?
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

你把客户端改成100个看看,注意看数据库的连接数。

如果到100左右就不再增长,则没有问题了
如果一直涨到1000,那就是Spring管理的事务有问题。
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

哭了``不用测试工具``一条一条执行也同样不释放`昏迷了

老大```
mysqladmin -uroot -proot status
你试试你的吧```
我不用spring管理hibernate 就会释放连接!!!!!!!!!
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

spring的连接池是用hiberntealocal还是c3p0的,连接池都配了吗

<!--连接池中保留的最小连接数。-->
<property name="minPoolSize">
<value>2</value>
</property>

<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize">
<value>30</value>
</property>

<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">
<value>3</value>
</property>

<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime">
<value>60</value>
</property>

<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement">
<value>5</value>
</property>

<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
<property name="maxStatements">
<value>0</value>
</property>

<!--每5小时检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod">
<value>18000</value>
</property>
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

你是不是在另外一个问题里面解决了。
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP

Re:Hibernate+Spring 无法释放connection连接!(最后的希望)

是的已经解决```呵呵`
编辑 回复 快速回复 TOP
发新话题