当前位置:Gxlcms > 数据库问题 > 自定义数据库连接池

自定义数据库连接池

时间:2021-07-01 10:21:17 帮助过:35人阅读

internal.Pool; public class MyPool implements DataSource { /**创建一个容器存放数据库连接池*/ static List<Connection> pool = new LinkedList<Connection>(); /**静态类初始化数据库连接*/ static{ for (int i = 0; i < 10; i++) { //可以从工具类里面获取数据库的连接池, 如下: //Connection con = JDBCUtils.getConnection(); pool.add(con); } } /**提供给外界connection方法,外界用来获取数据库连接*/ public Connection getConnection() throws SQLException { //从连接池中移除一个对象并返回这个对象 Connection conn = pool.remove(0); return conn; } /**提供一个返回连接的方法*/ public void returnConnection(Connection conn) { try { if (conn!=null && !conn.isClosed()) { pool.add(conn); } } catch (Exception e) { // TODO: handle exception } } public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null; } public void setLogWriter(PrintWriter out) throws SQLException { // TODO Auto-generated method stub } public void setLoginTimeout(int seconds) throws SQLException { // TODO Auto-generated method stub } public int getLoginTimeout() throws SQLException { // TODO Auto-generated method stub return 0; } public Logger getParentLogger() throws SQLFeatureNotSupportedException { // TODO Auto-generated method stub return null; } public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } public boolean isWrapperFor(Class<?> iface) throws SQLException { // TODO Auto-generated method stub return false; } public Connection getConnection(String username, String password) throws SQLException { // TODO Auto-generated method stub return null; } }

 

自定义数据库连接池

标签:import   rate   utils   turn   span   连接池   数据库连接池   取数   jdbc   

人气教程排行