在Macos Sierra上缓慢的jdbc连接

时间:2016-12-02 13:25:14

标签: java jdbc macos-sierra

如果我这样做:

package mypackage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

public class ProvaDb {
    static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
    static final String CONNECTION_STRING = "jdbc:oracle:thin:@192.168.1.75:1521:XE";
    static final String USERNAME = "dbuser";
    static final String PASSWORD = "dbpassword";
    static final String QUERY = "select sysdate from dual";
    public static void main(String[] args) throws Exception {
        for(int i = 0; i < 15; i ++) {
            final Integer ind = i;
            Thread t = new Thread(new Runnable() {

                @Override
                public void run() {
                    Connection connection = null;
                    Statement statement= null;
                    ResultSet resultSet = null;
                    try {
                        Class.forName(DRIVER);
                        long be = (new Date()).getTime();
                        connection = DriverManager.getConnection(CONNECTION_STRING,USERNAME,PASSWORD);
                        long ae = (new Date()).getTime();
                        System.out.println(ind + " Connection time: " + ((double)(ae - be)) / 1000 + " sec.");
                        statement = connection.createStatement();
                        resultSet = statement.executeQuery(QUERY);
                        System.out.println("Thread " + ind + " sleep");
                        Thread.sleep(15000);
                        System.out.println("Thread " + ind + " wake up");
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if(resultSet != null) {
                                resultSet.close();
                            }
                            if(statement != null) {
                                statement.close();
                            }
                            if(connection != null) {
                                connection.close();
                                System.out.println("Connection " + ind + " closed");
                            }
                        } catch(Exception e) {
                            e.printStackTrace();
                        }
                    }

                }
            });
            t.start();
        }
    }

}

在Macos El Capitan上,我得到了这个:

8 Connection time: 1.267 sec.
2 Connection time: 1.635 sec.
6 Connection time: 1.647 sec.
Thread 6 sleep
Thread 8 sleep
Thread 2 sleep
5 Connection time: 1.663 sec.
Thread 5 sleep
14 Connection time: 1.678 sec.
Thread 14 sleep
10 Connection time: 1.692 sec.
Thread 10 sleep
11 Connection time: 1.709 sec.
Thread 11 sleep
4 Connection time: 1.722 sec.
Thread 4 sleep
12 Connection time: 1.741 sec.
Thread 12 sleep
13 Connection time: 1.763 sec.
Thread 13 sleep
3 Connection time: 1.77 sec.
Thread 3 sleep
0 Connection time: 1.782 sec.
Thread 0 sleep
7 Connection time: 1.797 sec.
Thread 7 sleep
1 Connection time: 1.812 sec.
Thread 1 sleep
9 Connection time: 1.83 sec.
Thread 9 sleep
Thread 2 wake up
Thread 6 wake up
Thread 8 wake up
Connection 2 closed
Connection 6 closed
Connection 8 closed
Thread 5 wake up
Connection 5 closed
Thread 14 wake up
Connection 14 closed
Thread 10 wake up
Connection 10 closed
Thread 11 wake up
Connection 11 closed
Thread 4 wake up
Connection 4 closed
Thread 12 wake up
Connection 12 closed
Thread 13 wake up
Thread 3 wake up
Connection 13 closed
Connection 3 closed
Thread 0 wake up
Connection 0 closed
Thread 7 wake up
Connection 7 closed
Thread 1 wake up
Connection 1 closed
Thread 9 wake up
Connection 9 closed

但如果我在Macos Sierra上使用相同的Java版本(7和8),相同的硬件,相同的网络,相同的网络配置,相同的数据库(包括Oracle和SQLServer)运行相同的操作,我会得到:

14 Connection time: 5.566 sec.
Thread 14 sleep
7 Connection time: 10.546 sec.
Thread 7 sleep
0 Connection time: 15.548 sec.
Thread 0 sleep
8 Connection time: 20.553 sec.
Thread 8 sleep
Thread 14 wake up
Connection 14 closed
Thread 7 wake up
Connection 7 closed
9 Connection time: 25.561 sec.
Thread 9 sleep
Thread 0 wake up
Connection 0 closed
13 Connection time: 30.566 sec.
Thread 13 sleep
Thread 8 wake up
Connection 8 closed
11 Connection time: 35.565 sec.
Thread 11 sleep
Thread 9 wake up
Connection 9 closed
2 Connection time: 40.567 sec.
Thread 2 sleep
Thread 13 wake up
10 Connection time: 45.572 sec.
Connection 13 closed
Thread 10 sleep
Thread 11 wake up
Connection 11 closed
6 Connection time: 50.58 sec.
Thread 6 sleep
Thread 2 wake up
Connection 2 closed
12 Connection time: 55.584 sec.
Thread 12 sleep
Thread 10 wake up
Connection 10 closed
5 Connection time: 60.589 sec.
Thread 5 sleep
Thread 6 wake up
Connection 6 closed
java.sql.SQLRecoverableException: Non vi sono altri dati da leggere nel socket
    at oracle.jdbc.driver.T4CMAREngineStream.unmarshalUB1(T4CMAREngineStream.java:456)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:397)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOSESSKEY(T4CTTIoauthenticate.java:460)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:594)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.mindInformatica.webAppUtils.db.ProvaDb$1.run(ProvaDb.java:28)
    at java.lang.Thread.run(Thread.java:745)
java.sql.SQLRecoverableException: Non vi sono altri dati da leggere nel socket
    at oracle.jdbc.driver.T4CMAREngineStream.unmarshalUB1(T4CMAREngineStream.java:456)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:397)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOSESSKEY(T4CTTIoauthenticate.java:460)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:594)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.mindInformatica.webAppUtils.db.ProvaDb$1.run(ProvaDb.java:28)
    at java.lang.Thread.run(Thread.java:745)
Thread 12 wake up
Connection 12 closed
java.sql.SQLRecoverableException: Non vi sono altri dati da leggere nel socket
    at oracle.jdbc.driver.T4CMAREngineStream.unmarshalUB1(T4CMAREngineStream.java:456)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:397)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOSESSKEY(T4CTTIoauthenticate.java:460)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:594)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.mindInformatica.webAppUtils.db.ProvaDb$1.run(ProvaDb.java:28)
    at java.lang.Thread.run(Thread.java:745)
Thread 5 wake up
Connection 5 closed

我不关心异常,问题是连接时间。 我无法弄清楚这种差距的原因,这使我无法在Macos Sierra上进行开发。 任何想法如何解决它?

提前致谢

西蒙

1 个答案:

答案 0 :(得分:4)

尝试在/ etc / hosts中插入这些行:

127.0.0.1       localhost
::1             localhost
127.0.0.1       *[your-machine-name]*
::1             *[your-machine-name]*
127.0.0.1       *[your-machine-name]*.local
::1             *[your-machine-name]*.local