c3p0的几种使用方式(原文地址: https://my.oschina.net/liangtee/blog/101047)
package com.c3p0.test; import java.sql.Connection;
import java.sql.SQLException;
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.ComboPooledDataSource; public class DBPool {
private static DBPool dbPool;
private ComboPooledDataSource dataSource; static {
dbPool = new DBPool();
} public DBPool() {
try {
dataSource = new ComboPooledDataSource();
dataSource.setUser("id");
dataSource.setPassword("pw");
dataSource.setJdbcUrl(
"jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=GB2312");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setInitialPoolSize(2);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
} public final static DBPool getInstance() {
return dbPool;
} public final Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("无法从数据源获取连接", e);
}
} public static void main(String[] args) throws SQLException {
Connection con = null;
try {
con = DBPool.getInstance().getConnection();
} catch (Exception e) {
} finally {
if (con != null)
con.close();
}
} }
以上是直接在combopooleddatasource里面直接设置属性值
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="user">test</property>
<property name="password">test</property>
<property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="driverClass">oracle.jdbc.OracleDriver</property>
<property name="automaticTestTable">con_test</property>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property> <user-overrides user="test-user">
<property name="maxPoolSize">10</property>
<property name="minPoolSize">1</property>
<property name="maxStatements">0</property>
</user-overrides>
</default-config> <!-- This app is massive! -->
<named-config name="intergalactoApp">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property> <!-- intergalactoApp adopts a different approach to configuring statement
caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config> <named-config name="userApp">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">20</property>
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config>
</c3p0-config>
package com.c3p0.test; import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DbConnection {
private static DataSource dataSource;
static {
dataSource = new ComboPooledDataSource("userApp");
} public static Connection getConnectioon() throws SQLException {
return dataSource.getConnection();
}
}
以上是使用配置文件的形式
package com.c3p0.test; import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties; import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources; public final class ConnectionManager {
private static ConnectionManager instance; public ComboPooledDataSource ds;
private static String c3p0Properties = "c3p0.properties"; private ConnectionManager() throws Exception {
Properties p = new Properties();
p.load(this.getClass().getResourceAsStream(c3p0Properties));
ds = new ComboPooledDataSource();
ds.setUser(p.getProperty("user"));
ds.setPassword(p.getProperty("password"));
ds.setJdbcUrl(p.getProperty("jdbcUrl"));
ds.setDriverClass(p.getProperty("driverClass"));
ds.setInitialPoolSize(Integer.parseInt(p.getProperty("initialPoolSize")));
ds.setMinPoolSize(Integer.parseInt(p.getProperty("minPoolSize")));
ds.setMaxPoolSize(Integer.parseInt(p.getProperty("maxPoolSize")));
ds.setMaxStatements(Integer.parseInt(p.getProperty("maxStatements")));
ds.setMaxIdleTime(Integer.parseInt(p.getProperty("maxIdleTime")));
} public static final ConnectionManager getInstance() {
if (instance == null) {
try {
instance = new ConnectionManager();
} catch (Exception e) {
e.printStackTrace();
}
}
return instance;
} public synchronized final Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
} protected void finalize() throws Throwable {
DataSources.destroy(ds); // 关闭datasource
super.finalize();
}
}
以上使用properties属性文件设置属性
c3p0的几种使用方式(原文地址: https://my.oschina.net/liangtee/blog/101047)的更多相关文章
- 【c3p0】 C3P0的三种配置方式以及基本配置项详解
数据库连接池C3P0框架是个非常优异的开源jar,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理. ---------------------------------------- ...
- spring 2种下载方式 下载地址 download 地址
spring 在官网只提供 maven 的下载方式,把zip方式的不再提供,两种方法下载: 1.想找回以前版本的spring zip包,如果知道版本号,那么直接在google里输入 ” spring ...
- JGit与远程仓库链接使用的两种验证方式(ssh和https)
JGit是使用JAVA的API来操控Git仓库的库,由Eclipse公司维护.他提供的API分成两个层次,底层命令和高层命令.底层API是直接作用于低级的仓库对象,高层的API是一个面向普通用户级别功 ...
- Windows Server 2008 R2 /2012 修改密码策略(摘抄 原文地址 https://www.cnblogs.com/mili3/p/7799347.html)
今天建了域环境,在添加新用户的时候,发现用简单的密码时域安全策略提示密码复杂度不够,于是我就想在域安全策略里面把密码复杂度降低一点. 问题: 在“管理工具 >> 本地安全策略 > ...
- nmap使用命令(转载)原文地址https://www.jianshu.com/p/4030c99fcaee
- 转 VMware虚拟机三种联网方式(图文详细解说)
原文地址https://blog.csdn.net/lucienduan/article/details/38233147 VMware三种网络模式联网 首先说一下VMware的几个虚拟设备 安装了V ...
- 魔改swagger:knife4j的另外一种打开方式
之前公司使用了swagger作为文档管理工具,原生的swagger-ui非常丑,之后就用了开源项目 萧明 / knife4j 的swagger组件进行了swagger渲染,改造之后界面漂亮多了,操作也 ...
- Ajax的get和post两种请求方式区别
Ajax的get和post两种请求方式区别 (摘录):http://ip-10000.blog.sohu.com/114437748.html 解get和post的区别. 1. get是把参数数据队列 ...
- iOS活动倒计时的两种实现方式
代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...
随机推荐
- 42.纯 CSS 创作一个均衡器 loader 动画
原文地址: https://segmentfault.com/a/1190000015157160 感想: 不难,最简单的动画.拓展地址: https://scrimba.com/c/cWqVv9hd ...
- spark sql 中的结构化数据
1. 连接mysql 首先需要把mysql-connector-java-5.1.39.jar 拷贝到 spark 的jars目录里面: scala> import org.apache.spa ...
- kubectl version报did you specify the right host or port
现象: [root@localhost shell]# kubectl version Client Version: version.Info{Major:", GitVersion:&q ...
- DirectShow设置采集帧率码率YUV<转>
// 设置参数,p1=宽,p2=高,p3=帧率 AM_MEDIA_TYPE *p = NULL; IAMStreamConfig *pSC = NULL; pCGB2->FindInterfac ...
- ABAP-语法检查
1.界面 2.代码 *&---------------------------------------------------------------------* *& Report ...
- Android Studio安装插件提示was not installed: Cannot download的解决
https://blog.csdn.net/xiayiye5/article/details/80510910 2.[Android Studio安装部署系列]三十二.Android模拟器Genymo ...
- ubuntu oracle 环境搭建
安装 Oracle SQL Developer Oracle客户端安装 https://oracle.github.io/odpi/doc/installation.html#linux
- 2018SDIBT_国庆个人第一场
A - Turn the Rectangles CodeForces - 1008B There are nn rectangles in a row. You can either turn eac ...
- ACM__队列
今天学回顾bfs的时候遇到了,遂总结一下 队列是一种特殊的线性表,只允许在队列的前端(front)进行删除操作,在队尾进行插入操作,进行插入操作的端称作队尾,进行删除操作的端称作对头.(来自百度百科) ...
- 实现一个简单的shell
使用已学习的各种C函数实现一个简单的交互式Shell,要求:1.给出提示符,让用户输入一行命令,识别程序名和参数并调用适当的exec函数执行程序,待执行完成后再次给出提示符.2.该程序可识别和处理以下 ...