Jdbcデータベース接続プールの簡易実装とJdbcUtils

5179 ワード


public class MyDataSource {
	
	private static String url = "jdbc:mysql://localhost:3306/riverevaluationsys";
	private static String user = "root";
	private static String password = "root";
	
	private static int initCount = 5;
	private static int maxCount = 20;
	private int currentCount = 0;
	
	private LinkedList<Connection> connectionsPool = new LinkedList<Connection>();
	
	public MyDataSource() {
		try {
			for (int i=0; i<5; i++) {
				this.connectionsPool.add(this.createConnection());
				this.currentCount++;
			}
		} catch (SQLException e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public Connection getConnection() throws SQLException {
		//      
		synchronized (connectionsPool) {
			if (this.connectionsPool.size() > 0) {
				return this.connectionsPool.removeFirst();
			} else {
				if (this.currentCount < maxCount) {
					this.currentCount++ ;
					return this.createConnection();
				} else {
					throw new SQLException("no more connection.");
				}
			}
		}
	}
	
	public void free(Connection conn) {
		this.connectionsPool.addLast(conn);
	}
	
	private Connection createConnection() throws SQLException {
		return DriverManager.getConnection(url, user, password);
	}
}
 

public class JdbcUtils {
	
	/*private static String url = "jdbc:mysql://localhost:3306/riverevaluationsys";
	private static String user = "root";
	private static String password = "root";*/
	
	private static MyDataSource myDataSource ;
	
	//      ,    
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			myDataSource = new MyDataSource();
		} catch (ClassNotFoundException e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	/**
	 * 
	 * getConnection:<br />
	 *         
	 *
	 * @author zhangzhaoyu
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		//return DriverManager.getConnection(url, user, password);
		return myDataSource.getConnection();
	}
	/**
	 * 
	 * close:<br />
	 *     
	 *
	 * @author zhangzhaoyu
	 * @param conn
	 * @param rs
	 * @param st
	 */
	public static void close(Connection conn, ResultSet rs, Statement st) {
		
		 try {  
	            if (rs != null)  
	                rs.close();  
	        } catch (SQLException e) {  
	            e.printStackTrace();  
	        } finally {  
	            try {  
	                if (st != null)  
	                    st.close();  
	            } catch (SQLException e) {  
	                e.printStackTrace();  
	            } finally {  
	            	try {  
                        myDataSource.free(conn); 
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    } 
	            }  
	        }  
	}
	
	/**
	 * 
	 * close:<br />
	 *          
	 *
	 * @author zhangzhaoyu
	 * @param conn
	 * @param rs
	 * @param pst
	 */
	public static void close(Connection conn, ResultSet rs, PreparedStatement pst) {
		
		 try {  
	            if (rs != null)  
	                rs.close();  
	        } catch (SQLException e) {  
	            e.printStackTrace();  
	        } finally {  
	            try {  
	                if (pst != null)  
	                	pst.close();  
	            } catch (SQLException e) {  
	                e.printStackTrace();  
	            } finally {  
	                if (conn != null)  
	                    try {  
	                        myDataSource.free(conn); 
	                    } catch (Exception e) {  
	                        e.printStackTrace();  
	                    }  
	            }  
	        }  
	}
	
	/**
	 * 
	 * close:<br />
	 *     
	 *
	 * @author zhangzhaoyu
	 * @param conn
	 * @param pst
	 */
	public static void close(Connection conn, PreparedStatement pst) {
		
		    try {  
		        if (pst != null)  
		        	pst.close();  
		    } catch (SQLException e) {  
		        e.printStackTrace();  
		    } finally {  
		        if (conn != null)  
		        	try {  
                        myDataSource.free(conn); 
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    } 
		    }  
	 }  
}