连接池的作用:
初始化一些个连接,每次需要时从空闲池中获取,用完再归还池中,
不用频繁的创建连接,减少系统资源,连接池大小事宜,减少内存开销。
连接池大致内容:
1.有两个池:一个活动池(正在使用的连接),一个空闲池(空闲的连接),空闲池初始化池确定初始大小(initPoolSize)。活动池确定最大活跃连接数(maxPoolSize)。
2.空闲池封装了LinkedList集合(适合增删)类型的连接,当客户需要连接时判断空闲池中是否有连接,有连接则拿到dataSource.getConnection(); 空闲池从头开始拿(LinkedList.removeFirst(),并把他放到活动池的尾部linkedList.add();
3.如果客户端获取连接时,空闲池没有连接了,判断活动池是否到达最大连接:3.1.没有到,先创建连接,放活动池内使用。
3.2.已经到了最大连接数,进行等待,当等待超时:思路1:直接报错;思路2:将活动池里最久的销毁掉(也就是最开始的一个:尾部添加的原因).
4.连接使用完毕策略:归还到空闲池。
使用第三方连接池:
环境:
1.jar包
2.将配置文件标记成类路径
其中c3p0的配置文件时properties;德鲁伊的配置文件时xml
一.C3P0连接池
配置文件:c3p0-config.xml
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day20</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>
</c3p0-config>
c3p0_demo.java
import com.mchange.v2.c3p0.ComboPooledDataSource;
import pojo.user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
/**
* @Author:ZZZ
* @Date: 2020/11/21 17:14
* @Version 1.0
*/
public class c3p0_demo {
public static void main(String[] args) throws Exception{
//创建连接池
ComboPooledDataSource dataSource =new ComboPooledDataSource();
//会自动去类路径下查找配置文件,如果硬编码写死了,代码变少了但是没有那么灵活
//获得连接
Connection connection = dataSource.getConnection();
String sql="select * from user ";
PreparedStatement pstm = connection.prepareStatement(sql);
ResultSet resultSet = pstm.executeQuery();
//集合得javabean
List<user> list =new ArrayList<>();
user user =null;
while (resultSet.next()) {
user=new user();//在循环里新建user对象,不然操作得都是一个对象
user.setId(resultSet.getInt("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setNickname(resultSet.getString("nickname"));
list.add(user);
}
System.out.println(list);
resultSet.close();
pstm.close();
connection.close();
}
}
代码增删改类似
@Test
public void insert()throws Exception
{
ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();
Connection connection = comboPooledDataSource.getConnection();
String sql="insert into user (username,password,nickname) values(?,?,?)";
PreparedStatement pstm = connection.prepareStatement(sql);
pstm.setString(1,"axxx");
pstm.setString(2,"123456");
pstm.setString(3,"axxxxxa");
pstm.executeUpdate();
//最后记得关闭资源,关闭策略:后创建的先关闭,连接池一般不关
pstm.close();
connection.close();
}
二.德鲁伊连接池:
配置文件:druid.properties
url=jdbc:mysql://localhost:3306/day20
username=root
password=root
driverClassName=com.mysql.jdbc.Driver
initialSize=8
maxActive=10
maxWait=2000
druid_demo.java
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;
import pojo.user;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
/**
* @Author:ZZZ
* @Date: 2020/11/21 17:45
* @Version 1.0
*/
public class druid_demo {
@Test
public void select() throws Exception{
// DruidDataSource dataSource=new DruidDataSource();
//druid可以灵活得加载配置文件加载配置文件
Properties properties = new Properties();
//当前类名获得类得加载器再获得配置文件得路径
InputStream is = druid_demo.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(is);
// 另一个加载路径得方式创建连接池
DruidDataSource dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
// 3.获得连接
Connection connection = dataSource.getConnection();
// 4.书写sql语句,预编译sql语句,得到预编译对象
String sql = "select * from user where id = ?";
PreparedStatement ps = connection.prepareStatement(sql);
// 5.设置参数
ps.setInt(1, 3);
// 6.执行sql语句
ResultSet resultSet = ps.executeQuery();
// 封装,处理数据
user user = null;
while (resultSet.next()) {
user = new user();
user.setId(resultSet.getInt("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setNickname(resultSet.getString("nickname"));
}
System.out.println(user);
}
@Test
public void insert() throws Exception
{
Properties properties=new Properties();
InputStream resourceAsStream =druid_demo.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(resourceAsStream);
DruidDataSource dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
DruidPooledConnection connection = dataSource.getConnection();
String sql="insert into user (username,password,nickname) values(?,?,?)";
PreparedStatement pstm = connection.prepareStatement(sql);
pstm.setString(1,"张无忌");
pstm.setString(2,"123");
pstm.setString(3,"zwj");
pstm.executeUpdate();
pstm.close();
connection.close();
}
@Test
public void delete() throws Exception
{
Properties properties=new Properties();
InputStream resourceAsStream = druid_demo.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(resourceAsStream);
DruidDataSource dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
DruidPooledConnection connection = dataSource.getConnection();
String sql="delete from user where id=?";
PreparedStatement pstm = connection.prepareStatement(sql);
pstm.setString(1,"4");
pstm.executeUpdate();
pstm.close();
connection.close();
}
@Test
public void update()
{
}
}