Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Establishing multiple connections in parallel can throw an IndexOutOfBoundsException #1852

Closed
Chrriis opened this issue Jun 24, 2022 · 2 comments
Projects
Milestone

Comments

@Chrriis
Copy link

Chrriis commented Jun 24, 2022

Driver version

11.1.2

Problem description

At startup, we create many connections in parallel to many servers to check their status/versions. Occasionally, with this version of the driver, we get an exception:

Exception in thread "ConfigDatabaseData - get rights from DB" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
	at java.base/java.util.Objects.checkIndex(Objects.java:359)
	at java.base/java.util.ArrayList.get(ArrayList.java:427)
	at com.microsoft.sqlserver.jdbc/com.microsoft.sqlserver.jdbc.SocketFinder.getInetAddressByIPPreference(IOBuffer.java:2710)
	at com.microsoft.sqlserver.jdbc/com.microsoft.sqlserver.jdbc.SocketFinder.getSocketByIPPreference(IOBuffer.java:2753)
	at com.microsoft.sqlserver.jdbc/com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2399)
	at com.microsoft.sqlserver.jdbc/com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:766)
	at com.microsoft.sqlserver.jdbc/com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:3298)
	at com.microsoft.sqlserver.jdbc/com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2948)
	at com.microsoft.sqlserver.jdbc/com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2788)
	at com.microsoft.sqlserver.jdbc/com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1661)
	at com.microsoft.sqlserver.jdbc/com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1067)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)

It seems that access to "addressList" in "IOBuffer" is not synchronized and it is mutated by some of the connections.

I am happy to help testing fixes if you cannot easily reproduce the issue (although the DLL issue #1850 is a bit of a pain for our setup).

@Chrriis
Copy link
Author

Chrriis commented Jun 24, 2022

Here is a self-contained example:

public class TestMsSqlJdbcParallelConnections {
    public static void main(String[] args) throws Exception {
        Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver.class.getName());
        String connectionString = "jdbc:sqlserver://?databaseName=xxxxx;serverName=localhost;userName=xxxxx;password=xxxxx;instanceName=SQLEXPRESS;packetSize=16384;prepareMethod=prepare;trustServerCertificate=true";
        for(int i=0; i<100; i++) {
            new Thread() {
                @Override
                public void run() {
                    try {
                        Connection conn = DriverManager.getConnection(connectionString);
                        conn.close();
                    } catch (SQLException e) {
                    }
                }
            }.start();
        }
    }
}

Simply change the "xxxxx" with proper data so that it actually works. Perhaps the fact that the database is on localhost on a fast machine makes the problem appear more often: with the above I get the error on 3 out of 4 runs.

@tkyc
Copy link
Member

tkyc commented Jun 24, 2022

Hey @Chrriis, thanks for reporting this. We'll look into this issue.

@lilgreenbird lilgreenbird added this to Under Investigation in MSSQL JDBC via automation Jun 27, 2022
@lilgreenbird lilgreenbird added this to the 11.2.0 milestone Jun 27, 2022
@lilgreenbird lilgreenbird moved this from Under Investigation to In progress in MSSQL JDBC Jun 28, 2022
@tkyc tkyc closed this as completed Jul 11, 2022
MSSQL JDBC automation moved this from In progress to Closed Issues Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
MSSQL JDBC
  
Closed Issues
Development

No branches or pull requests

3 participants