Thursday, May 5, 2011

TG2.1 Trouble with Sqlite Database

If you use sqlite as the backend db for your tg2.1 web apps, you might occasionally get errors like the following:
ProgrammingError: (ProgrammingError) Cannot operate on a closed database

It turns out that the default sqlalchemy dbpool is not optimized for filebased db, like sqlite.

To solve this matter, simply change sqlalchemy dbpool to NullPool.

Make the modification in webapp/config/app_cfg.py:
#################################
from sqlalchemy.pool import NullPool
class PLAppConfig(AppConfig):
# override
def setup_sqlalchemy(self):
from sqlalchemy import engine_from_config
engine = engine_from_config(pylons_config, 'sqlalchemy.', poolclass=NullPool)
config['pylons.app_globals'].sa_engine = engine
# Pass the engine to initmodel, to be able to introspect tables
self.package.model.init_model(engine)

base_config = PLAppConfig()
#################################

All set, no more ProgrammingError.

No comments:

Post a Comment