简单的连接池( Connectionpoo) public synchronized Connection getConnection ( thr SQLException if(connections. size== 0) return DriverManager getConnection(url, user, passwd)i lse i int lastIndex connections size( return connections. remove(lastindex)i public synchronized void closeConnection(Connection conn) throws SQLException f(connections. size(== max)[ Ise connections. add(conn)
简单的连接池(Connectionpool) public synchronized Connection getConnection() throws SQLException { if(connections.size() == 0) { return DriverManager.getConnection(url, user, passwd); } else { int lastIndex = connections.size() - 1; return connections.remove(lastIndex); } } public synchronized void closeConnection(Connection conn) throws SQLException { if(connections.size() == max) { conn.close(); } else { connections.add(conn); } }
简单的连接池( Connectionpoo) DBSource dbsource newBasicDBSource( "]dbc2properties")i Connection conn=dbsource getConnection ( dbsource. closeConnection(connl)i Connection conn2= dbsource getConnection( System. out. println(conn = conn2)i onlyfun. caterpillar. driver=com mysql jdbc Driver onlyfun. caterpillar. url=jdbc: mysql://localhost: 3306/demo onlyfun. caterpillar.user=root onlyfun caterpillar password=123456 onlyfun. caterpillar coolmax=10
简单的连接池(Connectionpool) DBSource dbsource = newBasicDBSource("jdbc2.properties"); Connection conn1 = dbsource.getConnection(); dbsource.closeConnection(conn1); Connection conn2 = dbsource.getConnection(); System.out.println(conn1 == conn2); onlyfun.caterpillar.driver=com.mysql.jdbc.Driver onlyfun.caterpillar.url=jdbc:mysql://localhost:3306/demo onlyfun.caterpillar.user=root onlyfun.caterpillar.password=123456 onlyfun.caterpillar.poolmax=10
简单的连接池( Connectionpoo) 初始的 Connection数量 Connection最大idle的数量 如果超过多久时间,要回收多少数量的 Connection Proxool http://proxool.sourceforge.net/index.html Apache JakartaE'y Common DBCP httpjakartaapacheorg/commons/dbcp/
简单的连接池(Connectionpool) • 初始的Connection数量 • Connection最大idle的数量 • 如果超过多久时间,要回收多少数量的 Connection • Proxool – http://proxool.sourceforge.net/index.html • Apache Jakarta的Common DBCP – http://jakarta.apache.org/commons/dbcp/