Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>See EDIT at the bottom for tested solution</strong></p> <p>I didn't try it, but maybe using <strong><a href="http://www.sqlalchemy.org/docs/reference/sqlalchemy/interfaces.html#sqlalchemy.interfaces.PoolListener" rel="noreferrer">PoolListener</a></strong> is a way to go?</p> <p>You could do something like this:</p> <pre><code>class MyListener(sqlalchemy.interfaces.PoolListener): def __init__(self): self.retried = False def checkout(self, dbapi_con, con_record, con_proxy): try: dbapi_con.info() # is there any better way to simply check if connection to mysql is alive? except sqlalchemy.exc.OperationalError: if self.retried: self.retried = False raise # we do nothing self.retried = True raise sqlalchemy.exc.DisconnectionError # next, code according to documentation linked above follows e = create_engine("url://", listeners=[MyListener()]) </code></pre> <p>This way every time connection is about to be checked out from the pool we test if it's actually connected to the server. If not, we give sqlalchemy one chance to reconnect. After that, if problem is still there, we let it go.</p> <p>PS: I didn't test if this works.</p> <p>Edit: As for the Pylons, modifications to the engine initialization showed above would need to be done in <strike>your_app.model.init_model (Pylons 0.9.7) or </strike> your_app.config.environment.load_environment <strike>(Pylons 1.0)</strike> function - <strike>these are</strike> this is the <strike>places</strike> place where engine instance gets created.</p> <p><strong>EDIT</strong></p> <p><strong>Ok. I was able to reproduce described situation. The code above needs some changes in order to work. Below is how it should be done. Also it doesn't matter whether it's 0.9.7 or 1.0.</strong></p> <p>You need to edit your_app/config/environment.py. Put these exports at top of the file:</p> <pre><code>import sqlalchemy import sqlalchemy.interfaces import _mysql_exceptions </code></pre> <p>And the end of load_environment function should look like that:</p> <pre><code>class MyListener(sqlalchemy.interfaces.PoolListener): def __init__(self): self.retried = False def checkout(self, dbapi_con, con_record, con_proxy): try: dbapi_con.cursor().execute('select now()') except _mysql_exceptions.OperationalError: if self.retried: self.retried = False raise self.retried = True raise sqlalchemy.exc.DisconnectionError config['sqlalchemy.listeners'] = [MyListener()] engine = engine_from_config(config, 'sqlalchemy.') init_model(engine) </code></pre> <p>This time I was able to test it (on Pylons 1.0 + SQLAlchemy 0.6.1) and <strong>it works.</strong> :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload