Java中使用C3P0连接池
先看官网给的范例:
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.DataSources; /**
* This example shows how to acquire a c3p0 DataSource and
* bind it to a JNDI name service.
*/
public final class JndiBindDataSource
{
// be sure to load your database driver class, either via
// Class.forName() [as shown below] or externally (e.g. by
// using -Djdbc.drivers when starting your JVM).
static
{
try
{ Class.forName( "com.mysql.jdbc.Driver" ); }
catch (Exception e)
{ e.printStackTrace(); }
} public static void main(String[] argv)
{
try
{
// let a command line arg specify the name we will
// bind our DataSource to.
String jndiName = argv[0]; // acquire the DataSource using default pool params...
// this is the only c3p0 specific code here
DataSource unpooled = DataSources.unpooledDataSource("jdbc:mysql://127.0.0.1:3306/gpsdata",
"root",
"root");
DataSource pooled = DataSources.pooledDataSource( unpooled ); // Create an InitialContext, and bind the DataSource to it in
// the usual way.
//
// We are using the no-arg version of InitialContext's constructor,
// therefore, the jndi environment must be first set via a jndi.properties
// file, System properties, or by some other means.
InitialContext ctx = new InitialContext();
ctx.rebind( jndiName, pooled );
System.out.println("DataSource bound to nameservice under the name \"" +
jndiName + '\"');
}
catch (Exception e)
{ e.printStackTrace(); }
} static void attemptClose(ResultSet o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
} static void attemptClose(Statement o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
} static void attemptClose(Connection o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
} private JndiBindDataSource()
{}
}
1.建立 com.mchange.v2.c3p0.ComboPooledDataSource
这是一个JavaBean,在使用前应设置它的jdbcURL、user、password和driverClass。其他参数参考configuration properties
- ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass( "org.postgresql.Driver" );
- cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" ); cpds.setUser("swaldman");
- cpds.setPassword("test-password");
- // 下面的设置是可选的,c3p0可以在默认条件下工作,也可以设置其他条件
- cpds.setMinPoolSize(5);
- cpds.setAcquireIncrement(5);
- cpds.setMaxPoolSize(20);
也可以使用命名Configuration
- ComboPooledDataSource cpds = new ComboPooledDataSource("intergalactoApp");
2.使用工厂类com.mchange.v2.c3p0.DataSources
com.mchange.v2.c3p0.DataSources 可以按照传统的JDBC驱动建立一个无连接池的DataSource,然后转化为连接池的DataSource。
- DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
- DataSource ds_pooled = DataSources.pooledDataSource( ds_unpooled );
- // 此时DataSource已经可以使用,但应该显示的设置driver Class
- Class.forName("org.postgresql.Driver");
如果想设置其中的参数,可以将参数放入一个Map中
- DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
- Map overrides = new HashMap();
- overrides.put("maxStatements", "200"); overrides.put("maxPoolSize", new Integer(50));
- //建立一个包括默认值和设置值的PooledDataSource
- ds_pooled = DataSources.pooledDataSource( ds_unpooled, overrides );
如果使用命名的Configuration,可以如下
- ds_pooled = DataSources.pooledDataSource( ds_unpooled, "intergalactoAppConfig", overrides );
销毁DataSource有两种方式
DataSource.destroy()方式
- DataSource ds_pooled = null;
- try {
- DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
- ds_pooled = DataSources.pooledDataSource( ds_unpooled );
- // 下面正常使用DataSource...
- }
- finally {
- DataSources.destroy( ds_pooled );
- }
另一种是PooledDataSource 接口提供的close() 方法
- static void cleanup(DataSource ds) throws SQLException {
- // 确定是否为c3p0的PooledDataSource
- if ( ds instanceof PooledDataSource) {
- PooledDataSource pds = (PooledDataSource) ds;
- pds.close();
- } else
- System.err.println("Not a c3p0 PooledDataSource!");
- }
Java中使用C3P0连接池的更多相关文章
- Spring框架中 配置c3p0连接池 完成对数据库的访问
开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...
- JAVA中事物以及连接池
一.事物 什么是事物? 事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元.这些单元要么全都成功,要么全都不成功. 做一件事情,这个一件事情中有多个 ...
- java学习笔记—c3p0连接池与元数据分析(42)
第一步:导入c3p0包 第二步:在classpath目录下,创建一个c3p0-config.xml <?xml version="1.0" encoding="UT ...
- Spring框架中 配置c3p0连接池
开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...
- Hibernate的配置中,c3p0连接池相关配置
一.配置c3p0 1.导入 hibernate-c3po连接池包,Maven地址是:http://mvnrepository.com/artifact/org.hibernate/hibernate- ...
- C3P0连接池在hibernate和spring中的配置
首先为什么要使用连接池及为什么要选择C3P0连接池,这里就不多说了,目前C3P0连接池还是比较方便.比较稳定的连接池,能与spring.hibernate等开源框架进行整合. 一.hibernate中 ...
- Spring之c3p0连接池配置和使用
1.导入包:c3p0和mchange包 2.代码实现方式: package helloworld.pools; import com.mchange.v2.c3p0.ComboPooledDataSo ...
- C3P0连接池工具类实现步骤及方法
C3P0连接池的工具类 使用C3P0获得连接对象连接池有一个规范接口 javax.sal.DataSourse 接口定义了一个从连接池中获得连接的方法getConnection(); 步骤导入jar包 ...
- (七)Spring 配置 c3p0 连接池
目录 在 Spring 核心配置文件中配置 c3p0 连接池 配置 JdbcTemplate 对象 在 service 层注入 userDao 在 UserDao 里面注入 JdbcTemplate ...
随机推荐
- UNIX环境高级编程——单实例的守护进程
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h&g ...
- shell的数值计算,小数计算
shell脚本中,可以进行数值计算, 如加减乘除,通过expr.let.(())等完成,文章介绍:http://blog.csdn.net/longshenlmj/article/details/14 ...
- CUDA5.5 的环境变量设置
为了方便,我写了这个文件用于设置cuda5.5的环境变量. 其中有些环境变量可能用不到,大家根据需要修改就是了. export CUDA_HOME=/usr/local/cuda-5.5 export ...
- 05 Activity生命周期
生命周期:一个Activity从创建到销毁经过的全部方法 1.onCreate() 创建一个Activity的时候执行的方法 2.onStart()Activity可以被看见到时候无法交互(没有焦点) ...
- Hadoop:Hadoop基本命令
http://blog.csdn.net/pipisorry/article/details/51223877 常用命令 启用hadoop start-dfs.sh start-hbase.sh 停止 ...
- Swift基础之显示波纹样式
最近项目用到了蓝牙连接,搜索设备的内容,其中需要搜索过程中出现波纹的动画效果,在这里将项目中OC语言编写的这种动画效果,转换成Swift编写,下面简单介绍说明一下代码. 这里用到了两种方法实现波纹效果 ...
- iOS积分抽奖Demo,可以人为控制不同奖项的得奖率
最近公司让写一个转盘积分抽奖的样式,所以把创建过程中的心得记录一下,给大家分享 首先创建了相关的图片转盘,指针图片,然后就是考虑转盘如何旋转的问题,我是通过给指针图片添加一个动画效果,从而实现旋转效果 ...
- React Native入门教程 1 -- 开发环境搭建
有人问我为啥很久不更新博客..我只能说在学校宿舍真的没有学习的环境..基本上在宿舍里面很颓废..不过要毕业找工作了,我要渐渐把这个心态调整过来,就从react-native第一篇博客开始.话说RN也出 ...
- 使用C++的string实现高精度加法运算
对于超大数字的运算,用long long int仍然不能解决,这时候就需要考虑通过模拟运算和数组存储来实现高精度运算. 本文讨论借助C++的string来实现高精度的运算. 首先输入的量直接存储为st ...
- Synchronize 和 Lock 的区别与用法
一.synchronized和lock的用法区别 (1)synchronized(隐式锁):在需要同步的对象中加入此控制,synchronized可以加在方法上,也可以加在特定代码块中,括号中表示需要 ...