C3p0和Druid的简单使用和介绍

C3p0:

C3p0是一种开源的数据连接池,它实现了DataSource数据源接口,支持JDBC2和JDBC3的标准规范。
在使用C3p0数据源开发的时候,需要了解DataSource接口的实现类ComboPooledDataSource,它是C3p0的核心类,以下是它的一些常用方法:

  • void setDriverClass() —————————-设置连接数据库的数据名称
  • void setJdbcUrl() ———————————-设置连接数据库的路径
  • void setUser() ————————————–设置连接数据库的登录账号
  • void setPassword() ——————————-设置连接数据库的登录密码
  • void setMaxPoolSize() —————————-设置数据库连接池最大连接数
  • void setMinPoolSize() —————————–设置数据库连接池最小连接数
  • void setInitialPoolSize() —————————-设置数据库连接池初始化连接数目
  • Connection getConnection() ———————-从数据库连接池中获取一个连接

通过ComboPooledDataSource()构造方法创建数据源对象,需要手动设置数据源对象的属性值。

ComboPooledDataSource cpds = new ComboPooledDataSource(); 
cpds.setDriverClass( "com.mysql.jdbc.Driver" );
cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/jdbc?useSSL=false" ); 
cpds.setUser("root");
cpds.setPassword("qaz12345"); 
Connection connection = cpds.getConnection();

通过ComboPooledDataSource(String configName)构造方法创建数据源对象,需要读取c3p0-config.xml配置文件中的数据。不需要手动配置。

ComboPooledDataSource cpds = new ComboPooledDataSource("dev"); 
Connection connection = cpds.getConnection();

c3p0-config.xml配置文件

<c3p0-config>
  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>
    <property name="user">root</property>
    <property name="password">qaz12345</property>
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
  </default-config>
  <named-config name="dev">
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc?useSSL=false</property>
  </named-config>
</c3p0-config>

当ComboPooledDataSource()中加入dev时,表示name=”dev”的named-config标签内部内容替换c3p0-config.xml配置文件中的默认设置,当ComboPooledDataSource()中无值传入,默认为default-config标签中设置。

Druid:

Druid号称是Java语言中最好的数据库连接池,能够提供强大的监控和扩展功能。下面Druid的使用做一些简单的介绍:

当Druid连接池没有准备配置文件的时候,通过以下方式设置其中的配置属性:

DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/jdbc");
dataSource.setUsername("root");
dataSource.setPassword("qaz12345");
Connection connection = dataSource.getConnection();

当Druid连接池准备有配置文件的时候(例如配置文件为alibaba.properties),通过以下方式读取配置文件中的配置属性:

Properties properties = new Properties();   properties.load(DruidDemo2.class.getClassLoader().getResourceAsStream("alibaba.properties"));
DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();

alibaba.properties的配置文件

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false
username=root
password=qaz12345
filters=stat
initialSize=2
maxActive=300
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=false
maxPoolPreparedStatementPerConnectionSize=200

注意:
dataSource数据源是DataSource 类的,但是在关闭数据源的时候DataSource 类并没有提供.close()方法,所以我们可以采用下面的方法解决:

DruidDataSource dataSource =(DruidDataSource) DruidDataSourceFactory.createDataSource(properties);

强转之后DruidDataSource 类的dataSource 就有了.close()方法,便可以正常关闭了。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值