# HG changeset patch # User Daniele Nicolodi # Date 1319652009 -7200 # Node ID 80e43dd34d71fb968f74689ad3f1bb07432376d9 # Parent d2d83ea3f369a3df62618ad3168d346548837ec5 Avoid opening the database connection when serving static resources. diff -r d2d83ea3f369 -r 80e43dd34d71 src/ltpdarepo/__init__.py --- a/src/ltpdarepo/__init__.py Wed Oct 26 19:59:04 2011 +0200 +++ b/src/ltpdarepo/__init__.py Wed Oct 26 20:00:09 2011 +0200 @@ -18,19 +18,23 @@ @app.before_request def before_request(): + # get version information from package + g.version = get_distribution('ltpdarepo').version + + # optimization: do not open db connection for static resources + if request.endpoint == 'static': + return + # open database connection g.db = mysql.connect(host=app.config['HOSTNAME'], db=app.config['DATABASE'], user=app.config['USERNAME'], passwd=app.config['PASSWORD'], charset='utf8') - # get version information from package - g.version = get_distribution('ltpdarepo').version - # validate schema revision curs = g.db.cursor() curs.execute("SELECT CAST(value AS UNSIGNED) FROM options WHERE name='version'") g.schema = curs.fetchone()[0] - if g.schema != SCHEMA and '/static/' not in request.url: + if g.schema != SCHEMA: raise InternalServerError( '

The database needs to be upgraded.

' 'Current database schema version: %s. ' @@ -40,7 +44,9 @@ @app.teardown_request def teardown_request(exception): # close database connection - g.db.close() + db = getattr(g, 'db', None) + if db is not None: + db.close() # register error handlers