Tuesday, November 22, 2011

Connection caching and sharing

Hello,

I'm sharing and caching a database connection in my Python App Engine for each instance by holding a reference to the connection at module level. Something like this:

_conn = None

def get_connection():
  global _conn
  
  if _conn is None:
    _conn = rdbms.connect(...)  
  return _conn

This seems to work OK and so far I haven't seen any problems with the connection timing etc.

Does anyone know if this is a good idea? Could I run into threading problems etc. At the moment each instance only served one request at a time but our app will be switching to multi-threaded Python2.7 soon. 

Is there any way of detecting a connection has become disconnected or timed-out and re-connect?

Regards
Richard





No comments:

Post a Comment