首先为什么要使用连接池及为什么要选择C3P0连接池,这里就不多说了,目前C3P0连接池还是比较方便、比较稳定的连接池,能与spring、hibernate等开源框架进行整合。

一、hibernate中使用C3P0连接池

首先在hibernate项目中引入此c3p0相关jar包,我是在hibernate4.2中拿出来的:

在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"> -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property> <!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property> <!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property> <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="hibernate.c3p0.timeout">120</property>
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
<property name="hibernate.c3p0.max_statements">100</property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="hibernate.c3p0.maxStatementsPerConnection">100</property>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="hibernate.c3p0.idle_test_period">120</property>
<!-- 当连接池耗尽,且未达到最大连接数时,一次获取的连接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
<!--最大空闲时间,25000秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="hibernate.c3p0.maxIdleTime">25000</property> <!--初始化时获取10个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="c3p0.initialPoolSize">10</property>
<!--连接关闭时默认将所有未提交的操作回滚。Default: false -->
<property name="autoCommitOnClose">false</property>
<!--c3p0将建一张名为c3p0_test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么
属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试
使用。Default: null-->
<property name="automaticTestTable">c3p0_test</property>
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts">30</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://192.168.162.125:1433;DatabaseName=test</property>
<property name="connection.username">admin</property>
<property name="connection.password">passwd</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property> <mapping resource="com/hibernate/learn/bean/TestBean.hbm.xml" />
</session-factory>
</hibernate-configuration>

与原来的hibernate配置比较可发现在原来的基础上增加了一些配置,如不使用C3P0连接池,则这些新加的配置去掉即可,hibernate默认使用jdbc连接池。java代码的使用与hibernate的使用方法没区别:

Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();

 二、spring中使用C3P0连接池

直接引入C3P0对应的jar包即可,applicationContext.xml中配置数据源的bean

<?xml version="1.0" encoding="UTF-8"?>
<!-- <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"> <bean id="person" class="com.learn.yt.bean.Person">
<property name="height" value="170"/>
<constructor-arg index="0" ref="skill"/>
</bean> <bean id="skill" class="com.learn.yt.bean.Skill"/> <bean id="dataSource" name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="jdbcUrl" value="jdbc:sqlserver://192.168.162.25:1433;DatabaseName=test_db"/>
<property name="user" value="admin"/>
<property name="password" value="passwd"/>
<property name="maxPoolSize" value="20"/>
<property name="minPoolSize" value="5"/>
<!-- 初始化建立的连接数 -->
<property name="initialPoolSize" value="10"/>
<!-- 最大空闲时间,120秒内未被使用的连接将被丢弃 -->
<property name="maxIdleTime" value="120"/>
<!-- 当连接池耗尽,且未达到最大连接数时,一次获取的连接数 -->
<property name="acquireIncrement" value="2"/>
<!-- 空闲检查时间间隔, 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="idleConnectionTestPeriod" value="60"/>
</bean> <bean id="dataSource2" name="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="jdbcUrl" value="jdbc:sqlserver://192.168.162.25:1433;DatabaseName=test_db2"/>
<property name="user" value="admin"/>
<property name="password" value="passwd"/>
<property name="maxPoolSize" value="20"/>
<property name="minPoolSize" value="5"/>
<property name="initialPoolSize" value="10"/>
<property name="maxIdleTime" value="120"/>
<property name="acquireIncrement" value="2"/>
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
</beans>

其中的dataSource即为C3P0连接池的配置,可配置多个数据库的连接,bean的id不一样即可。代码中的调用直接获取bean实例,用得到的dataSource对象获取连接即可,如:

 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource ds = (DataSource)context.getBean("dataSource");
