Search code examples
javajdbcdatabricksazure-databricks

[Databricks][DatabricksJDBCDriver](500593) Communication link failure. Failed to connect to server. Reason: HTTP Response code: 403


I am trying to connect to databricks using java code. Can someone help me please? Here is the code so far I have got::

package digital.eComm.ui.tests;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties;

public class DatabricksSetup {


    public static void main(String[] args) throws SQLException {
        String url = "jdbc:databricks://XXXX.azuredatabricks.net:443/default;transportMode=http;ssl=1;httpPath=sql/protocolv1/o/XXXXX;AuthMech=3;UID=token;PWD=XXXXXX";
        String username = "token";
        String password = "XXXX"; //Token generated from databricks profile page.

        Connection connection = DriverManager.getConnection(url, username, password);


        System.out.println("Database connected!");
        if(connection != null){
            System.out.println("Connection Established");

        }
        else {
            System.out.println("Connection Failed");
        }

    }

}

Below dependancy is already added :

  "com.databricks:databricks-jdbc:2.6.25"

Error :

Exception in thread "main" java.sql.SQLException: [Databricks][DatabricksJDBCDriver](500593) Communication link failure. Failed to connect to server. Reason: HTTP Response code: 403, Error message: Unknown.
    at com.databricks.client.hivecommon.api.HS2Client.handleTTransportException(Unknown Source)
    at com.databricks.client.spark.jdbc.DowloadableFetchClient.handleTTransportException(Unknown Source)
    at com.databricks.client.hivecommon.api.HS2Client.openSession(Unknown Source)
    at com.databricks.client.hivecommon.api.HS2Client.<init>(Unknown Source)
    at com.databricks.client.spark.jdbc.DowloadableFetchClient.<init>(Unknown Source)
    at com.databricks.client.spark.jdbc.DownloadableFetchClientFactory.createClient(Unknown Source)
    at com.databricks.client.hivecommon.core.HiveJDBCCommonConnection.connectToServer(Unknown Source)
    at com.databricks.client.spark.core.SparkJDBCConnection.connectToServer(Unknown Source)
    at com.databricks.client.hivecommon.core.HiveJDBCCommonConnection.establishConnection(Unknown Source)
    at com.databricks.client.spark.core.SparkJDBCConnection.establishConnection(Unknown Source)
    at com.databricks.client.jdbc.core.LoginTimeoutConnection.connect(Unknown Source)
    at com.databricks.client.jdbc.common.BaseConnectionFactory.doConnect(Unknown Source)
    at com.databricks.client.jdbc.common.AbstractDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at digital.eComm.ui.tests.DatabricksSetup.main(DatabricksSetup.java:16)
Caused by: com.databricks.client.support.exceptions.ErrorException: [Databricks][DatabricksJDBCDriver](500593) Communication link failure. Failed to connect to server. Reason: HTTP Response code: 403, Error message: Unknown.
    ... 16 more

Solution

  • 403 is a clear refusal. Server telling you: I know who you are, but you can't do what you're trying to do.

    Most likely because the driver is trying to use user/password mechanism to authenticate instead of token in URL because you're explicitly passing the params to getConnection().

    replace DriverManager.getConnection(url, username, password); with DriverManager.getConnection(url); and try.

    Full example from databricks:

    Databricks JDBC Driver on Maven

    Java and JVM developers use JDBC as a standard API for accessing databases. Databricks JDBC Driver is now available on the Maven Central repository, letting you use this driver in your build system and CI/CD runs. To include it in your Java project, add the following entry to your application’s pom.xml:

        <dependency>
          <groupid>com.databricks
          <artifactid>databricks-jdbc</artifactid>
          <version>2.6.25-1</version>
        </groupid></dependency>
    

    Here is some sample code to query data using JDBC driver:

    import java.sql.*;
    
    public static void main(String[] args) throws Exception {
        // Open a connection
    
        // replace the values below
        String token = "dapi*****";
        String url = "jdbc:databricks://********.cloud.databricks.com:443/default;" +      
                     "transportMode=http;ssl=1;AuthMech=3;httpPath=sql/protocolv1/o/*****;" +
                     "UID=token;" +
                     "PWD=" + token;
    
        try (Connection conn = DriverManager.getConnection(url);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM samples.nyctaxi.trips");) {
            // Extract data from result set
            while (rs.next()) {
                // Retrieve by column name
                System.out.print("ID: " + rs.getString("col_name"));
            }
        } 
    }