Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce number of connections using SQLAlchemy + postgreSQL?

I'm developing on heroku using their Postgres add-on with the Dev plan, which has a connection limit of 20. I'm new to python and this may be trivial, but I find it difficult to abstract the database connection without causing OperationalError: (OperationalError) FATAL: too many connections for role.

Currently I have databeam.py:

import os
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from settings import databaseSettings

class Db(object):
    def __init__(self):
        self.app = Flask(__name__)
        self.app.config.from_object(__name__)
        self.app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', databaseSettings())
        self.db = SQLAlchemy(self.app)

db = Db()

And when I'm creating a controller for a page, I do this:

import databeam

db = databeam.db
locations = databeam.locations

templateVars = db.db.session.query(locations).filter(locations.parent == 0).order_by(locations.order.asc()).all()

This does produce what I want, but slowly and at times causes the error metioned above. Since I come from a php background I have a certain mindset of how to deal with DB connections (I.e. like the example above), but I fear it doesn't fit well with python.

What is the proper way of abstracting the db connection in one place and then just using the same connection in all imports?

like image 236
Morgan Wilde Avatar asked Mar 19 '13 10:03

Morgan Wilde


People also ask

Does SQLAlchemy close connection automatically?

close() method is automatically invoked at the end of the block. The Connection , is a proxy object for an actual DBAPI connection.

Does SQLAlchemy use connection pool?

SQLAlchemy includes several connection pool implementations which integrate with the Engine . They can also be used directly for applications that want to add pooling to an otherwise plain DBAPI approach.

How many connections PostgreSQL can handle?

PostgreSQL Connection Limits 15 connections are reserved for the superuser to maintain the state and integrity of your database, and 100 connections are available for you and your applications. If the number of connections to the database exceeds the 100-connection limit, new connections fail and return an error.

What is pool size in SQLAlchemy?

pool_size – The size of the pool to be maintained, defaults to 5. This is the largest number of connections that will be kept persistently in the pool. Note that the pool begins with no connections; once this number of connections is requested, that number of connections will remain.


1 Answers

Within SQL Alchemy you should be able to create a connection pool. This pool is what the pool size would be for each Dyno. On the Dev and Basic plan since you could have up to 20, you could set this at 20 if you run 1 dyno, 10 if you run 2, etc. To configure your pool you can setup the engine:

engine = create_engine('postgresql://me@localhost/mydb',
                   pool_size=20, max_overflow=0)

This sets up your db engine with a pool which you pull from automatically then. You can also configure the pool manually, more details on that can be found on the pooling guide of SQL Alchemy - http://docs.sqlalchemy.org/en/latest/core/pooling.html

like image 170
CraigKerstiens Avatar answered Oct 09 '22 01:10

CraigKerstiens