Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Cassandra inside a Pylons app?

I created a new Pylons project, and would like to use Cassandra as my database server. I plan on using Pycassa to be able to use cassandra 0.7beta. Unfortunately, I don't know where to instantiate the connection to make it available in my application.

The goal would be to :

  • Create a pool when the application is launched
  • Get a connection from the pool for each request, and make it available to my controllers and libraries (in the context of the request). The best would be to get a connexion from the pool "lazily", i.e. only if needed
  • If a connexion has been used, release it when the request has been processed

Additionally, is there something important I should know about it ? When I see some comments like "Be careful when using a QueuePool with use_threadlocal=True, especially with retries enabled. Synchronization may be required to prevent the connection from changing while another thread is using it.", what does it mean exactly ?

Thanks.

-- Pierre

like image 921
Pierre Avatar asked Sep 08 '10 20:09

Pierre


1 Answers

Well. I worked a little more. In fact, using a connection manager was probably not a good idea as this should be the template context. Additionally, opening a connection for each thread is not really a big deal. Opening a connection per request would be.

I ended up with just pycassa.connect_thread_local() in app_globals, and there I go.

like image 126
Pierre Avatar answered Oct 28 '22 13:10

Pierre