Reputation: 4625
I am getting the following error when connecting to a SQL Server database using version the Microsoft JDBC Driver:
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server returned an incomplete response. The connection has been closed. ClientConnectionId:98d0b6f4-f3ca-4683-939e-7c0a0fca5931".
We recently upgraded our applications from Java 6 & Java 7 to Java 8. All systems running Java are running SUSE Linux Enterprise Server 11 (x86_64), VERSION = 11, PATCHLEVEL = 3.
Here are the facts I have collected with a Java program that I wrote which simply sequentially opens and closes 1,000 database connections.
The thing that makes my observations unique on this compared to others on the web is that although the problem happens ONLY on Java 8, I cannot get the problem to occur on one of the seemingly identical Linux servers that is running the same Java 8 JVM. Other folks have seen this problem on earlier versions of Java as well, but that has not been our experience.
Any input, suggestions, or observations you may have are appreciated.
Upvotes: 113
Views: 404456
Reputation: 427
What ended up working for me was updating to the latest mssql jdbc driver and then making sure I use the fully qualified domain name as it appears on the SSL certificate of the SQL server.
Upvotes: 0
Reputation: 25
We can find answer of this question in below page:
Answer:";integratedSecurity=true;" + "encrypt=true;trustServerCertificate=true";"
Upvotes: 0
Reputation: 61
Thanks to @Sunil Kumar and @Joce. I used the jar and below syntax:
String myDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String myURL = "jdbc:sqlserver://DB_ipaddress\\DB_instance;" +
"databaseName=DB_name;" +
"user=myusername;" +
"password=mypass;" +
"encrypt=true;" +
"trustServerCertificate=true;"; // here the semicolon will be twice like shown
Connection con = DriverManager.getConnection(myURL);
Upvotes: 6
Reputation: 139
spring.datasource.url=jdbc:sqlserver://<YourServerName>:1433;database=<YourDatabaseName>;encrypt=true;trustServerCertificate=true;
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.hibernate.ddl-auto = create-drop
Upvotes: 7
Reputation: 25
I have faced this issue while connecting SQL database. Faced this issue because of incorrect JAR file.
Connection URL that I used
"jdbc:sqlserver://localhost:8080;databaseName=db_name;encrypt=true;trustServerCertificate=true";
Before I used "sqljdbc4-2.0.jar" JAR file, it won't work for me.
Once I changed this JAR file then it worked fine
mssql-jdbc-8.4.1.jre8.jar
Upvotes: 2
Reputation: 1
If you came here for Ignition 8.1+ error, and you're using MSSQL express on your local host, at your "Connect URL" put this: jdbc:sqlserver://localhost\SQLEXPRESS; encrypt=true;trustServerCertificate=true; URL settings
Upvotes: 0
Reputation: 437
Add ;encrypt=true;trustServerCertificate=true
into connection.url. Example
jdbc:sqlserver://localhost\SQLEXPRESS:1433;databaseName=testdb;encrypt=true;trustServerCertificate=true
And check Compatible compiler version java in pom file with version jre of mssql-jdbc of maven. Like this
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
And
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>11.2.1.jre8</version>
</dependency>
compiler 1.8 and jre8
Upvotes: 9
Reputation: 179
Using openjdk 11 it's possible to add the following properties to the connection URL in order to force the use of SSL
;integratedSecurity=false;encrypt=false;trustServerCertificate=true;
Upvotes: 16
Reputation: 443
For me I add to add Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
between the two lines
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
Connection con = DriverManager.getConnection(connectionUrl);
So the final code for the connection would be:
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(connectionUrl);
Upvotes: 0
Reputation: 606
In case someone arrives here looking for a solution to connect to a database from PhpStorm, just add the following after the port in the URL:
;encrypt=true;trustServerCertificate=true;
I got the solution from this comment: SQL Server JDBC Error on Java 8: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption
Thank you so much! Can confirm it's working on PhpStorm 2022.1.1.
Upvotes: 38
Reputation: 136
Issue got resolved for me when I changed the sqljdbc-4.2.0 jar with mssql-jdbc-8.4.1 jar
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.1.jre8</version>
</dependency>
Upvotes: 4
Reputation: 2507
I have also faced same issue while creating connection to the database with below connection string.
DriverManager.getConnection("jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";user=" + userName + ";password=" + password);
After updating connection string as below, it worked.
Connection con = DriverManager.getConnection("jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";encrypt=true;trustServerCertificate=true;user=" + userName + ";password=" + password);
here i have added encrypt=true;trustServerCertificate=true
after the database name.
Upvotes: 0
Reputation: 19
I have fixed the issue on my local enviroment, it mainly contains two steps below.
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
2.Replace the Driver Class Name additionally:
ru.yandex.clickhouse.ClickHouseDriver
3.Modify the JDBC URL like this:
jdbc:jtds:sqlserver://xxxx:1433;databaseName=xxx
Upvotes: 1
Reputation: 188
I had this issue recently after doing a yum update in a client's server running RedHat 7. Since the above thread did not help me resolve my issue, I am posting this answer.
Issue:- Yum update in RedHat automatically reinstalls OpenJDK , my applications use oracle JDK. Verifying Default JDK:
java version Switch the default version:
update-alternatives --config java
There are 2 programs which provide 'java'.
Enter to keep the current selection[+], or type selection number:
Enter the number alongside the appropriate version you want to us and hit enter.
Upvotes: 0
Reputation: 99
Like @2Aguy wrote, you can change the JVM parameter. In my case I couldn't change it, and used the connection string "sslProtocol" parameter, lowering the connection to TLSV1.
Connection String: jdbc:sqlserver://<HOST>:<PORT>;encrypt=true;trustServerCertificate=true;sslProtocol=TLSv1;database=<DB NAME>
Upvotes: 4
Reputation: 396
If the server name in the connection string does not match the server name in the SQL Server SSL certificate, the following error will be issued: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "java.security.cert.CertificateException: Failed to validate the server name in a certificate during Secure Sockets Layer (SSL) initialization."
This helped me resolve the issue. Was using the localhost in servername, finally changing in jdbc connection string to the same name as the CN was able to connect.
Refer: https://wiki.deepnetsecurity.com/pages/viewpage.action?pageId=1410867 for more info
Upvotes: 1
Reputation: 111
In my case, the issue was because the app was set to use spring-boot-ext-security-starter-credhub-credential and there were some issues with that setup.
So I removed credhub from the manifest file and pom and fetched credentials in a different way; then the error was gone.
Upvotes: 1
Reputation: 1445
In my case i had a sql server using the 3DES_EDE_CBC algorithm, this is disabled by default on jdk 1.8 , so checking the
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/security/java.security
And eliminating the algorithm from:
jdk.tls.disabledAlgorithms=SSLv3, RC4, DES, MD5withRSA, DH keySize < 1024, \ EC keySize < 224, 3DES_EDE_CBC, anon, NULL
Worked for me.
Upvotes: 3
Reputation: 2159
Your url should be like below and add sql sqljdbc42.jar. This will resolve your issue
url = "jdbc:sqlserver://" +serverName + ":1433;DatabaseName=" + dbName + ";encrypt=true;trustServerCertificate=true;
Upvotes: 211
Reputation: 71
This appears to have been fixed in version 4.2 of the MS SQL JDBC driver. I created a program where I connected to the server 1000 times, pausing 100ms between each attempt. With version 4.1 I was able to reproduce the problem every time, although it happened only sporadically. With version 4.2 I was unable to reproduce the problem.
Upvotes: 5
Reputation: 4625
I turned on SSL logging in the Java 8 JVM on a Linux instance which reproduces the problem. SSL logging is turned on using -Djavax.net.debug=ssl:handshake:verbose
. This revealed some useful information.
The workaround that we are using in production and has proven to work for us is to set this parameter on the JVM:
-Djdk.tls.client.protocols=TLSv1
If you want more details, please read on.
On a server where the problem can be reproduced (again, only 5-10% of the time), I observed the following:
*** ClientHello, TLSv1.2
--- 8<-- SNIP -----
main, WRITE: TLSv1.2 Handshake, length = 195
main, READ: TLSv1.2 Handshake, length = 1130
*** ServerHello, TLSv1.2
--- 8<-- SNIP -----
%% Initialized: [Session-79, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256]
** TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
--- 8<-- SNIP -----
Algorithm: [SHA1withRSA]
--- 8<-- SNIP -----
*** Diffie-Hellman ServerKeyExchange
--- 8<-- SNIP -----
*** ServerHelloDone
*** ClientKeyExchange, DH
--- 8<-- SNIP -----
main, WRITE: TLSv1.2 Handshake, length = 133
--- 8<-- SNIP -----
main, WRITE: TLSv1.2 Change Cipher Spec, length = 1
*** Finished
verify_data: { 108, 116, 29, 115, 13, 26, 154, 198, 17, 125, 114, 166 }
***
main, WRITE: TLSv1.2 Handshake, length = 40
main, called close()
main, called closeInternal(true)
main, SEND TLSv1.2 ALERT: warning, description = close_notify
main, WRITE: TLSv1.2 Alert, length = 26
main, called closeSocket(true)
main, waiting for close_notify or alert: state 5
main, received EOFException: ignored
main, called closeInternal(false)
main, close invoked again; state = 5
main, handling exception: java.io.IOException: SQL Server returned an incomplete response. The connection has been closed. ClientConnectionId:12a722b3-d61d-4ce4-8319-af049a0a4415
Notice that TLSv1.2 is selected by the database server and used in this exchange. I've observed that, when connections fail from the problematic linux service, TLSv1.2 is ALWAYS the level which was selected. However, connections do not ALWAYS fail when TLSv1.2 is used. They only fail 5-10% of the time.
Now here is an exchange from a server that does NOT have the problem. Everything else is equal. I.e., connecting to the same database, same version of the JVM (Java 1.8.0_60), same JDBC driver, etc. Notice that, here, TLSv1 is selected by the database server instead of TLSv1.2 as in the faulty server's case.
*** ClientHello, TLSv1.2
--- 8<-- SNIP -----
main, WRITE: TLSv1.2 Handshake, length = 207
main, READ: TLSv1 Handshake, length = 604
*** ServerHello, TLSv1
--- 8<-- SNIP -----
Cipher Suite: TLS_RSA_WITH_AES_128_CBC_SHA
--- 8<-- SNIP -----
%% Initialized: [Session-79, TLS_RSA_WITH_AES_128_CBC_SHA]
** TLS_RSA_WITH_AES_128_CBC_SHA
--- 8<-- SNIP -----
Algorithm: [SHA1withRSA]
--- 8<-- SNIP -----
***
*** ServerHelloDone
*** ClientKeyExchange, RSA PreMasterSecret, TLSv1
--- 8<-- SNIP -----
main, WRITE: TLSv1 Handshake, length = 134
main, WRITE: TLSv1 Change Cipher Spec, length = 1
*** Finished
verify_data: { 26, 155, 166, 89, 229, 193, 126, 39, 103, 206, 126, 21 }
***
main, WRITE: TLSv1 Handshake, length = 48
main, READ: TLSv1 Change Cipher Spec, length = 1
main, READ: TLSv1 Handshake, length = 48
*** Finished
So, when TLSv1 is negotiated between the Linux JVM and the SQL Server, connections are ALWAYS successful. When TLSv1.2 is negotiated, we get sporadic connection failures.
(Note: Java 7 (1.7.0_51) always negotiates TLSv1, which is why the problem never occurred for us with a Java 7 JVM.)
The open questions we still have are:
Update 6/10/2017: This posting from Microsoft describes the problem and their proposed solution.
Resources:
Java 8 , JCE Unlimited Strength Policy and SSL Handshake over TLS
http://blogs.msdn.com/b/saponsqlserver/archive/2013/05/10/analyzing-jdbc-connection-issues.aspx
https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html#descPhase2
https://blogs.oracle.com/java-platform-group/entry/java_8_will_use_tls
Upvotes: 49
Reputation: 21
I've also hit this issue on Windows Server 2012 R2, using JDBC driver 4.0 & 4.1 with Java 7. This Microsoft article pins the blame on DHE ciphersuites, and recommends disabling them or reducing their priority if you cannot upgrade to JDBC driver 4.2
Upvotes: 1
Reputation: 590
Microsoft Recently open sourced their driver. One can see mssql-jdbc driver activity on GitHub. I guess latest preview version is 6.1.5.
Also you can find all preview versions on maven too. Supporting both JDK7 & JDK 8.
Upvotes: 1
Reputation: 61
Before you upgrade SQL JDBC Driver, check the compatibility first:
Source: https://www.microsoft.com/en-us/download/details.aspx?id=11774
Upvotes: 6