简体   繁体   中英

Snowflake JDBC How to check hostname / server validity?

I'm in the process of testing my Snowflake JDBC connection and I'm running to an issue: How do I handle invalid Snowflake host machine names?

eg correct hostname should be 123abc.us-east-1 but I input 123123.us-east-1

Here is the code:

public static Connection getConnection(connectConfigs sfconfig) {
        try {
            Class.forName("com.snowflake.client.jdbc.SnowflakeDriver");
        } catch (ClassNotFoundException ex) {
            System.err.println("Driver not found");
        }

        try {
            // build connection properties
            Properties properties = new Properties();
            properties.put("user", sfconfig.getSfUser());     // replace "" with your username
            properties.put("password", sfconfig.getSfPassword()); // replace "" with your password
            properties.put("account", sfconfig.getSfURL());  // replace "" with your account name
            properties.put("db", sfconfig.getSfDatabase().toUpperCase());       // replace "" with target database name
            properties.put("schema", sfconfig.getSfSchema().toUpperCase());   // replace "" with target schema name
            properties.put("loginTimeout", 30);
            //properties.put("tracing", "on");

            // create a new connection
            String connectStr = System.getenv("SF_JDBC_CONNECT_STRING");

            // use the default connection string if it is not set in environment
            if (connectStr == null) {
                connectStr = "jdbc:snowflake://" + sfconfig.getSfURL() + ".snowflakecomputing.com"; // replace accountName with your account name
            }
            return DriverManager.getConnection(connectStr, properties);
        } catch (SQLException e) {
            System.err.println("some error here");
        }
        return null;
    }

If I provide an invalid hostname, all this gets printed to the console:

May 24, 2021 4:34:05 PM net.snowflake.client.jdbc.RestRequest execute
SEVERE: Stop retrying since elapsed time due to network issues has reached timeout. Elapsed: 69,280(ms), timeout: 60,000(ms)
May 24, 2021 4:34:05 PM net.snowflake.client.jdbc.RestRequest execute
SEVERE: Error response: HTTP Response code: 403, request: POST https://123123.us-east-1.snowflakecomputing.com:443/session/v1/login-request?databaseName=TEST_CONNECT&schemaName=FRIEND&requestId=b4c13959-cbbf-45b8-acf6-f758176545e5 HTTP/1.1
May 24, 2021 4:34:05 PM net.snowflake.client.core.HttpUtil executeRequestInternal
SEVERE: Error executing request: POST https://123123.us-east-1.snowflakecomputing.com:443/session/v1/login-request?databaseName=TEST_CONNECT&schemaName=FRIEND&requestId=b4c13959-cbbf-45b8-acf6-f758176545e5 HTTP/1.1
May 24, 2021 4:34:05 PM net.snowflake.client.jdbc.SnowflakeUtil logResponseDetails
SEVERE: Response status line reason: Forbidden
May 24, 2021 4:34:05 PM net.snowflake.client.jdbc.SnowflakeUtil logResponseDetails
SEVERE: Response content: <html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
</body>
</html>

How can I handle this gracefully instead of it printing this huge mess to console?

Thanks ahead of time!

Adding this one line to the connection properties solved my issue.

properties.put("tracing", "OFF");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM