question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JDBC Driver hangs when trying to connect to Snowflake

See original GitHub issue

I tried to run the JDBC example application to connect to my database.

When it tries to create a connection with the database, it seems to hang indefinitely on the DriverManager.getConnection() function. I stepped through the code and it seems that it fails when it tries to execute the POST request for login request. I think it is unable to create or access the socket, but I’m not sure.

I was thinking it could be a database issue or a network issue but it successfully connects and queries the database when run with either Node or Python.

Here’s the JDBC example:

/*
 * Copyright (c) 2012-2019 Snowflake Inc. All rights reserved.
 *
 * - Download the latest version of the driver (snowflake-jdbc-<ver>.jar) from Maven:
 *       https://repo1.maven.org/maven2/net/snowflake/snowflake-jdbc/<ver>
 * - Download this file (SnowflakeJDBCExample.java) into the same directory. 
 * - Edit this file (SnowflakeJDBCExample.java) and set the connection properties correctly.
 * - From the command line, run:
 *     javac SnowflakeJDBCExample.java
 * - From the command line, run:
 *   - Linux/MacOS:
 *     java -cp .:snowflake-jdbc-<ver>.jar SnowflakeJDBCExample
 *   - Windows:
 *     java -cp .;snowflake-jdbc-<ver>.jar SnowflakeJDBCExample
 *
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class SnowflakeJDBCExample {

  public static void main(String[] args) throws Exception {
    // get connection
    System.out.println("Create JDBC connection");
    Connection connection = getConnection();
    System.out.println("Done creating JDBC connection\n");

    // create statement
    System.out.println("Create JDBC statement");
    Statement statement = connection.createStatement();
    System.out.println("Done creating JDBC statement\n");

    // create a table
    System.out.println("Create demo table");
    statement.executeUpdate("create or replace table demo(c1 string)");
    statement.close();
    System.out.println("Done creating demo table\n");
 
    // insert a row
    System.out.println("Insert 'hello world'");
    statement.executeUpdate("insert into demo values ('hello world')");
    statement.close();
    System.out.println("Done inserting 'hello world'\n");

    // query the data
    System.out.println("Query demo");
    ResultSet resultSet = statement.executeQuery("select * from demo");
    System.out.println("Metadata:");
    System.out.println("================================");

    // fetch metadata
    ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
    System.out.println("Number of columns=" +
                       resultSetMetaData.getColumnCount());
    for (int colIdx = 0; colIdx < resultSetMetaData.getColumnCount();
         colIdx++) {
      System.out.println("Column " + colIdx + ": type=" +
                         resultSetMetaData.getColumnTypeName(colIdx + 1));
    }

    // fetch data
    System.out.println("\nData:");
    System.out.println("================================");
    int rowIdx = 0;
    while (resultSet.next()) {
      System.out.println("row " + rowIdx + ", column 0: " +
                         resultSet.getString(1));
    }
    resultSet.close();
    statement.close();
    connection.close();
  }


  private static Connection getConnection()
      throws SQLException {
    try {
      Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");
    } catch (ClassNotFoundException ex) {
      System.err.println("Driver not found");
    }

    // build connection properties
    Properties properties = new Properties();
    properties.put("user", "");        // replace "" with your user name
    properties.put("password", "");    // replace "" with your password
    properties.put("account", "");     // replace "" with your account name
    properties.put("warehouse", "");   // replace "" with target warehouse name
    properties.put("db", "");          // replace "" with target database name
    properties.put("schema", "");      // replace "" with target schema name
    //properties.put("tracing", "on"); // optional tracing property

    // replace <account_name> with the name of your account, as provided by Snowflake
    // replace <region_id> with the name of the region where your account is located (if not US West)
    // remove region ID segment (not needed) if your account is located in US West
    String connectStr = "jdbc:snowflake://<account_name>.<region_id>.snowflakecomputing.com";
    return DriverManager.getConnection(connectStr, properties);
  }
}

PS: I did replace the empty fields with my information

Any ideas what the problem might be?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
howryucommented, Jun 19, 2019

Probably because python or Node take proxy settings from environment variable. I don’t remember if JDBC honor environment variable or not.

0reactions
patelneel55commented, Jun 19, 2019

I’m curious, I had not passed the proxy settings in Node and Python, why did it work there and not in Java?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing Snowflake via JDBC - 403 errors for requests ...
I am trying to connect to Snowflake via a private link from an EC2 within an AWS VPC. I am able to use...
Read more >
JDBC driver encountered communication error while ...
We have entered username, password and all other details and click on Test connection. Getting JDBC driver encountered communication error.
Read more >
Configuring the JDBC Driver - Snowflake Documentation
This topic describes how to configure the JDBC driver, including how to connect to Snowflake using the driver. Note. The connection parameters are...
Read more >
Using the JDBC Driver - Snowflake Documentation
The Snowflake JDBC Driver supports asynchronous queries (i.e. queries that return control to the user before the query completes). Users can start a...
Read more >
I am using Snowflake JDBC driver, through a JDBC test ...
Where is the application that you are testing the connection with? Are you able to run the same query with a LIMIT 100...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found