1

Background: My local MS SQL database is configured as following: TCP/IP disabled with dynamic port (default setting)

When using .Net to connect to my local database, I don't need to enable TCP connection in SQL Server Configuration Manager or specifying the port number in connection string. I just need to put this in app.config

<add name="MyDatabase" connectionString="Initial Catalog=MyDatabase;Data Source=localhost\SQLEXPRESS;Integrated Security=SSPI;"/>

However, I have to enable TCP in order to connect to it using JDBC. I wonder whether there is any way to get around it?

Laiwen Yi
  • 33
  • 3
  • Why a `C#` and `.Net` tags are here, if you are asking about java? – vasily.sib Nov 26 '18 at 03:28
  • 1
    also, after just 3.52 seconds of googling i found [this article at MS docs](https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-2017) – vasily.sib Nov 26 '18 at 03:30
  • @vasily.sib That link does not really address the question though. – Mark Rotteveel Nov 26 '18 at 07:35
  • ok, another search ("jdbc named pipes") and [here it is](https://stackoverflow.com/questions/11345746/connecting-to-sql-server-localdb-using-jdbc/24412037#24412037) – vasily.sib Nov 26 '18 at 09:55
  • Found that .Net uses shared memory to connect to SQL server. Unfortunately I wasn't able to find any JDBC driver for SQL server which supports this protocol. My intent was to avoid enabling additional protocols when using JDBC(only shared memory is enabled by default). Since it's not possible in this case, I'd rather go with TCP than named pipe. – Laiwen Yi Dec 11 '18 at 06:55

1 Answers1

1

The Microsoft SQL Server JDBC driver is a Type 4 (pure Java) implementation, and it only supports TCP/IP sockets to communicate with SQL Server, therefor it requires TCP/IP to be enabled on the server instance. There is no workaround that magically allows you to connect otherwise.

As also indicated by the comment of vasily.sib, you can switch to jTDS which supports named pipes. See also Connecting to SQL Server LocalDB using JDBC. Just be aware that jTDS is lagging in supporting newer SQL Server versions and newer JDBC specifications.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197