首页 > 代码库 > 编写一个基本的连接池来实现连接的复用&一些工程细节上的优化
编写一个基本的连接池来实现连接的复用&一些工程细节上的优化
1 package it.cast.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 import java.util.LinkedList; 7 8 public class MyDataSource { 9 10 private static String url = "jdbc:mysql://localhost:3306/jdbc";11 private static String username = "root";12 private static String password = "123";13 14 private static int initCount = 5;15 private static int maxCount = 10;16 private int currentCount = 0;17 18 private LinkedList<Connection> connectionPool = new LinkedList<Connection>();19 20 public MyDataSource() {21 try {22 for (int i = 0; i < initCount; i++) {23 24 this.connectionPool.addLast(this.createConnection());25 26 this.currentCount++;27 }28 } catch (SQLException e) {29 throw new ExceptionInInitializerError(e);30 }31 32 }33 34 public Connection getConnection() throws SQLException {35 synchronized (connectionPool) {36 if (this.connectionPool.size() > 0) {37 return this.connectionPool.removeFirst();38 }39 if (this.currentCount < maxCount) {40 this.currentCount++;41 return this.createConnection();42 }43 throw new SQLException("NO Connection!!");44 }45 46 }47 48 public void free(Connection conn) {49 this.connectionPool.addLast(conn);50 }51 52 private Connection createConnection() throws SQLException {53 return DriverManager.getConnection(url, username, password);54 }55 }
1 package it.cast.jdbc; 2 3 import java.sql.CallableStatement; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 11 public class jdbcUtils { 12 13 private static String url = "jdbc:mysql://localhost:3306/jdbc?generateSimpleParameterMetadata=http://www.mamicode.com/true"; 14 private static String user = "root"; 15 private static String password = "123"; 16 17 private static MyDataSource myDataSource = null; 18 19 private jdbcUtils() { 20 21 } 22 23 static { 24 try { 25 Class.forName("com.mysql.jdbc.Driver"); 26 myDataSource =new MyDataSource(); 27 } catch (ClassNotFoundException e) { 28 e.printStackTrace(); 29 } 30 } 31 32 public static Connection getConnection() throws SQLException { 33 return myDataSource.getConnection(); 34 } 35 36 public static void free(ResultSet rs, Statement st, Connection conn) { 37 38 try { 39 if (rs != null) 40 rs.close(); 41 } catch (SQLException e) { 42 e.printStackTrace(); 43 } finally { 44 45 try { 46 if (st != null) 47 st.close(); 48 } catch (SQLException e) { 49 e.printStackTrace(); 50 } finally { 51 52 try { 53 if (conn != null) 54 //conn.close(); 55 myDataSource.free(conn); 56 } catch (Exception e) { 57 e.printStackTrace(); 58 } 59 } 60 61 } 62 63 } 64 65 public static void free(ResultSet rs, PreparedStatement ps, Connection conn) { 66 67 try { 68 if (rs != null) 69 rs.close(); 70 } catch (SQLException e) { 71 e.printStackTrace(); 72 } finally { 73 74 try { 75 if (ps != null) 76 ps.close(); 77 } catch (SQLException e) { 78 e.printStackTrace(); 79 } finally { 80 81 try { 82 if (conn != null) 83 //conn.close(); 84 myDataSource.free(conn); 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 } 89 90 } 91 92 } 93 public static void free(ResultSet rs, CallableStatement cs, Connection conn) { 94 95 try { 96 if (rs != null) 97 rs.close(); 98 } catch (SQLException e) { 99 e.printStackTrace();100 } finally {101 102 try {103 if (cs != null)104 cs.close();105 } catch (SQLException e) {106 e.printStackTrace();107 } finally {108 109 try {110 if (conn != null)111 //conn.close();112 myDataSource.free(conn);113 } catch (Exception e) {114 e.printStackTrace();115 }116 }117 118 }119 120 }121 }
1 package it.cast.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class Base { 9 10 static Connection conn = null;11 12 public static void main(String[] args) throws SQLException, ClassNotFoundException {13 for (int i = 0; i < 20; i++) {14 Connection conn = jdbcUtils.getConnection();15 System.out.println(conn);16 //jdbcUtils.free(null, null, conn);17 }18 }19 20 static void test() throws SQLException, ClassNotFoundException {21 22 23 // 2.建立连接24 conn = jdbcUtils.getConnection();25 26 // 3.创建语句27 Statement st = conn.createStatement();28 29 // 4.执行语句30 ResultSet rs = st.executeQuery("select * from user");31 32 // 5.处理结果33 while (rs.next()) {34 System.out.println(rs.getObject(1) + "\t" + rs.getObject(2) + "\t"35 + rs.getObject(3)+"\t" + rs.getObject(4));36 }37 38 //6.释放资源39 jdbcUtils.free(rs, st, conn);40 }41 42 }
编写一个基本的连接池来实现连接的复用&一些工程细节上的优化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。