编写一个基本的连接池来实现连接的复用&一些工程细节上的优化

时间:2023-03-08 17:26:28
编写一个基本的连接池来实现连接的复用&一些工程细节上的优化
 package it.cast.jdbc;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList; public class MyDataSource { private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String username = "root";
private static String password = "123"; private static int initCount = 5;
private static int maxCount = 10;
private int currentCount = 0; private LinkedList<Connection> connectionPool = new LinkedList<Connection>(); public MyDataSource() {
try {
for (int i = 0; i < initCount; i++) { this.connectionPool.addLast(this.createConnection()); this.currentCount++;
}
} catch (SQLException e) {
throw new ExceptionInInitializerError(e);
} } public Connection getConnection() throws SQLException {
synchronized (connectionPool) {
if (this.connectionPool.size() > 0) {
return this.connectionPool.removeFirst();
}
if (this.currentCount < maxCount) {
this.currentCount++;
return this.createConnection();
}
throw new SQLException("NO Connection!!");
} } public void free(Connection conn) {
this.connectionPool.addLast(conn);
} private Connection createConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
}

MyDataSource

 package it.cast.jdbc;

 import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class jdbcUtils { private static String url = "jdbc:mysql://localhost:3306/jdbc?generateSimpleParameterMetadata=true";
private static String user = "root";
private static String password = "123"; private static MyDataSource myDataSource = null; private jdbcUtils() { } static {
try {
Class.forName("com.mysql.jdbc.Driver");
myDataSource =new MyDataSource();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} public static Connection getConnection() throws SQLException {
return myDataSource.getConnection();
} public static void free(ResultSet rs, Statement st, Connection conn) { try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (conn != null)
//conn.close();
myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
} } } public static void free(ResultSet rs, PreparedStatement ps, Connection conn) { try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (conn != null)
//conn.close();
myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
} } }
public static void free(ResultSet rs, CallableStatement cs, Connection conn) { try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (cs != null)
cs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (conn != null)
//conn.close();
myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
} } }
}

jdbcUtils

 package it.cast.jdbc;

 import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class Base { static Connection conn = null; public static void main(String[] args) throws SQLException, ClassNotFoundException {
for (int i = 0; i < 20; i++) {
Connection conn = jdbcUtils.getConnection();
System.out.println(conn);
//jdbcUtils.free(null, null, conn);
}
} static void test() throws SQLException, ClassNotFoundException { // 2.建立连接
conn = jdbcUtils.getConnection(); // 3.创建语句
Statement st = conn.createStatement(); // 4.执行语句
ResultSet rs = st.executeQuery("select * from user"); // 5.处理结果
while (rs.next()) {
System.out.println(rs.getObject(1) + "\t" + rs.getObject(2) + "\t"
+ rs.getObject(3)+"\t" + rs.getObject(4));
} //6.释放资源
jdbcUtils.free(rs, st, conn);
} }

Base