配置檔案db.properties 資料庫連線
阿新 • • 發佈:2020-07-23
一、配置檔案db.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/test username=root password= min=5 max=20
二、DbUtil用於生成連線
package com.db; import com.mysql.jdbc.Connection; import java.io.IOException; import java.io.InputStream; import java.sql.DriverManager; import java.util.Properties; public class DbUtil { private static String PROP_PATH = "db.properties"; private static String driver = null; private static String url = null; private static String username = null; private static String password = null; private DbUtil(){} static { try { InputStream in = DbUtil.class.getClassLoader().getResourceAsStream(PROP_PATH); Properties props = new Properties(); props.load(in); driver = props.getProperty("driver"); url = props.getProperty("url"); username = props.getProperty("username"); password = props.getProperty("password"); } catch (IOException ex) { ex.printStackTrace(); } } public static Connection getConnection(){ try { Class.forName(driver); Connection conn = (Connection) DriverManager.getConnection(url,username,password); System.out.println("generate a new connection: " + conn.hashCode()); return conn; } catch (Exception ex) { ex.printStackTrace(); return null; } } }
三、連線池DbPool
package com.db; import com.mysql.jdbc.Connection; import java.io.IOException; import java.io.InputStream; import java.sql.SQLException; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Properties; import java.util.concurrent.ConcurrentLinkedQueue; public class DbPool { private static String PROP_PATH = "db.properties"; private static String max = null; private static String min = null; private static int poolSize = 0; //記錄連線池中的數量 private DbPool(){} //private static List<Connection> pool = Collections.synchronizedList(new LinkedList<Connection>());//讓LinkedList變成執行緒安全的 private static ConcurrentLinkedQueue<Connection> pool = new ConcurrentLinkedQueue<Connection>();//更加高效的執行緒安全容器 static { if (poolSize == 0 && pool.isEmpty()) { synchronized (DbPool.class) { if (poolSize == 0 && pool.isEmpty()) { try { System.out.println("連線池為空,初始化"); InputStream in = DbUtil.class.getClassLoader().getResourceAsStream(PROP_PATH); Properties props = new Properties(); props.load(in); max = props.getProperty("max"); min = props.getProperty("min"); for (int i = 0; i < Integer.parseInt(min); i++) { pool.add(DbUtil.getConnection()); poolSize++; } } catch (IOException ex) { ex.printStackTrace(); } } } } } public static Connection getConnection() { try { if (!pool.isEmpty()) { System.out.println("連線池非空,直接返回"); //return (pool).removeFirst(); return pool.remove(); } else { System.out.println("連線池為空,進行擴容"); expland(5); System.out.println("等待獲取"); while (true) { if (!pool.isEmpty()) { System.out.println("獲取成功,返回"); //return pool.removeFirst(); return pool.remove(); } } } } catch (Exception ex) { ex.printStackTrace(); return null; } } /** * 銷燬連線池 */ public static void distory(){ for (Connection conn : pool) { try { conn.close(); pool.remove(conn); } catch (SQLException e) { e.printStackTrace(); } } poolSize = 0; System.out.println("銷燬成功,未使用:" + getAvaliableSize() + "總數:" + getPoolSize()); } /** * 回收連線 * @param conn */ public static void recycle(Connection conn){ pool.add(conn); System.out.println("回收成功,未使用:" + getAvaliableSize() + "總數:" + getPoolSize()); } /** * 獲取當前連線池有多少個連線,包括已使用和未使用 * @return */ public static int getPoolSize(){ return poolSize; } /** * 獲取未使用的數量 * @return */ public static int getAvaliableSize(){ return pool.size(); } /** * 執行緒池擴容 * @num 增加的數量 */ private static void expland(int num) { if (poolSize > Integer.parseInt(max)) { System.out.println("總連線數量大於最大值,直接返回"); return; } for (int i=0; i<num; i++) { pool.add(DbUtil.getConnection()); poolSize++; } System.out.println("擴容成功,未使用:" + getAvaliableSize() + "總數:" + getPoolSize()); } }