配置文件:
#驱动路径
driver=com.mysql.cj.jdbc.Driver
#JDBC连接URL
url=jdbc:mysql://localhost:3306/sqldemo?useSSL=false&serverTimezone=UTC
#账号
username=root
#密码
password=123456
#初始连接池大小
initPoolSize=3
#最大空闲时间
maxIdleTime=20
#最长连接等待时间
maxCreateTime=20
#最大连接池数
maxPoolSize=6
连接池代码:
package com.JDBC.CreateConnectionPool;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.logging.Logger;
class run implements Runnable{
private DataSourceDemo dataSourceDemo;
public run(DataSourceDemo dataSourceDemo){
this.dataSourceDemo=dataSourceDemo;
}
@Override
public void run() {
dataSourceDemo.release();
}
}
public class DataSourceDemo implements DataSource{
private String driverclassName;
private String url;
private String username;
private String password;
private int initialSize;
private long maxWait;
private long maxCreateTime;
private int maxActive;
private ArrayList<Connection> list;
private int size;
private long lastTime;
public DataSourceDemo(){
Thread thread=new Thread(new run(this));
thread.start();
}
public void release(){
for (; ; ) {
if(this.size>this.initialSize&&(System.currentTimeMillis()>this.lastTime+this.maxWait)){
synchronized (list){
if(!list.isEmpty()){
try {
list.remove(0).close();
this.size--;
this.lastTime=System.currentTimeMillis();
System.out.println("当前剩余连接数1 "+this.size);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
public synchronized void release(Connection connection) throws SQLException {
if(this.size>this.initialSize&&(System.currentTimeMillis()>this.lastTime+this.maxWait)){
connection.close();
this.size--;
System.out.println("当前剩余连接数:"+(this.size));
}
if(null!=connection){
list.add(connection);
}
}
@Override
public Connection getConnection() throws SQLException {
if(this.list==null){
init();
}else{
long future=System.currentTimeMillis()+this.maxCreateTime;
long remaining=this.maxCreateTime;
while(list.isEmpty()&&remaining>0){
createConnection();
remaining=future-System.currentTimeMillis();
}
}
if(!list.isEmpty()){
synchronized (list){
if(!list.isEmpty()){
this.lastTime=System.currentTimeMillis();
System.out.println("当前剩余连接数 get:"+(this.size));
return list.remove(0);
}
}
}
this.lastTime=System.currentTimeMillis();
return null;
}
private synchronized void createConnection() throws SQLException {
if(this.list==null){
list=new ArrayList<>(maxActive);
}
try {
if(this.size<this.maxActive){
Class.forName(driverclassName);
list.add(DriverManager.getConnection(url,username,password));
this.size++;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void init() throws SQLException {
if(initialSize>0&&initialSize<=maxActive){
for (int i = 0; i < initialSize; i++) {
createConnection();
}
}
}
public void setMaxCreateTime(long maxCreateTime) {
this.maxCreateTime = maxCreateTime;
}
public void setDriverclassName(String driverclassName) {
this.driverclassName = driverclassName;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setInitialSize(int initialSize) {
this.initialSize = initialSize;
}
public void setMaxWait(long maxWait) {
this.maxWait = maxWait;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return null;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
}
@Override
public int getLoginTimeout() throws SQLException {
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
模拟连接代码:
package com.JDBC.CreateConnectionPool;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
class rundemo implements Runnable{
DataSourceDemo dataSource;
public rundemo(DataSourceDemo dataSource){
this.dataSource=dataSource;
}
@Override
public void run() {
for (int i = 0; i < 1; i++) {
Connection connection= null;
try {
while(connection==null){
connection = dataSource.getConnection();
}
String sql="select * from Student,SC where Student.SID=SC.SID and Student.Ssex=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,"男");
ResultSet resultSet=statement.executeQuery();
while(resultSet.next()){
String name=resultSet.getString("Sname");
int age=resultSet.getInt("Sage");
int score=resultSet.getInt("score");
}
Thread.sleep(20);
statement.close();
dataSource.release(connection);
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class DataSourcesDemoTest {
public static void main(String[] args) {
Properties properties=new Properties();
try {
properties.load(DataSourcesDemoTest.class.getClassLoader().getResourceAsStream("datasourceconfig.properties"));
final DataSourceDemo dataSource=new DataSourceDemo();
dataSource.setDriverclassName(properties.getProperty("driver"));
dataSource.setUrl(properties.getProperty("url"));
dataSource.setUsername(properties.getProperty("username"));
dataSource.setPassword(properties.getProperty("password"));
dataSource.setInitialSize(Integer.parseInt(properties.getProperty("initPoolSize")));
dataSource.setMaxWait(Long.parseLong(properties.getProperty("maxIdleTime")));
dataSource.setMaxCreateTime(Long.parseLong(properties.getProperty("maxCreateTime")));
dataSource.setMaxActive(Integer.parseInt(properties.getProperty("maxPoolSize")));
for (int i = 0; i <8; i++) {
new Thread(new rundemo(dataSource)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试结果:
当前剩余连接数 get:3
当前剩余连接数 get:4
当前剩余连接数 get:5
当前剩余连接数 get:6
当前剩余连接数 get:6
当前剩余连接数 get:6
当前剩余连接数 get:6
当前剩余连接数 get:6
当前剩余连接数 get:6
当前剩余连接数 get:6
当前剩余连接数:5
当前剩余连接数:4
当前剩余连接数:3