实现:
标准:接口DataSource javax.sql报下。
方法:
获取连接 :getConnction();
归还连接 :Connection.close()。如果连接对象是从池中获取的,那么调用Connection.close()方法,则不会在关闭连接了而是归还连接
一般我们不去实现它,有数据库厂来实现
c3p0 :数据库连接池技术
Druid : 数据库连接池技术有阿里巴巴提供
数据库连接池基本使用
c3p0连接池技术:
步骤:
导入jar包(连个) c3p0的依赖
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
不要忘记导入数据库驱动包
定义配置文件
名称:c3p0.properties 或者c3p0-Config.xml
路径:直接将文件存放在src目录下即可
创建核心对象 数据库连接池对象 ComboPooledDataSouce
获取连接: getConnection
java代码
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/a2</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 连接池参数 -->
<!--初始化申请的连接数量-->
<property name="initialPoolSize">5</property>
<!--最大的连接数量-->
<property name="maxPoolSize">10</property>
<!--超时时间-->
<property name="checkoutTimeout">3000</property>
</default-config>
<named-config name="otherc3p0">
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/kk</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>
public static void main(String[] args) throws SQLException {
DataSource ds = new ComboPooledDataSource();
Connection connection = ds.getConnection();
System.out.println(connection);
}

搜索
复制
原创文章,作者:wdmbts,如若转载,请注明出处:https://blog.ytso.com/tech/bigdata/277525.html