dwelch
dwelch

Reputation: 3046

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):
    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.

Upvotes: 1

Views: 1907

Answers (1)

Daniel Kluev
Daniel Kluev

Reputation: 11315

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.

Upvotes: 2

Related Questions