开发者

Slackware 12 Really Slow JDBC

Greeting, I've some problem with JDBC, it is so slow its unbeliveble.

Same code works perfect with Windows connection to a remote Database.

But with slackware, it takes like 30 seconds to connect, and the first try always fail. I had to do a work around to make it try again to make sure it connects. But even it should not be that slow.

using mysql command in bash seems to connect instantly.

Here is the code used to create the database

public static Object createDBConnection(String dbType, String host, String hostPort, String dbName, String userName, String password) {

    String connectionString = buildConnectionString(dbType, host, hostPort, dbName, userName, password);
    Connection connection = null;

    try {
        if(dbType.equals(K_dbTypeMySQL)){               开发者_运维技巧
            Class.forName("com.mysql.jdbc.Driver");
        }else if(dbType.equals(K_dbTypeSqlServer)){
            Class.forName("net.sourceforge.jtds.jdbc.Driver");              
        }else if(dbType.equals(K_dbTypeSqlServerExpress)){
            Class.forName("net.sourceforge.jtds.jdbc.Driver");              
        }else if(dbType.equals(K_dbTypePostgres))   {
            Class.forName("org.postgresql.Driver");
        }


        connection = DriverManager.getConnection(connectionString);
        return connection; 

    }
    catch (Exception e)
    {               
        e.printStackTrace();
        return null;
    }       
}

public static String buildConnectionString(String dbType, String host, String hostPort, String dbName, String userName, String password){
    String connectionString = null;
    if((dbType.equals(K_dbTypeMySQL))){
        connectionString = "jdbc:mysql://" + host + ":" + hostPort + "/" + dbName + "?user=" + userName + "&password=" + password;
    }else if((dbType.equals(K_dbTypeSqlServer)))
        connectionString = "jdbc:jtds:sqlserver://" + host + ":" + hostPort + ";databaseName=" + dbName + ";selectMethod=cursor;user=" + userName + ";password=" + password;        
    else if((dbType.equals(K_dbTypeSqlServerExpress)))
        connectionString = "jdbc:jtds:sqlserver://" + host + ":" + hostPort + ";databaseName=" + dbName + ";selectMethod=cursor;user=" + userName + ";password=" + password;
    else if(dbType.equals(K_dbTypePostgres))
        connectionString = String.format("jdbc:postgresql://%s:%s/%s?user=%s&password=%s",host,hostPort,dbName,userName,password);


    return connectionString;
}

I tried using different drivers but same thing, first try always takes forever to connect (20 seconds), second time takes like 2 seconds.

I have the same problem when trying to open an https url with URLConnection first time always fail, second time no problem.

Anyone have any insight about this problem? Since I have problem with opening HTTPS url with java, maybe some problem with SSL? do mysql use SSL in any way?

server version: 5.1.41

host: localhost

jdbc driver: 5.1.8, 5.1.12, 5.1.17 (all have same problem)

java build: root@LinuxMZ:~/nx_dev/sicm# java -version java version "1.6.0_06" Java(TM) SE Runtime Environment (build 1.6.0_06-b02) Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)

//EDIT 127.0.0.1 and localhost do the same problem

thank you for your help


Nothing yet, DBManager.createConnection hang for 20 seconds. I tried connecting from another machine and it works flawlessly.


30 seconds smells like a DNS timeout when looking up the name you've given.

You can confirm this by using the IP address as the host name instead.


Looks like MySQL is trying to resolve the client using nslookup. In the current context, client has 127.0.0.1 or 192.168.1.XXX. I think this is due to bad naming in /etc/hosts. Look in that file if there is not a bad entry!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