我有一个基于java的Web应用程序,其中包含了连接池方法。
代码如下:
public final class Database {
private static final String SQL_EXIST = "show tables;";
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
}
private static final BasicDataSource dataSource = new BasicDataSource();
static {
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://link");
dataSource.setUsername("user");
dataSource.setPassword("pass");
dataSource.setMaxTotal(10);
}
private Database() {
//
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
现在,只要我需要连接,我就可以使用Database.getConnection()
。问题在于,无论何时呼叫Database.getConnection()
,它都需要大约900毫秒。
每次数据库操作后也会关闭连接。
我使用的代码是:
System.out.println("Time before callingg Func is : "+(System.currentTimeMillis()-main_time));
ABC a=new ABC();
count = a.d(Database.getConnection(), jObj.getString("a"), jObj.getString("b"),c,jObj.getString("d"),jObj.getString("e"));
System.out.println("Time after callingg Func is : "+(System.currentTimeMillis()-main_time));
我得到的输出是:
Time before callingg Func is : 61
sql Query time is : 266
Time after callingg Func is : 1123
编辑:
我计算了conn未关闭的时间。
我还在进入db函数之前添加了一个时间戳,并在函数的第一行添加了一个时间戳。然后我计算了从第二行到函数结束的时间,实际上是sql时间。
Time before callingg Func is : 68 and time in ms is : 1490001506121
Time in ms when inside function is : 1490001506843
Diff in time is : 722
sql Query time is : 260
Time after callingg Func is : 1050
编辑2
从功能中删除了连接部分,即
count = a.d(jObj.getString("a"), jObj.getString("b"),c,jObj.getString("d"),jObj.getString("e"));
然后计算时间:
Time before callingg Func is : 65 and time in ms is : 1490003211049
Time in ms when inside function is : 1490003211049
Diff in time is : 0
sql Query time is : 1
Time after callingg Func is : 66