心血来潮,想看看数据库连接池的作用有多大。
于是就尝试着看看,给自己一个感性的认识。
相关参数如下:
win server 2003
tomcat 6.0.10
Mysql 5.0
Myeclipse 6.0
无连接池的插入方法:
有连接池时:
servlet代码:
刚开始的配置是:
插入1000条,连接池配置:
结果:
如果数据库连接配置没变,改为插入10000条,则:
如果修改maxActive为1000
如何配置才能最优呢?
于是就尝试着看看,给自己一个感性的认识。
相关参数如下:
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
如何配置才能最优呢?