---
title: "spring + hibernate4 + BoneCP 配置"
author: "focus2008"
date: "2014-07-19T23:19:59Z"
tags: []
categories: ["编程语言"]
slug: "spring-hibernate4-bonecp"
draft: false
lastmod: "2014-07-20T20:34:05Z"
id: 2094074
url: "https://focus2008.iteye.com/blog/2094074"
---

BoneCP(http://www.jolbox.com/) 号称是最快的java数据库连接池，我们项目中使用的就是它。其与spring，hibernate4的配置如下所示：

<?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:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:context="http://www.springframework.org/schema/context"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/tx
			http://www.springframework.org/schema/tx/spring-tx.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context.xsd
			http://www.springframework.org/schema/aop
			http://www.springframework.org/schema/aop/spring-aop.xsd">
			
	<!-- 采用注解驱动事务 -->		
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<!-- 注解事务扫描的基包路径(DAO, Service) -->
	<context:component-scan base-package="com.hz.xcb" />
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- Hibernate SessionFactory -->
	   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
	   <!-- Tell hibernate to use our given datasource -->
	       <property name="dataSource" ref="dataSource" /> 	   
	       <property name="hibernateProperties">
	           <props>
	              <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			<prop key="hibernate.hbm2ddl.auto">update</prop> 
	              <prop key="hibernate.show_sql">true</prop>
	              <prop key="hiberante.format_sql">true</prop>
	           </props>
	       </property>
	       <property name="configLocations">
		<list>
		<value>
			**classpath*:config/hibernate.cfg.xml**
		</value>
		</list>
	</property>
	   </bean>
     
	<!-- Spring bean configuration. Tell Spring to bounce off BoneCP -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.**LazyConnectionDataSourceProxy**">
        <property name="targetDataSource">
            <ref bean="mainDataSource" />
        </property>
    </bean>
    
    <!-- BoneCP configuration -->
    <bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dba01?useUnicode=true&amp;characterEncoding=utf8" />
        <property name="username" value="root"/>
        <property name="password" value="xxxxx"/>
        <property name="idleConnectionTestPeriod" value="60"/>
        <property name="idleMaxAge" value="240"/>      
        <property name="maxConnectionsPerPartition" value="60"/>
        <property name="minConnectionsPerPartition" value="2"/>
        <property name="partitionCount" value="3"/>
        <property name="acquireIncrement" value="10"/>                              
        <property name="statementsCacheSize" value="50"/>
        <property name="releaseHelperThreads" value="3"/>
    </bean>

</beans>

其中hibernate.cfg.xml配置如下：

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="com.hz.xcb.entity.User"/>
        <mapping class="com.hz.xcb.entity......."/>
    </session-factory>
</hibernate-configuration>