Search code examples
c#orientdb

OrientDB Connection Pool in C#


I'm using OrientDB v2.2.16 and the C# driver v0.1.12.0. I cannot find any documentation on how to create a connection pool with the new iteration of this driver. The ODatabase constructor has a connectionPool parameter:

ODatabase db=new ODatabase("localhost", 2424, "testDb", ODatabaseType.Graph, "USER", "PASSWORD", "Pool");

But there's no way to create "Pool". The old version of the driver contains a CreateDatabasePool function in the OClient class:

OClient.CreateDatabasePool("localhost", 2424, "testDb", ODatabaseType.Graph, "USER", "PASSWORD", 10, "Pool");

The new one does not. Does the pool spontaneously create itself upon the first call to the ODatabase constructor? If so, how do I control parameters such as pool size?


Solution

  • Does the pool spontaneously create itself upon the first call to the ODatabase constructor?

    Looking at the source, the constructor does, in fact, create a connection pool automatically:

    public ODatabase(string hostName, int port, string databaseName, ODatabaseType type, string userName, string userPassword)
    {
        _connectionPool = new ConnectionPool(hostName, port, databaseName, type, userName, userPassword);
        ClientCache = new ConcurrentDictionary<ORID, ODocument>();
    }
    

    If so, how do I control parameters such as pool size?

    Looking at the code for the ConnectionPool object, there isn't any direct way to control the number of pooled connections explicitly:

    public Connection GetConnection()
    {
        if (_connectionPool.ContainsKey(_poolAlias))
            return _connectionPool[_poolAlias];
    
        var connection = new Connection(_hostName, _port, _databaseName, _type, _userName, _userPassword, _sslCerts);
        _connectionPool.AddOrUpdate(_poolAlias, i => connection,
            (i, conn) => _connectionPool[i] = conn);
    
        return connection;
    }
    

    As you can see, the connection is maintained for the duration of the lifetime of your ODatabase object instance. So the only way you can control the pool size is to control the lifetime of your ODatabase object, as well as the values you use for "poolAlias."

    Note that a pool is always created when you construct this object, even if you don't specify a poolAlias. In that case, the alias is set to "Default."