Skip to content

Instantly share code, notes, and snippets.

@luhn
Created July 18, 2014 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luhn/98d18dca00edae2af93a to your computer and use it in GitHub Desktop.
Save luhn/98d18dca00edae2af93a to your computer and use it in GitHub Desktop.
txpostgres connection function (With automatic reconnect)
from psycopg2.extras import DictCursor
from txpostgres import txpostgres
from txpostgres.reconnection import DeadConnectionDetector
def db_pool_from_config(config):
"""
Create a connection pool. Returns a deferred that will fire when the
connection pool is ready.
"""
def connection_factory(self, *args, **kwargs):
kwargs['detector'] = DeadConnectionDetector()
return txpostgres.Connection(*args, **kwargs)
txpostgres.ConnectionPool.connectionFactory = connection_factory
return txpostgres.ConnectionPool(
None,
database=config.get('database'),
user=config.get('user'),
password=config.get('password'),
host=config.get('host'),
cursor_factory=DictCursor,
min=config.get('num_conn', 5),
).start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment