Connection Pooling

Many applications will desire some form of connection pooling for improved performance. As of version 1.2, cTDS does provide a simple connection pool implementation: k_ctds.pool.ConnectionPool. It can also be used with 3rd party implementation, such as antipool.

Note

Whatever connection pooling solution is used, it is important to remember that k_ctds.Connection and k_ctds.Cursor objects must not be shared across threads.

k_ctds.pool Example

import k_ctds
import k_ctds.pool
import pprint

config = {
    'server': 'my-host',
    'database': 'MyDefaultDatabase',
    'user': 'my-username',
    'password': 'my-password',
    'appname': 'ctds-doc-pooling-example',
    'timeout': 5,
    'login_timeout': 5,
    'autocommit': True
}

pool = k_ctds.pool.ConnectionPool(
    ctds,
    config
)

with pool.connection() as connection:
    with connection.cursor() as cursor:
        try:
            cursor.execute('SELECT @@VERSION;')
            rows = cursor.fetchall()
            print([c.name for c in cursor.description])
            pprint.pprint([tuple(row) for row in rows])
        except k_ctds.Error as ex:
            print(ex)

# Explicitly cleanup the connection pool.
pool.finalize()