使用資料庫連線池連線資料庫
阿新 • • 發佈:2019-01-22
資料庫連線池是什麼?
資料庫連線池(Connection pooling)是程式啟動時建立足夠的資料庫連線,並將這些連線組成一個連線池,由程式動態地對池中的連線進行申請,使用,釋放。
在對資料庫進行操作之前都要先獲取資料庫連線,然後才能向後進行操作,增刪改查,獲取結果集,浪費時間的地方就是在獲取資料庫連線上,以往每次操作的時候,先獲取連線,操作完之後關掉連線,這麼一次一次,時間都浪費在獲取連線上了,我們需要的就是一個數據庫連線池,先建立好一定數量的連線放在池子裡,當我們要用的時候,去池子中去找可用的連線,當用完之後,再把連線放回池子中,這樣我們用的連線都是池子負責管理的,每次用完之後只要放回去,這樣相比於之前的獲取到連線,用完關閉速度很快,只是把連線拿來不用每次都建立,關閉,可以省很多的時間。
使用c3p0可以很輕鬆地構建資料庫連線池,是一個開源的專案,https://sourceforge.net/projects/c3p0/files/?source=navbar下載網址,一個壓縮包中包含jar包和例子,說明
需要的jar包,匯入我們的專案中就能使用了
使用單例模式(餓漢式)構建資料庫連線池,
private static ComboPooledDataSource dataSource; private static ConnectionManager connectionManager = new ConnectionManager(); private ConnectionManager(){ try { //ComboPooledDataSource cpds = new ComboPooledDataSource(); // cpds.setDriverClass(“org.postgresql.Driver”); // 載入jdbc驅動程式cpds.setJdbcUrl(“jdbc:postgresql:localhost / testdb”); // cpds.setUser( “swaldman”); // cpds.setPassword( “測試密碼”); // 下面的設定是可選的 - c3p0可以使用預設值 // cpds.setMinPoolSize(5); // cpds.setAcquireIncrement(5); // cpds.setMaxPoolSize(20); // DataSource cpds現在是一個完全配置和可用的彙集資料來源... dataSource = new ComboPooledDataSource(); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mail"); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setInitialPoolSize(5); //初始化連線數 dataSource.setMinPoolSize(1);//最小連線數 dataSource.setMaxPoolSize(20);//最大連線數 dataSource.setMaxStatements(50);//最長等待時間 dataSource.setMaxIdleTime(60);//最大空閒時間,單位毫秒 } catch (PropertyVetoException e) { e.printStackTrace(); } } public static ConnectionManager getInstance(){ return connectionManager; } public synchronized Connection getConnection() { Connection conn = null; try { conn=dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return conn; }
在下載的說明文件中也介紹了使用方法
在main方法中測試,輸出兩種方法所用的時間
public static void main(String[] args) throws SQLException { for(int i=0;i<50;i++) { long begin = System.currentTimeMillis(); Connection conn = ConnectionManager.getInstance().getConnection(); PreparedStatement ps = conn.prepareStatement("SELECT * FROM USER WHERE id=2"); ResultSet rs = ps.executeQuery(); while (rs.next()) { } conn.close(); ps.close(); rs.close(); long end = System.currentTimeMillis(); System.out.print(end-begin+" "); } System.out.println("-------------"); for(int i=0;i<50;i++) { long begin = System.currentTimeMillis(); Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/mail"; String username = "root"; String password = "root"; conn = DriverManager.getConnection(url, username, password); PreparedStatement ps = conn.prepareStatement("SELECT * FROM USER WHERE id=2"); ResultSet rs = ps.executeQuery(); while (rs.next()) { } conn.close(); ps.close(); rs.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.print(end - begin+" "); } }
輸出:
利用資料庫連線池第一次使用耗時時間長,以後平均用時均為1ms,不使用連線池的程式碼平均都在15ms左右
參考:http://blog.csdn.net/wenwen091100304/article/details/48035003