简体   繁体   English

JDBC-Windows上的连接极其缓慢,Linux上的罚款

[英]JDBC - Connection extremely slow on Windows, Fine on Linux

Using JDBC to connect to SQL Server on a Windows Server 2008 computer, I've ran into an extremely confusing problem. 使用JDBC连接Windows Server 2008计算机上的SQL Server,我遇到了一个非常令人困惑的问题。

While it works great on my Linux laptop both using Microsoft's JDBC driver and jTDS, It works extremely slow when I move the application to a Windows running device a single SQL command would take 4 to 10 seconds to execute! 虽然它在使用Microsoft的JDBC驱动程序和jTDS的Linux笔记本电脑上都可以很好地工作,但是当我将应用程序移动到Windows运行设备上时,它的运行速度非常慢,单个SQL命令执行将花费4到10秒!

I've tried all of the following techniques to connect to the database server, almost all of them are fast on Linux and very slow on Windows. 我已经尝试了以下所有技术来连接数据库服务器,几乎所有技术在Linux上都是很快的,而在Windows上却很慢。 Using jTDS data source, I've learned that it works fine on Windows 8 but ALWAYS slow when I move the code on to the computer running the database itself (windows server). 使用jTDS数据源,我了解到它在Windows 8上可以正常工作,但是当我将代码移到运行数据库本身的计算机(Windows服务器)上时,速度总是很慢。

// =============== jTDS

    JtdsDataSource ds = new JtdsDataSource();

    ds.setUser(DB_USERNAME);
    ds.setPassword(DB_PASSWORD);
    ds.setServerName(SERVER_ADDRESS);
    ds.setPortNumber(SERVER_PORT);
    ds.setDatabaseName(DATABASE_NAME);
    ds.setLoginTimeout(server.SQL_LOGIN_TIMEOUT);

    try {
        _conn_ = ds.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    }


    // =============== Microsoft

    try {

        String connectionUrl =
                String.format("jdbc:sqlserver://%s:%d;" +
                "databaseName=%s;" +
                "ssl=require", SERVER_ADDRESS, SERVER_PORT, DATABASE_NAME);

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        this._conn_ = DriverManager.getConnection(connectionUrl, DB_USERNAME, DB_PASSWORD);

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new RuntimeException("JDBC not loaded");
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    }

    // =============== Apache

    String connectionUrl = "jdbc:sqlserver://"+SERVER_ADDRESS+";database="+DATABASE_NAME+";integratedSecurity=false;";
    String jtdsConnectionUrl = "jdbc:jtds:sqlserver://"+SERVER_ADDRESS+":"+SERVER_PORT+"/"+DATABASE_NAME+"";
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jtdsConnectionUrl, DB_USERNAME, DB_PASSWORD);

    PoolableConnectionFactory poolableConnectionFactory
            = new PoolableConnectionFactory(connectionFactory, null);
    poolableConnectionFactory.setDefaultAutoCommit(true);
    poolableConnectionFactory.setDefaultReadOnly(false);

    GenericObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);


    this.pooledDataSource = new PoolingDataSource(connectionPool);

It's already a week I'm stuck on this, any kind of help is appreciated. 我已经坚持了一周,我们将提供任何帮助。

Well this is not really a true solution, but works. 嗯,这不是真正的解决方案,但是可以。 After hours of working on this and trying almost everything (from writing a custom connection pool to using Apache DBCP) I came to realize that the problem starts to exist, AFTER a TCP connection is made to my server. 经过数小时的工作并尝试了几乎所有内容(从编写自定义连接池到使用Apache DBCP),我终于意识到问题是在与服务器建立TCP连接之后开始存在的。

I came to realize if I reconnect to SQL database with each connection made to the server, the extremely slow behavior simply degrades to not so fast, that is a transaction taking about 50ms instead of 5 seconds which is almost good enough. 我开始意识到,如果我与服务器的每个连接都重新连接到SQL数据库,那么极其缓慢的行为只会降级到不那么快,也就是说,一个事务大约需要50毫秒而不是5秒,这已经足够了。

I still have no idea why this happens and my bet is on a Windows Server or VMWare network bug since this behavior changes by changing platforms. 我仍然不知道为什么会这样,我的赌注是在Windows Server或VMWare网络错误上,因为此行为会通过更改平台而改变。 I hope this helps if anyone else is experiencing same problem. 我希望这对其他任何人遇到同样的问题有帮助。

I dont know if this will help, but as I can see, you are not referring to JTDS driver but to Microsoft JDBC Driver for SQL Server. 我不知道这是否有帮助,但是正如我所看到的,您不是在指的是JTDS驱动程序,而是指用于SQL Server的Microsoft JDBC驱动程序。 Try changing 尝试改变

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

to: 至:

Class.forName("net.sourceforge.jtds.jdbc.Driver");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM