Spring3.2.8+Hibernate4.2.12+SpringMVC+JPA框架:
http://blog.csdn.net/lx376693576/article/details/44116267
在上面的基础上加入c3p0连接池:
1、需要的jar包:
hibernate-c3p0-4.2.12.Final.jar //需与hibernate的版本对应
c3p0-0.9.1.2.jar
2、在persistece.xml文件中做如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<!--
Name属性用于定义持久化单元的名字 (name必选,空值也合法);
transaction-type 指定事务类型(可选)
-->
<persistence-unit name="myJPA" transaction-type="RESOURCE_LOCAL">
<!-- javax.persistence.PersistenceProvider接口的一个实现类(可选) -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!--配置Hibernate方言 -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<!--配置数据库驱动 -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<!--配置数据库用户名 -->
<property name="hibernate.connection.username" value="root" />
<!--配置数据库密码 -->
<property name="hibernate.connection.password" value="123456" />
<!--配置数据库url -->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />
<!--设置外连接抓取树的最大深度 -->
<property name="hibernate.max_fetch_depth" value="3" />
<!--自动在控制台输出schema创建DDL的语句 -->
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="javax.persistence.validation.mode" value="none"/>
<!-- 下面开始c3p0的配置 -->
<property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider"/>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size" value="1"/>
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size" value="20"/>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位(毫秒) -->
<property name="hibernate.c3p0.timeout" value="5000"/>
<!-- 指定连接池里最大缓存多少个Statement对象 -->
<property name="hibernate.c3p0.max_statements" value="100"/>
<!-- 每隔3000秒检查连接池里的空闲连接 ,单位是(秒)-->
<property name="hibernate.c3p0.idle_test_period" value="3000"/>
<!-- 当连接池里面的连接用完的时候,C3P0自动一次性获取多少个新的连接 -->
<property name="hibernate.c3p0.acquire_increment" value="2"/>
<!-- 每次都验证连接是否可用 -->
<property name="hibernate.c3p0.validate" value="true"/>
</properties>
</persistence-unit>
</persistence>
3、然后就可以用了。