hibernate+jndi+dbcp

环境:tomcat6+ hibernate2.1.jar

问题:在使用tomcat6自带的dbcp的jar包(tomcat-dbcp.jar,其中已经包含了commons-collections.jar,

commons-dbcp.jar, commons-pool.jar)来配置dbcp数据库连接池出现了各种莫名其妙的问题

解决:使用hibernate2.1自带的 commons-collections.jar,commons-dbcp.jar, commons-pool.jar来替换tomcat-dbcp.jar就ok了

1.jndi与dbcp有什么关系?

jndi是不是也是数据库连接池呢?--不是

不使用dbcp只是用jndi会出现什么情况呢?hibernate中也可以连接数据库

dbcp与c3p0是hibernate推荐使用的

在tomcat的context.xml下,添加

--dbcp连接池配置,这种方式配置可以使用JDNI
<Resource name="jdbc/test"
	          auth="Container" 
	          type="javax.sql.DataSource"
		      maxActive="100" maxIdle="30" maxWait="10000" 
		      username="produsr" password="prod_777"
	          driverClassName="oracle.jdbc.driver.OracleDriver" 
	          url="jdbc:oracle:thin:@10.72.240.31:1522:ffv2prod" 
	          factory="org.apache.commons.dbcp.BasicDataSourceFactory" --必须
	          />

在hibernate.cfg.xml中配置

<!DOCTYPE hibernate-configuration PUBLIC 
	"-//Hibernate/Hibernate Configuration DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="connection.datasource">java:comp/env/jdbc/test</property>
                
                <mapping resource="com/hsp/config/Cat.hbm.xml" />

	    <!--  注释掉了 start
		<property name="connection.provider_class">net.sf.hibernate.connection.DBCPConnectionProvider</property>
		<property name="connection.url">jdbc:oracle:thin:@10.72.240.31:1522:ffv2prod</property>
		<property name="connection.provider_class">net.sf.hibernate.connection.DBCPConnectionProvider</property>
		<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="connection.username">produsr</property>
		<property name="connection.password">prod_777</property>
	
		end-->
	</session-factory>
</hibernate-configuration>

编写hibernateUtil工具类

package com.hsp.util;

import java.io.File;
import java.net.URL;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HibernateUtil {
	
	private static Log log = LogFactory.getLog(HibernateUtil.class);
	
	private static SessionFactory sessionFactory = null;
	
	static{
		//create the SessionFactory
		try {
		 /*1.配置读取"hibernate.cfg.xml"配置文件,默认读取时src目录下的
		  *2.hibernate.cfg.xml配置文件可以放置mapping,使用hibernate.properties配置                  *  文件不行。hibernate.cfg.xml不是必须的
		  */
			sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
		} catch (HibernateException e) {
			log.error("Initial SessionFactory creation failed", e);
			throw new ExceptionInInitializerError(e);
		}
	}
	public static final ThreadLocal session = new ThreadLocal();
	
	public static Session currentSession() throws HibernateException{
		Session s = (Session)session.get();
		if(s == null){
			s = sessionFactory.openSession();
			session.set(s);
		}
		
		return s;
	}
	
	public static void closeSession() throws HibernateException{
		Session s = (Session)session.get();
		session.set(null);
		if(s != null){
			s.close();
		}
	}
	

}

猜你喜欢

转载自weigang-gao.iteye.com/blog/2107311