tsingh2k15_176037 avatar image
tsingh2k15_176037 asked Erick Ramirez edited

Cassandra connection management using python-driver


I am using datastax python-driver to connect to Cassandra. I have following questions/clarifications:

  1. I am following the examples code( but was curious to check if I need to programmatically close the connection after every query "session.execute" ? or underlying connection pool will close it once a query is executed?
  2. Any recommendations around connection handling while using python-driver?
  3. I am using a long running process which uses one global cassandra session obtained using "session = cluster.connect(...)" snippet and uses this session to execute all the queries while this process is running and processes continuous stream of records. Can this approach cause any issues on cassandra cluster in terms of long running connections? I wanted to reuse same connection as having a new one for each record may be costly? Please advise.
  4. I want to implement close connection logic in my program itself, but wanted to learn more about if this is handled by python-driver already? If pyhon-driver does it underneath, then I may not need it.


python driver
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

jorgebg avatar image
jorgebg answered tsingh2k15_176037 commented

The DataStax drivers maintain a pool of connections to each of the DataStax nodes selected by the load balancing policy. By default, the driver instance creates one connection to each of the local datacenter hosts in the default load balancing policy.

You should reuse your cluster and session instance across your application:

and it should be a single instance for all the application lifetime.

1 comment Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

tsingh2k15_176037 avatar image tsingh2k15_176037 commented ·

This helps, thank you for clarification.

0 Likes 0 ·