Getting adal4j/AuthenticationException while connecting to Azure SQL from Google Dataproc Cluster - java

Please hear me out.
I am able to successfully write to Azure SQL server table using Apache Spark connector in Dataproc cluster. But then while testing I found sometime that the SQL server database in not available and because of this the write operation fails.
So, I decided to add an exception logic to check if the database is available or not using the following code before writing to Azure DB,
var connection: Connection = null
val url = "jdbc:sqlserver://***.database.windows.net:1433;databaseName=some_db;encrypt=true;trustServerCertificate=true;authentication=ActiveDirectoryPassword"
val user = "xxxx"
val password = ******
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
connection = DriverManager.getConnection(url, user, password)
log.info(s"Successfully established connection on attempt: "+attempts)
return
}
This code works fine in my local machine. But while running the same in Dataproc I am getting the below error,
Exception in thread "main" java.lang.NoClassDefFoundError: com/microsoft/aad/adal4j/AuthenticationException
I already went though this similar issue discussed in SO. But it's bit different for me as the Apache Spark connector to write to SQL server is working fine. I just want to have the code DriverManager.getConnection(url, user, password) work to test the connection before my write.

Related

Socket write error with activejdbc

I have a pretty simple commandline app that runs inside of eclipse that connects to an Oracle 12 database and issues a pretty simple query. I'm getting the following exception:
Exception in thread "main" org.javalite.activejdbc.InitException: Failed to connect to JDBC URL: jdbc:oracle:thin:#oratest2a.emspic.org:1521:workstst
at org.javalite.activejdbc.DB.open(DB.java:93)
at org.javalite.activejdbc.Base.open(Base.java:64)
at org.emspic.works.RegistrationUtil.doMain(RegistrationUtil.java:135)
at org.emspic.works.RegistrationUtil.main(RegistrationUtil.java:75)
Caused by: java.sql.SQLRecoverableException: IO Error: Connection reset by peer: socket write error
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:752)
at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
Code throwing the exception is basically this:
// use properties to open the datasource for work
System.out.println("Opening database connection.");
logger.info("Opening database");
String dbDriver = dbProps.get("dbDriver");
String dbURL = dbProps.get("dbURL");
String username = dbProps.get("username");
String password = dbProps.get("password");
Base.open(dbDriver, dbURL, username, password);
logger.info("Database successfully opened.");
Everything I've googled indicates the problem is a "network issue"...except that on this same machine, I am currently connected to the very table I'm trying to open via TOAD...and I also am running a jboss as server connecting to that very same database...so there is no way it is a network problem.
I'm at a total loss.
Java1.8, ojdbc7.jar, all running on a Microsoft Surface Pro 3..., Oracle version is 12.0.2 running remotely.
Your exception has nothing to do with AcitveJDBC. There is a problem with your Oracle configuration. I bet you will get the same exception if you try just JDBC with the same parameters. When/if you fix your issue with JDBC, ActiveJDBC will work just fine.

Could not create connection to database server. Attempted reconnect 3 times. Giving up

I am trying to connect to db4free.net but am getting this error. It was working yesterday. I was able to connect yesterday but today, I can't.
URL:
jdbc:mysql://db4free.net:3306/mydb?autoReconnect=true
Line of code error is at:
connection = DriverManager.getConnection(url, username, password);
JDBC driver is loading correctly so that shouldn't be the error.
That's what you get for free. I would suggest using a different database hosting provider, or check into hosting the database server yourself.

Unable to get SQL Server connection object

I wrote a simple code to connect to SQL Server database and to does a simple select statement, this code runs successfully on my local machine over VPN but when I deploy the same code in the servers, then my code is stopping at getting the DB connection object.
It is not even throwing any timeout exception, so I thought of a port issue and checked using telnet which is successful. Can anyone please suggest why this might happen? I am able to get connection object of the same DB from my local machine over VPN.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = props.getProperty("DBURI");
Connection conn=null;
System.out.println(connectionUrl.trim());
conn = DriverManager.getConnection(connectionUrl.trim());
Problem with the sqljdbc jar file , Used any 32 bit one , After replacing the jar , everything worked fine. Thanks

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

I have a google app engine connected project with which I am trying to connect to a google cloud sql database.I upload my project to google and via it I try to connect to the db.
my connection URL is as follows->
Class.forName("com.mysql.jdbc.GoogleDriver");
String password="";
url = "jdbc:google:mysql://my-project-id:my-instance-name/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";
Connection conn = DriverManager.getConnection(url);
it is at the point where I attempt to get a connection with the url that I get an exception
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
I then added the following string to my url->
&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
then received a different exception-->
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 10 times. Giving up.
I have tried pinging the ip address of the db instance and it works fine.
Do I need to give any explicit permissions in my cloud sql instance for it to be able to connect?
Any suggestions are welcome.
The strange thing is that I am able to connect to the db using eclipse by going to project->google->app engine->usegoogle cloud sql instance->configure.I use the same user name (root) and password ("") here as in the url in the code but for some reason it refuses to connect through the code.
I have also enabled the
<use-google-connector-j>true</use-google-connector-j>
tag in my
appengine-web.xml
I changed the root password from "" to some value and I am able to connect via mysql client
and I can access the database, but on giving the same password in code it refuses to connect.
Any suggestions will be appreciated.
Thanks,
Laura
Instance Id by default contains the project Id. So don't explicitly add project Id again.
Project ID: sampleapp
Instance Id: sampleapp:instance1 (This is how google cloud sql creates by default)
Connections string:
Correct:
jdbc:google:mysql://sampleapp:instance1/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";
In correct
jdbc:google:mysql://sampleapp:sampleapp:instance1/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";

Connect Java to MS SQL Server Database using Windows Authentication

I have found some similar questions but with no helpful answers.
I need to manipulate an MS SQL Server database named PDPJ_Student from my java application and i cannot connect to it. I get the following error:
[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Login failed for user 'LAPTOP-TITI/Titi'. The user is not associated with a trusted SQL Server connection.
and my source looks like this:
Connection con = null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://LAPTOP-TITI;DatabaseName=PDPJ_Student", "LAPTOP-TITI/Titi", "");
}
catch(Exception e){
System.out.println("Error at connection");
e.printStackTrace();
}
When i start MS SQL Server, at authenticatin, the SERVER NAME says: LAPTOP-TITI; and the Authentication is set to Windows Authentication. The user name field says LAPTOP-TITI/Titi but it is disabled, as well as the Password field, which is empty.
I have also tried:
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://LAPTOP-TITI;DatabaseName=PDPJ_Student", integratedSecurity=true);
but still nothing ..
What am I doing wrong ? Can you give me some indications please?
p.s I am not allowed to change my Authentication type, and even if I try changing to SQL Server Authentication, it doesn`t let me create any new users
Your question doesn't mention whether your client machine is running Windows or non-windows. This topic on MSDN suggests that if your client is using Linux, you'll need to purchase the DataDirect driver to get Kerberos authentication working. If you're on Windows, it should be possible with Microsoft's JDBC driver.

Categories

Resources