Is snowflake net connector thread safe?

Viewed 657

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?

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:

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));
    }
  }
}
1 Answers

Summary from GitHub: The connector is thread safe.

As long as you do not use any session variables in your snowflake code, you can have multiple threads sharing the same 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.

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

Related