Как можно научить SQLAlchemy восстанавливаться после отключения?Python

Программы на Python
Гость
Как можно научить SQLAlchemy восстанавливаться после отключения?

Сообщение Гость »


Согласно http://docs.sqlalchemy.org/en/rel_0_9/c ... essimistic, SQLAlchemy можно использовать для повторного подключения, если запись в пуле соединений больше не действительна. Чтобы проверить это, я создаю следующий тестовый пример:

Код: Выделить всё

import subprocess
from sqlalchemy import create_engine, event
from sqlalchemy import exc
from sqlalchemy.pool import Pool

@event.listens_for(Pool, "checkout")
def ping_connection(dbapi_connection, connection_record, connection_proxy):
cursor = dbapi_connection.cursor()
try:
print "pinging server"
cursor.execute("SELECT 1")
except:
print "raising disconnect error"
raise exc.DisconnectionError()
cursor.close()

engine = create_engine('postgresql://postgres@localhost/test')

connection = engine.connect()

subprocess.check_call(['psql', str(engine.url), '-c',
"select pg_terminate_backend(pid) from pg_stat_activity " +
"where pid  pg_backend_pid() " +
"and datname='%s';" % engine.url.database],
stdout=subprocess.PIPE)

result = connection.execute("select 'OK'")
for row in result:
print "Success!", " ".join(row)
But instead of recovering I receive this exception:

Код: Выделить всё

sqlalchemy.exc.OperationalError: (OperationalError) terminating connection due to administrator command
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Since "pinging server" is printed on the terminal it seems safe to conclude that the event listener is attached. How can SQLAlchemy be taught to recover from a disconnect?


Источник: https://stackoverflow.com/questions/282 ... disconnect

Вернуться в «Python»