Getting connection from Connection Pool taking too much time in java

微笑、不失礼 提交于 2019-12-25 09:03:36

问题


I have a java based web application in which I have included the Connection Pooling method.

Code is as follows :

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();
    }
}

Now in my application whenever I need a connection, I use Database.getConnection(). Issue is that, whenever am calling Database.getConnection(), it's taking around 900 ms .

Am also closing the connection after every DB operation.

Code which I used is :

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));

Output which I get is :

Time before callingg Func is : 61
sql Query time is : 266
Time after callingg Func is : 1123

EDIT :

I calculated the time when conn is not closed.

I also added a timestamp before going into the db function and a timestamp in the first line of the function. Then I calculated the time from 2nd line to the end of the function which is actually sql time.

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

EDIT 2

Removed the connection part from the function i.e.

count = a.d(jObj.getString("a"), jObj.getString("b"),c,jObj.getString("d"),jObj.getString("e"));

and then the time calculations are :

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

来源:https://stackoverflow.com/questions/42899224/getting-connection-from-connection-pool-taking-too-much-time-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!