主要分为三大块,第一部分是配置文件信息。
driver.properties文件信息
driver=com.mysql.jdbc.Driver url=jdbc:mysql://192.168.30.130:3306/mydemo username=root password=ok init=10 maxsize=100 maxwait=500 validationQuery=select 1
第二块内容是连接的实体类
import java.sql.Connection; @Data @AllArgsConstructor @NoArgsConstructor @Builder public class ConnectionEntity { private Connection con; //连接 private boolean isBusy; //判断当前连接是否在忙 }
第三块是主题代码实现,包含创建连接,获得连接,释放连接
package com.kgc.mymysql.dao; import java.io.FileInputStream; import java.io.IOException; import java.net.URLDecoder; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Properties; public class NormalLinkedPool { public static Connection getConnection(){ return Pool.getConnection(); } public static void destory(Connection connection){ Pool.destoryConnection(connection); } private static class Pool{ private static String DRIVER; private static String USERNAME; private static String PASSWORD; private static String URL; private static int INIT; private static int MAXSIZE; private static Long MAXWAIT; private static String VALIDATIONQUERY; //连接池,管理链接的 private static List<ConnectionEntity> pools=new ArrayList<>(); //制造链接 static { //读取属性文件 获得配置 loadProperties(); //System.out.println(URL); for (int i=0;i<=INIT;i++){ pools.add(buildConnection()); } } private static void loadProperties(){ try { String path= URLDecoder.decode(NormalLinkedPool.class.getResource("/driver.properties").getPath(),"utf-8"); Properties p=new Properties(); p.load(new FileInputStream(path)); DRIVER=p.getProperty("driver"); URL=p.getProperty("url"); USERNAME=p.getProperty("username"); PASSWORD=p.getProperty("password"); INIT=Integer.parseInt(p.getProperty("init")); MAXSIZE=Integer.parseInt(p.getProperty("maxsize")); MAXWAIT=Long.parseLong(p.getProperty("maxwait")); VALIDATIONQUERY =p.getProperty("validationQuery"); } catch (IOException e) { e.printStackTrace(); } } //创建链接 private static ConnectionEntity buildConnection(){ Connection conn=null; try { Class.forName(DRIVER); conn= DriverManager.getConnection(URL,USERNAME,PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return ConnectionEntity.builder().con(conn).build(); } //用户获取链接方法 public static Connection getConnection(){ //在POOLS里面先查询是否有空闲的链接,如果有直接给用户 for (ConnectionEntity connectionEntity:pools){ if (!connectionEntity.isBusy()){ connectionEntity.setBusy(true); return connectionEntity.getCon(); } } // 如果在上一步中发现没有空闲的链接,就用当前的链接个数和最大链接个数比较 //如果当前链接小于最大链接数,那么就新创建一个链接发送给用户,并把这个链接 //存放在pool池里 if (pools.size()<MAXSIZE){ ConnectionEntity conn = buildConnection(); conn.setBusy(true); pools.add(conn); return conn.getCon(); } //如果没有空闲链接,并且链接数达到了最大值。则用户等待,等待时间自己设 try { Thread.sleep(MAXWAIT); } catch (InterruptedException e) { e.printStackTrace(); } return getConnection(); } //用户释放链接的方法 public static void destoryConnection(Connection connection){ // 检查用户链接是否良好 try { PreparedStatement psata=connection.prepareStatement(VALIDATIONQUERY); psata.executeQuery(); //如果没有出现一场就继续下面的工作,将用户归还的链接设置为空闲 for (ConnectionEntity connectionEntity:pools){ if (connectionEntity.getCon()==connection){ connectionEntity.setBusy(false); } } } catch (SQLException e) { // 出现异常移除链接 for (ConnectionEntity connectionEntity:pools){ if (connectionEntity.getCon()==connection){ pools.remove(connectionEntity); break; } } }}}