How to connect to MS SQL after hibernation (S4) (or dropped connection)?

I run some sleep tests using python + microsoft pwrtest utility

I also use sqlalchemy (orm) to work with the database (ms sql server 2008 r2).

I am connected to a remote sql server and everything works fine, but after the computer enters sleep mode (S4), the sql server disconnects the connection (I consider it as an “Activity Monitor” in the management studio).

When my computer returns to sleep mode and continues to work with the script, I get the error message " DBAPIError: (Error) ('08S01', '[08S01] [Microsoft] [ODBC SQL Server Driver] failed (0) (SQLExecDirectW)' ) "

I tried using pool_recycle

 engine = create_engine(settings.sql_engine, echo=True, pool_recycle=1) 

However, as I understand it, sqlalchemy does not understand that the connection no longer exists.

I also tried using engine.dispose() and according to the documentation it should remove the current pool:

Dispose of the connection pool used by this engine.

A new connection pool is created immediately after the old one has been deleted. This new pool, like all SQLAlchemy connection pools, does not make any real database connections until first requested.

But that also didn't work.

How to connect to the database?

Thanks!

The code:

 #module db.py: from sqlalchemy.ext.declarative import declarative_base , declared_attr from sqlalchemy import * from sqlalchemy.orm import sessionmaker, relationship, backref from sqlalchemy.orm.exc import * Base = declarative_base() class Drive(Base): __tablename__ = "drives" id = Column(Integer, primary_key=True) isPhysical = Column(Boolean) devicePath = Column(String(100)) name = Column(String(10)) encrypted = Column(Boolean, default=False) size = Column(BigInteger) def __init__(self): pass sql_engine = 'mssql+pyodbc://Tester: Password@sql-server /Automation' engine = create_engine(sql_engine, echo=True) Session = sessionmaker(bind=engine) Base.metadata.create_all(engine) 

actual call:

 #hibernation.py from db import * import subprocess command = r"pwrtest /sleep /s:4 /h:n /c:1" out = subprocess.check_output(command) # hibernation occurs session = Session() session.query(Drive).all() 
+6
source share
1 answer
+3
source

Source: https://habr.com/ru/post/922961/


All Articles