简体   繁体   English

雪花网连接器螺纹安全吗?

[英]Is snowflake net connector thread safe?

Regarding the snowflake .NET connector: https://github.com/snowflakedb/snowflake-connector-net , I can find nothing in the documentation or source code to suggest that connection pooling is supported, and because the connection itself is backed by HttpClient , and we know that HttpClient should be reused rather than created/disposed constantly , what's the best way to use the snowflake .NET connector when you'll be making many queries across threads?关于雪花 .NET 连接器: https://github.com/snowflakedb/snowflake-connector-net ,我在文档或源代码中找不到任何内容表明支持连接池,并且因为连接本身由HttpClient支持,而且我们知道HttpClient 应该被重用而不是不断地创建/处置,当您跨线程进行许多查询时,使用雪花 .NET 连接器的最佳方法是什么?

Note: I'm not planning on changing any properties of the connection once it's created (schema, database, etc.).注意:我不打算在创建连接后更改连接的任何属性(模式、数据库等)。

For example:例如:

// application startup registers this provider as a singleton
public class SnowflakeConnectionProvider : IDisposable
{
  private IDbConnection _conn;

  public SnowflakeConnectionProvider()
  {
    _conn = new SnowflakeDbConnection();
    _conn.ConnectionString = "connectionString";
    _conn.Open();
  }

  public IDbConnection conn { get => _conn; }

  public Dispose() => _conn.Close();
}

Now, is it safe for multiple threads to share the one SnowflakeDbConnection like so:现在,多个线程像这样共享一个SnowflakeDbConnection是否安全:

public class Worker
{
  public Worker(SnowflakeConnectionProvider provider)
  {
    IDbConnection conn = provider.conn;
    
    IDbCommand cmd = conn.CreateCommand();
    cmd.CommandText = "select * from t";
    IDataReader reader = cmd.ExecuteReader();
                
    while(reader.Read())
    {
        Console.WriteLine(reader.GetString(0));
    }
  }
}

Summary from GitHub: The connector is thread safe. GitHub 总结:连接器是线程安全的。

As long as you do not use any session variables in your snowflake code, you can have multiple threads sharing the same SnowflakeDbConnection.只要您不在雪花代码中使用任何 session 变量,就可以让多个线程共享同一个 SnowflakeDbConnection。 It does not matter if you are reusing threads or if you are creating new threads all the time.无论您是在重用线程还是一直在创建新线程都没有关系。

Even if you are constantly creating and disposing connections from multiple different threads, a single HttpClient is created once and then reused and shared by all connections.即使您不断地创建和处理来自多个不同线程的连接,一个 HttpClient 也会被创建一次,然后被所有连接重用和共享。

https://github.com/snowflakedb/snowflake-connector-net/issues/275 https://github.com/snowflakedb/snowflake-connector-net/issues/275

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

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