Hibernate + c0p3 连接池配置

下面是简单的Hibernate + C3P0连接Oracle的简单配置. 这里记录起来,备以后所用.

1. 需要用到的jar 包

--for hibernate

antlr-2.7.5H3.jar

asm.jar

cglib-2.1.jar

dom4j-1.6.jar

ehcache-1.1.jar

hibernate3.jar

jakarta_commons_collections.jar

jakarta_commons_lang.jar

jakarta_commons_logging.jar

jakarta_log4j.jar

jta.jar

-- for oracle & c3p0

ojdbc14_g.jar

c3p0-0.9.1.2.jar
Hibernate.cfg.xml

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

<hibernate-configuration>
	<session-factory>
<!-- *************************************************** -->
<!-- 				  JDBC config 	 					 -->
<!-- *************************************************** -->
	<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
	<property name="connection.url">
	jdbc:oracle:thin:@(DESCRIPTION =
    	(ADDRESS_LIST =
     	 (ADDRESS = (PROTOCOL = TCP)(HOST = HKCRPD02.IT.HK.HIBM.HSBC)(PORT = 1521))
   		  )
   		 (CONNECT_DATA =
      		(SERVICE_NAME = DLEM02)
    	)
    )
  </property>
	<property name="connection.username">GRT_LEM_DEV1_USER</property>
	<property name="connection.password">abc123</property>
	
<!-- *************************************************** -->
<!-- 				  C3P0 Pool config 					 -->
<!-- *************************************************** -->
	<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
	<!-- 最大连接数 -->
		<property name="hibernate.c3p0.max_size">20</property>
	<!-- 最小连接数 -->
		<property name="hibernate.c3p0.min_size">5</property>
		<property name="hibernate.c3p0.timeout">120</property>
		<property name="hibernate.c3p0.max_statements">100</property>
		<property name="hibernate.c3p0.idle_test_period">120</property>
		<property name="hibernate.c3p0.acquire_increment">3</property>
		<!-- 每次都验证连接是否可用 -->
		<property name="hibernate.c3p0.validate">true</property>
		
		
		<!--是否将运行期生成的SQL输出到日志以供调试--> 
		<property name="show_sql">true</property> 
		
		<!--dialert﹐每個數據庫都有其對應的Dialert以匹配其平台-->
 		<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
		
		<!--事務管理類型,這里使用JDBC Transaction-->
 		<property name="hibernate.transaction.factory_class">
 			org.hibernate.transaction.JDBCTransactionFactory
 		</property>
		
	</session-factory>
</hibernate-configuration>
HibernateSessionFactory

package c3p0.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

	private static final String config_file="hibernate.cfg.xml";
	private static Configuration config;
	private static SessionFactory sf ;
	
	static{
		try{
			config = new Configuration().configure();
			sf = config.buildSessionFactory();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public Session getSession(){
		return sf.openSession();
	}

	
	public static void main(String[] args){
		HibernateSessionFactory hsf = new HibernateSessionFactory();
		
		Session s = hsf.getSession();
		
		System.out.println(s.isConnected());
	}
}

  

相关推荐