SQL Server "Application Intent=readonly" not working JDBC
Asked Answered
S

2

5

Im am trying to hit the secondary node on a sql server 2012 DB using "ApplicationIntent=readonly" in the connection string.

It is still hitting the primary node, not sure why.

I'm using sqljdbc4-4.0 jar

java JDBC call:

ResultSet results = null;
PreparedStatement preparedStatement = null;
Connection conn = null;

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

String dbString = "jdbc:sqlserver://*****;applicationIntent=ReadOnly;databaseName=****;user=*****;password=****;";
    
conn = DriverManager.getConnection(dbString);
preparedStatement = conn.prepareStatement("some_sql");

results = preparedStatement.executeQuery(); 
Shem answered 13/7, 2016 at 15:54 Comment(2)
Do you have a answer for this? We are also experiencing this problemHelen
Did you ever figure out how to get it to work properly?Mannuela
S
3

You should connect to availability group listener (AGL) address(es). you can use below query to find availability group listener addresses:

select * from sys.availability_group_listener_ip_addresses 

after you find the AGL address your jdbc connection string should be :

jdbc:sqlserver://[AGL-ADDRESS]:1433;databaseName=[DB_NAME];user=[username];password=[password];applicationIntent=ReadOnly
Smile answered 12/9, 2020 at 10:23 Comment(0)
S
0

For DBeaver Users:

enter image description here

enter image description here

For Copy Paste: ApplicationIntent ReadOnly

enter image description here

enter image description here

Skate answered 4/11 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.