1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| public class MyDataSource implements DataSource { private static List<Connection> pool = Collections.synchronizedList(new ArrayList<Connection>()); static{ for(int i=0;i<10;i++){ Connection conn = JdbcUtil.getConnection(); pool.add(conn); } } public Connection getConnection() throws SQLException { if(pool.size()>0){ final Connection conn = pool.remove(0); Connection proxyConn = (Connection)Proxy.newProxyInstance(conn.getClass().getClassLoader(), conn.getClass().getInterfaces(), new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if("close".equals(method.getName())){ return pool.add(conn); }else{ return method.invoke(conn, args); } } }); return proxyConn; }else{ throw new RuntimeException("连接池忙"); } } public PrintWriter getLogWriter() throws SQLException { return null; }
public void setLogWriter(PrintWriter out) throws SQLException {
}
public void setLoginTimeout(int seconds) throws SQLException {
}
public int getLoginTimeout() throws SQLException { return 0; }
public <T> T unwrap(Class<T> iface) throws SQLException { return null; }
public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; }
public Connection getConnection(String username, String password) throws SQLException { return null; }
}
|