I am trying to connect to an Azure SQL server using the JTDS JDBC driver. I have gotten the connection to work previously, but overnight it now hangs on
DriverManager.getConnection(url);
It gives me no exception, and continues running past the 30 second timeout that is default in the connection string. Full code is below
try {
StrictMode.ThreadPolicy pol = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(pol);
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String url = "jdbc:jtds:sqlserver://{server}.database.windows.net:1433;database=PROD;user={username}@{server};password={password};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
Connection c = DriverManager.getConnection(url);
c.close();
}
catch(Exception e)
{
new AlertDialog.Builder().setTitle("e").setMessage(e.getMessage()).setNegativeButton("e", null).show();
}
I am able to connect to and query the database using SSMS, so as far as I can tell the database is working correctly. Though I am a very beginner on using SQL servers so I may be wrong. I found that the SQL server version is 12.0.2000.8
. JTDS documentation suggests changing the TDS version in some cases, but from what I tried that didnt solve my issues. I am using JTDS version 1.3.1.
Again, this method was working fine yesterday and now I am having issues. Nothing in the project was changed. Yesterday I wasn't closing the connection after creating it, so I have a hunch that there is some issue with that on the SQL Server.
Now, a couple hours later, the connection went through after a few seconds, and when I tried to rerun the app it had the same issue.
Found my solution:
Seems that the android emulator had an issue with DNS. Following Android Studio - Android Emulator Wifi Connected with No Internet this thread I was able to manually set my DNS to 8.8.8.8
and 8.8.4.4
which resolved the issue.
Congratulation that you have found the solutions:
Seems that the android emulator had an issue with DNS. Following Android Studio - Android Emulator Wifi Connected with No Internet this thread I was able to manually set my DNS to 8.8.8.8
and 8.8.4.4
which resolved the issue.
I post this as answer and this can be beneficial to other community members.