比较方法:建立两个jsp页面,分别使用r2和proxool链接池读取数据库,执行select 1,两链接池均配置最大50个线程;分别使用jmeter用50个并发读取r2的jsp页面和proxool的jsp,得到如下数据:
从测试结果可以看出在最大链接数范围内r2的平均执行效率是proxool的10倍。错误率r2为0,但proxool较高报错率较高,后台报错误:
严重: Servlet.service() for servlet jsp threw exception
java.sql.SQLException: Couldn't get connection because we are at maximum connection count (50/50) and there are none available
下图为proxool配置成500个最大链接数时的结果:
(平均响应仍为50个最大链接数的R2的十倍,仍有少量的错误)
经过比较,R2在性能和稳定性上都要高于proxool。
jsp页面代码:
r2.jsp
<%
Connection conn = R2PoolUtil.getStaticPool().getConnection();
try{
PreparedStatement pstmt = conn.prepareStatement("select 1");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
%><%=rs.getInt(1)%><%
}
rs.close();
pstmt.close();
}catch(Exception e){
}finally{
try{
if(conn!=null)conn.close();
}catch(Exception e){
}
}
%>
<%!
private ProxoolDataSource dataSource =null;
private Connection getConnection()throws Exception{
if(dataSource==null){
dataSource =new ProxoolDataSource();
dataSource.setAlias("proxool1");
dataSource.setDriver("com.mysql.jdbc.Driver");
dataSource.setDriverUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false");
dataSource.setUser("root");
dataSource.setPassword("aaaaa");
dataSource.setSimultaneousBuildThrottle(500);
dataSource.setMaximumConnectionCount(500);
dataSource.setMinimumConnectionCount(500);
dataSource.setDelegateProperties("user=root,password=123qwe");
dataSource.setTestAfterUse(true);
}
return dataSource.getConnection();
}
%>
<%
Connection conn = getConnection();
try{
PreparedStatement pstmt = conn.prepareStatement("select 1");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
%><%=rs.getInt(1)%><%
}
rs.close();
pstmt.close();
}catch(Exception e){
}finally{
try{
if(conn!=null)conn.close();
}catch(Exception e){
}
}
%>