感性对比有无使用数据库连接池的性能

心血来潮,想看看数据库连接池的作用有多大。
于是就尝试着看看,给自己一个感性的认识。
相关参数如下:
win server 2003
tomcat 6.0.10
Mysql 5.0
Myeclipse 6.0

无连接池的插入方法:

public void addUser(String name,String password) {


String sql="insert into userinfo(usname,uspassword) values(?,?)";

try {
conn=DriverManager.getConnection(connstr);
try {
ps=conn.prepareStatement(sql);

ps.setString(1,name);
ps.setString(2, password);

try {
ps.execute();


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(rs!=null)rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(ps!=null)ps.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


有连接池时:

InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();


servlet代码:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("test 1000 add");

DBManager db=new DBManager();
db.getConnection();
long lIteratorStartTime = new Date().getTime();
System.out.println("开始: " + lIteratorStartTime);
for(int i=0;i<1000;i++){
db.addUser("persia"+i, "persia"+i);
}
long lIteratorEndTime = new Date().getTime();
System.out.println("结束: " + lIteratorEndTime);

long lIteratorDifference = lIteratorEndTime - lIteratorStartTime;
System.out.println("无数据库连接池的耗时: " + lIteratorDifference);

DBManagerByPool dbp=new DBManagerByPool();

long startTime = new Date().getTime();
System.out.println("开始: " + startTime);
for(int i=1000;i<2000;i++){
try {
dbp.addUser("persia"+i, "persia"+i);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long endTime = new Date().getTime();
System.out.println("结束: " + endTime);

long difference = endTime - startTime;
System.out.println("使用数据库连接池的耗时: " + difference);


out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}


刚开始的配置是:
插入1000条,连接池配置:

<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mylogin"/>


结果:

开始: 1242302560140
结束: 1242302596296
无数据库连接池的耗时: 36156
开始: 1242302596296
结束: 1242302622828
使用数据库连接池的耗时: 26532


如果数据库连接配置没变,改为插入10000条,则:

开始: 1242302704390
结束: 1242303057265
无数据库连接池的耗时: 352875
开始: 1242303057265
结束: 1242303365687
使用数据库连接池的耗时: 308422


如果修改maxActive为1000

开始: 1242305368468
结束: 1242305674984
使用数据库连接池的耗时: 306516


如何配置才能最优呢?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值