Connection conn = null;
try {
conn = ds.getConnection();
ResultSet res = conn.createStatement().executeQuery("select * from test_hibernate");
while(res.next()){
System.out.println(res.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}

C3P0连接池在hibernate和spring中的配置的更多相关文章

  1. Druid连接池及监控在spring中的配置

    Druid连接池及监控在spring配置如下: <bean id="dataSource" class="com.alibaba.druid.pool.DruidD ...

  2. Spring c3p0连接池通过Hibernate配置

    首先进行Hibernate配置,详见http://www.cnblogs.com/claricre/p/6509931.html 然后调用这三个包. 配置hibernate.cfg.xml文件: &l ...

  3. (七)Spring 配置 c3p0 连接池

    目录 在 Spring 核心配置文件中配置 c3p0 连接池 配置 JdbcTemplate 对象 在 service 层注入 userDao 在 UserDao 里面注入 JdbcTemplate ...

  4. Hibernate的配置中,c3p0连接池相关配置

    一.配置c3p0 1.导入 hibernate-c3po连接池包,Maven地址是:http://mvnrepository.com/artifact/org.hibernate/hibernate- ...

  5. Spring框架中 配置c3p0连接池 完成对数据库的访问

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...

  6. Spring框架中 配置c3p0连接池

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...

  7. Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法

    通过配置文件XML方法的配置 可以使用非常简练的Service类 UserService类代码如下: package com.swift; public class UserService { pri ...

  8. Hibernate配置C3P0连接池

    引入C3PO包 在hibernate.cfg.xml文件中配置 <!-- 数据库连接池的使用 --> <!-- 选择使用C3P0连接池 --> <property nam ...

  9. Spring c3p0连接池配置

    数据库连接池 数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”.预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去.我们可以通过设定连接 ...

随机推荐

  1. hibernate的session对象核心方法注意的问题

    1.session.save()方法 1).session.save()方法会使一个对象从临时状态转变为持久化状态. 2).session.save()方法会赋予持久化对象的OID属性一个ID值,以对 ...

  2. 新手用git

    最近几天用到了git,作为只看过教程,没有在实际项目中使用过的人来说,简直是 T_T ...... 在这里记录一下,以防以后忘记. clone : 本地没有该库,从远程repository拷贝到本地 ...

  3. servlet获取表单数据的方式和编码方式

    .在servlet中获取表单的数据的几种方式  1>request.getParameter(“name”)://获取指定名称的值,返回值类型是一个字符串  2>request.getPa ...

  4. 启动受管服务器出现:unable to get file lock, will retry...

    启动受管服务器出现:unable to get file lock, will retry... 解决方法:一.删掉Domain下的*.lok文件1. 删除edit.lok进入到domain_home ...

  5. python3 密码生成器

    用random模块实现按照要求生成一定个数和一定位数的密码: #Author by Andy #_*_ coding:utf-8 _*_ import random checkcode='' code ...

  6. android native crash 分析

    工具: addr2line arm-linux-androideabi-addr2line -aCfe libart.so 0x63006d 当libart.so包含符号表的情况下,可以查询到他的地址 ...

  7. Android N做了啥

    Android N做了哪些改变 一.    性能改善 Doze超级省电模式 手机在关屏同时没有充电的情况,在一段时间后会进入打盹状态,第一阶段会停掉同步.作业.网络等访问,第二阶段会停掉app的位置服 ...

  8. Centos启动Cassandra交互模式失败:No appropriate python interpreter found

    在CentOS6.5安装好Cassandra后,启动交互模式: bin/sqlsh 192.168.10.154 时,报错 No appropriate python interpreter foun ...

  9. 数字图像处理作业使用OpenCV - 配置

    使用环境:Windows7 旗舰版 + vs2008 + OpenCV2.0a 基本上配置都是通过网上一个教程,在此附上地址 Click ME. 为了避免因不同版本而出现的安装问题,我还是下载了2.0 ...

  10. oralce 密码长度

    Oracle 11G的新特性所致, Oracle 11G创建用户时缺省密码过期限制是180天, 如果超过180天用户密码未做修改则该用户无法登录. Oracle提示错误消息ORA-28001: the ...