数据库连接池可以通过两种方式来应用,针对web应用和非web应用而来。
非web应用的数据库连接池配置
第一种方式:工厂类
非web应用可以使用工厂模式将数据库连接创建封装在一个工具类中,工具类中又使用单例模式来创建加载驱动,代码如下:
工厂类:ConnectPoolFactory.java
1 import java.io.InputStream; 2 import java.sql.Connection; 3 import java.sql.DriverManager; 4 import java.util.Properties; 5 6 import org.logicalcobwebs.proxool.configuration.PropertyConfigurator; 7 8 public class ConnectPoolFactory { 9 10 private static ConnectPoolFactory connectPoolFactory=null; 11 12 private ConnectPoolFactory() {//构造方法 13 init(); 14 } 15 16 public void init(){//把properties文件加载到链接对象 17 InputStream is = ConnectPoolFactory.class.getResourceAsStream("/proxool.properties"); 18 Properties properties = new Properties(); 19 try { 20 properties.load(is); 21 PropertyConfigurator.configure(properties); 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 } 26 27 public static ConnectPoolFactory getInstance(){//单例模式 28 if(null == connectPoolFactory){ 29 connectPoolFactory = new ConnectPoolFactory(); 30 } 31 32 return connectPoolFactory; 33 } 34 35 36 public Connection getConnect(){ 37 Connection conn=null; 38 try { 39 Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");// 加载数据库连接池配备的驱动 40 conn = DriverManager.getConnection("proxool.test");//proxool为配置文件名,test为连接池别名 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 return conn; 45 } 46 47 }
配置文件:proxool.properties
1 jdbc-1.proxool.alias=test 2 #jdbc-1.proxool.driver-class=oracle.jdbc.OracleDriver 3 #jdbc-1.proxool.driver-url=jdbc:oracle:thin:@127.0.0.1:1521:orcl 4 jdbc-1.proxool.driver-class=com.mysql.jdbc.Driver 5 jdbc-1.proxool.driver-url=jdbc:mysql://localhost:3306/db_course 6 jdbc-1.user=root 7 jdbc-1.password=root 8 9 jdbc-1.proxool.maximum-connection-count=8 10 jdbc-1.proxool.minimum-connection-count=5 11 jdbc-1.proxool.prototype-count=4 12 jdbc-1.proxool.verbose=true 13 jdbc-1.proxool.statistics=10s,1m,1d 14 jdbc-1.proxool.statistics-log-level=error
然后通过工厂类创建工厂对象,通过工厂对象获取连接池实例,通过实例创建连接
测试类:ProxoolTest.java
1 public class ProxoolTest { 2 public static void main(String[] args) { 3 ConnectPoolFactory factory = ConnectPoolFactory.getInstance(); 4 Connection connect = factory.getConnect(); 5 System.out.println("ProxoolTest.main(conncet): " + connect); 6 } 7 }
第二种方式:工具类中加载
工具类:DBUtil.java
1 public class DBUtil { 2 static { 3 InputStream is = DBUtil.class 4 .getResourceAsStream("/proxool.properties"); 5 Properties properties = new Properties(); 6 try { 7 properties.load(is); 8 PropertyConfigurator.configure(properties); 9 } catch (Exception e) { 10 e.printStackTrace(); 11 } 12 } 13 14 /** 15 * 获取数据库连接 16 * 17 * @return 18 */ 19 public static Connection getConn() { 20 Connection conn = null; 21 try { 22 Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 23 conn = DriverManager.getConnection("proxool.test"); 24 } catch (SQLException e) { 25 e.printStackTrace(); 26 } catch (ClassNotFoundException e) { 27 e.printStackTrace(); 28 } 29 return conn; 30 } 31 32 /** 33 * 获取sql语句发送器 34 * 35 * @param conn 36 * @return 37 */ 38 public static Statement getStmt(Connection conn) { 39 Statement stmt = null; 40 try { 41 stmt = conn.createStatement(); 42 } catch (SQLException e) { 43 e.printStackTrace(); 44 } 45 return stmt; 46 } 47 48 /** 49 * 获取预处理发送器 50 * 51 * @param conn 52 * @param sql 53 * @return 54 */ 55 public static PreparedStatement getPstmt(Connection conn, String sql) { 56 PreparedStatement pstmt = null; 57 try { 58 pstmt = conn.prepareStatement(sql); 59 } catch (SQLException e) { 60 e.printStackTrace(); 61 } 62 return pstmt; 63 } 64 65 /** 66 * 动态绑定参数 67 * 68 * @param pstmt 69 * @param params 70 */ 71 public static void bindParam(PreparedStatement pstmt, Object... params) { 72 for (int i = 0; i < params.length; i++) { 73 try { 74 pstmt.setObject(i + 1, params[i]); 75 } catch (SQLException e) { 76 e.printStackTrace(); 77 } 78 } 79 } 80 81 /** 82 * 关闭资源 83 * @param autoCloseables 实现了AutoCloseable接口的实现类对象 84 */ 85 public static void closeAll(AutoCloseable...autoCloseables) { 86 for (AutoCloseable autoCloseable : autoCloseables) { 87 if(autoCloseable!=null){ 88 try { 89 autoCloseable.close(); 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } 93 } 94 } 95 } 96 }
在web应用中使用数据库连接池
proxool.properties配置文件是不可少的,但是DBUtil.java中的静态代码块就不再需要了。
不过要在web.xml中添加如下代码:
1 <!-- 配置连接池信息 --> 2 <servlet> 3 <servlet-name>servletConfigurator</servlet-name> 4 <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class> 5 <init-param> 6 <param-name>propertyFile</param-name> 7 <param-value>WEB-INF/classes/proxool.properties</param-value> 8 </init-param> 9 <load-on-startup>1</load-on-startup><!-- 应用启动时加载配置信息 --> 10 </servlet> 11 12 <!-- 监控连接池 --> 13 <servlet> 14 <servlet-name>adminServlet</servlet-name> 15 <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> 16 </servlet> 17 <servlet-mapping> 18 <servlet-name>adminServlet</servlet-name> 19 <url-pattern>/admin</url-pattern> 20 </servlet-mapping>
其中<url-pattern>/admin</url-pattern>用来配置访问监控连接池页面