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()方法,便可以正常关闭了。