Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLALchemy DB Session with Flask, Postgres

I'm using SQLAlchemy with Flask as shown here: http://flask.pocoo.org/docs/patterns/sqlalchemy/

I have a Selenium test suite that first runs with Firefox and then with Chrome.

Before the start of tests on each browsers, tables in the test database (PostgreSQL) are dropped and created.

It runs perfectly for the first browsers, but for the second browser the SQL create / drop attempt just freezes and no errors are shown.

I believe this is because of open SQLAlchemy sessions, is that correct?

like image 712
Sri Avatar asked Dec 20 '12 13:12

Sri


People also ask

Why SQLAlchemy to connect PostgreSQL to a flask application?

Why SQLAlchemy to Connect PostgreSQL to a Flask Application? SQLAlchemy is an ORM-Objects Relational Mapper written in Python. It gives away around to interact with the Databases without using SQL statements. It provides an extra layer on top of SQL which allows us to use Databases and Tables just like Python Class Objects.

What is PostgreSQL?

What is PostgreSQL? According to its Wikipedia page, "PostgreSQL, also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance" - Wikipedia

What does SQLAlchemy_database_Uri expect?

The SQLALCHEMY_DATABASE_URI expects a very particularly formatted string, which you can see below. We tell it what database and communication-library to use, the username, password, url and even database to connect to.

How do I install PostgreSQL in Linux?

To install PostgreSQL, visit the link here. Once PostgreSQL is installed, open the SQL shell and set up your DB connection Username and password. Run the below command to open shell in Terminal:


1 Answers

I believe this is because of open SQLAlchemy sessions, is that correct?

That's most likely the case. To confirm it, connect to the postgres database and run SELECT * FROM pg_stat_activity;

I'm not sure how you handle the DB creation/dropping but you may want to call dispose() and possibly recreate() on the SQLAlchemy connection pool, after making sure that any checked out connection has been returned (for example, with session.close()).

like image 168
jd. Avatar answered Sep 28 '22 20:09

jd.