简体   繁体   English

Snowflake JDBC 如何检查主机名/服务器的有效性?

[英]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?我正在测试我的 Snowflake JDBC 连接,但遇到了一个问题:如何处理无效的 Snowflake 主机名称?

eg correct hostname should be 123abc.us-east-1 but I input 123123.us-east-1例如正确的主机名应该是 123abc.us-east-1 但我输入 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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