sqlalchemy mysql server has gone away
I've read through a number of similar problems, but none seem to fix my issue. I'm running a pylons app and using SQLAlchemy to connect to 2 databases. The primary, configured through the config file works perfectly without issue. I'm trying to connect to a second database by initializing a class defined in model like so:
class DB2(object):
def __init__(self):
开发者_如何学JAVA self.engine = sa.create_engine('mysql://someaddress:3306/database', echo=False, pool_recycle=1800)
self.meta = sa.MetaData(self.engine)
<define tables>
<define mappings>
Session = orm.sessionmaker(bind=self.engine, autoflush=False, autocommit=False)
self.session = Session()
I thought that defining the pool_recycle there (with a time that matches my working database) would be enough to prevent this, but it's not. Can anyone recommend a solution? Thanks.
I've had same problem recently, and solved it by ensuring that it always closes transaction, even after SELECTs. Just add DB2.session.commit() to each place you do something with it, and it should start cycling connections.
精彩评论