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

大家是否测试过Spring管理Hibernate是否释放数据库连接????救命!



大家是否测试过Spring管理Hibernate是否释放数据库连接????救命!

cmd - Dos

mysqladmin -uroot -proot status

Threads 就是当前连接数!!!!!!

我的所有代码全部拿出来希望能解决问题`我是无法释放连接的`希望高手能帮忙!这问题已经捆绕我3天了!
我测试过 : 不用spring管理hibernate 手动关闭session```连接就可以释放!
一但使用Spring管理就无法释放了!郁闷啊!

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="show_sql">true</property>
		<property name="connection.release_mode">after_transaction</property>
		<mapping resource="com/xiaomaha/po/User.hbm.xml" />
	</session-factory>
</hibernate-configuration>


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="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation"
			value="classpath:hibernate.cfg.xml">
		</property>
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<bean id="transactionManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="userDao" class="com.xiaomaha.dao.UserDAO">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="userDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager" ref="transactionManage"></property>
		<property name="target" ref="userDao"></property>
		<property name="transactionAttributes">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
</beans>


po
package com.xiaomaha.po;

import java.util.Date;

/**
 * User generated by MyEclipse Persistence Tools
 */

public class User implements java.io.Serializable {

	// Fields

	private Integer id;

	private String name;

	private String password;

	private Date date;

	// Constructors

	/** default constructor */
	public User() {
	}

	/** minimal constructor */
	public User(String name) {
		this.name = name;
	}

	/** full constructor */
	public User(String name, String password, Date date) {
		this.name = name;
		this.password = password;
		this.date = date;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getDate() {
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

}


dao
package com.xiaomaha.dao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.xiaomaha.po.User;

public class UserDAO extends HibernateDaoSupport implements IUserDAO {

	public void insertUser(User user) {
		this.getHibernateTemplate().save(user);
	}

}

dao接口
package com.xiaomaha.dao;

import com.xiaomaha.po.User;

public interface IUserDAO {
	public void insertUser(User user);
}


客户端 使用的是webservice
package com.xiaomah.services;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xiaomaha.tools.Pack;
//Generated by MyEclipse

import com.xiaomaha.dao.IUserDAO;
import com.xiaomaha.po.User;

public class testImpl implements Itest {
	
	public String example(String message) {
		try {
			/*Pack.createObject(User.class, message) 公司的公共类`动态产生对象用的
			 * webservice调用的哈``自己每测试一次 数据库连接都关不了!还别说开多线程测试了
			 * */
			User user = (User)Pack.createObject(User.class, message);
			ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
			IUserDAO dao = (IUserDAO)app.getBean("userDaoProxy");
			dao.insertUser(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return message;
	}
	
}

也许你会叫我配置连接池`这个我使用过会出来同样的问题!无论是c3po还是tomcat里配置我都试过!连接数量只增加不减少!
现在让我很迷茫```希望高手们帮下我``请也顺便测试自己的程序!
如果你 mysqladmin -uroot -proot status 测试没问题`请大方一点把你的spring配置文件写出来!
我会非常感谢!

补充:插入一次数据库里是有数据的哈!

问题解决了```````解决方式:
webserivce让Spring管理`否则用不了spring事物!
Spring.xml 添加
<!-- Xfire -->

	<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
	
	<bean name="test" class="org.codehaus.xfire.spring.ServiceBean">
		<property name="serviceBean" ref="testImpl" />
		<property name="serviceClass" 
			value="com.xiaomaha.services.Itest" />	
	</bean>

	<bean id="testImpl" class="com.xiaomaha.services.testImpl" >
		<property name="dao" ref="userDao"></property>
	</bean>
	<!--  -->


web.xml 所有配置如下````org.codehaus.xfire.spring.XFireSpringServlet 作为Servlet
<?xml version="1.0" encoding="UTF-8"?>
<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>
			classpath:applicationContext.xml
		</param-value>
	</context-param>
	<!-- Spring framework -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<servlet>
		<servlet-name>XFireServlet</servlet-name>
		<servlet-class>
			org.codehaus.xfire.spring.XFireSpringServlet
		</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>XFireServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


接口实现类修改如下:
package com.xiaomaha.services;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xiaomaha.tools.Pack;
//Generated by MyEclipse

import com.xiaomaha.dao.IUserDAO;
import com.xiaomaha.dao.UserDAO;
import com.xiaomaha.po.User;

public class testImpl implements Itest {
	private UserDAO dao;
	
	public UserDAO getDao() {
		return dao;
	}

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

	public String example(String message) {
		try {
			/*Pack.createObject(User.class, message) 公司的公共类`动态产生对象用的
			 * webservice调用的哈``自己每测试一次 数据库连接都关不了!还别说开多线程测试了
			 * */
			User user = (User)Pack.createObject(User.class, message);
			dao.insertUser(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return message;
	}
	
}

编辑 回复 快速回复 TOP

Re:大家是否测试过Spring管理Hibernate是否释放数据库连接????救命!

对不起,Spring管理的事务我只用了JPA,没有测试过其它应用。
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP

Re:大家是否测试过Spring管理Hibernate是否释放数据库连接????救命!

o !没关系`谢谢你的回复!
编辑 回复 快速回复 TOP

Re:大家是否测试过Spring管理Hibernate是否释放数据库连接????救命!

问题以解决``
怎么结贴?
编辑 回复 快速回复 TOP

Re:大家是否测试过Spring管理Hibernate是否释放数据库连接????救命!

把你解决的方法贴上来吧。这里不需要结贴。

如果你的解决方法好,我可以赠送荣誉!
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP

Re:大家是否测试过Spring管理Hibernate是否释放数据库连接????救命!

<!-- Xfire -->
	<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
	
	<bean name="test" class="org.codehaus.xfire.spring.ServiceBean">
		<property name="serviceBean" ref="testImpl" />
		<property name="serviceClass" 
			value="com.xiaomaha.services.Itest" />	
	</bean>

	<bean id="testImpl" class="com.xiaomaha.services.testImpl" >
		<property name="dao" ref="userDao"></property>
	</bean>
	<!--  -->


我让Spring管理了Hibernate
但是WebServices没有被管理`就会出现释放不了连接!
编辑 回复 快速回复 TOP
发新话题