Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

tsingh2k15_176037 avatar image
tsingh2k15_176037 asked ·

Cassandra connection management using python-driver

Hi,

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

  1. I am following the examples code(https://github.com/datastax/python-driver/tree/master/examples) 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.

Thanks

python driver
10 |1000 characters needed characters left characters exceeded

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 ·

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.

https://docs.datastax.com/en/devapp/doc/devapp/driversConnectionPooling.html

You should reuse your cluster and session instance across your application: https://docs.datastax.com/en/devapp/doc/devapp/driversBestPractices.html

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

1 comment Share
10 |1000 characters needed characters left characters exceeded

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

This helps, thank you for clarification.

0 Likes 0 · ·