title: Flask-sqlalchemy losing connection after restarting of DB server
body: <p>I use flask-sqlalchemy in my application. DB is postgresql 9.3.
  I have simple init of db, model and view:</p>
  
  <pre><code>from config import *
  from flask import Flask, request, render_template
  from flask.ext.sqlalchemy import SQLAlchemy
  app = Flask(__name__)
  app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://%s:%s@%s/%s' % (DB_USER, DB_PASSWORD, HOST, DB_NAME)
  db = SQLAlchemy(app)
  
  class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      login = db.Column(db.String(255), unique=True, index=True, nullable=False)
  
  db.create_all()
  db.session.commit()
  
  @app.route('/users/')
  def users():
      users = User.query.all()
      return '1'
  </code></pre>
  
  <p>And all works fine. But when happens DB server restarting (<code>sudo service postgresql restart</code>), on first request to the <code>/users/</code> I obtain <code>sqlalchemy.exc.OperationalError</code>:</p>
  
  <pre><code>OperationalError: (psycopg2.OperationalError) terminating connection due to administrator command
  SSL connection has been closed unexpectedly
   [SQL: ....
  </code></pre>
  
  <p>Is there any way to renew connection inside view, or setup flask-sqlalchemy in another way for renew connection automatically?</p>
  
  <p>UPDATE.</p>
  
  <p>I ended up with using clear SQLAlchemy, declaring engine, metadata and db_session for every view, where I critically need it.</p>
  
  <p>It is not solution of question, just a 'hack'.</p>
  
  <p>So question is open. I am sure, It will be nice to find solution for this :)</p>