2

I need to set the socketTimeout property when establishing the connection from database using jdbc. I am using sqljdbc4 to connect with SQL SERVER 2008 R2. Here is my code.

java.util.Properties info = new java.util.Properties();
info.setProperty("user", informationStoreDefinition.getProperties().get(USER));
info.setProperty("password", informationStoreDefinition.getProperties().get(PASSWORD));
info.setProperty("socketTimeout", "10");
info.setProperty("loginTimeout", "10");
testConnection = DriverManager.getConnection(informationStoreDefinition.getProperties().get(DB_URL), info);

I did not find the socket timeout effective in my case. I have set it for 10 seconds but it throws time-out after 30 seconds. I will set the time out to 30 seconds but that need to work at 10 seconds first.

user207421
  • 305,947
  • 44
  • 307
  • 483
Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88
  • Socket timeout only applies to reads after connecting, it doesn't apply to the connect itself. You need to find a connect timeout setting (I'm not sure if it exists or not for SQL Server). – Mark Rotteveel Oct 07 '16 at 09:49
  • @MarkRotteveel Yes it has a `connectTimeout` setting and I tried it also. It thrown an Exception - com.microsoft.sqlserver.jdbc.SQLServerException: `The TCP/IP connection to the host xxx.xxx.x.xxx, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.` and it takes 30 seconds even I set it to 5 sec. – Ashish Pancholi Oct 07 '16 at 10:03
  • https://www.connectionstrings.com/all-sql-server-connection-string-keywords/ – Ashish Pancholi Oct 07 '16 at 10:03
  • That site lists _"connection string properties for the ADO.NET SqlConnection object"_, in other words it is for C#, not for the SQL Server JDBC driver. – Mark Rotteveel Oct 07 '16 at 10:58

0 Answers0