ResourceClosedError on Postgres Database - support needed

449 views
Skip to first unread message

Mirek Morek

unread,
Jan 13, 2020, 9:35:08 AM1/13/20
to sqlalchemy
Hello,
I use sqlalchemy to execute my tests on Postgres Database. I run like ~200 tests, every test uses its own setup and teardown. creates a connection and closes it after all.

I sometimes get ResourceClosedError ("StatementError: (sqlalchemy.exc.ResourceClosedError) This Connection is closed" in random places when tests are running one after another. It was not present on Sybase database. It seems like it wants to work on a closed connection...?
For a single test run I never got that.

Do you have any idea, what I could improve? This is a part of my code:


from sqlalchemy import create_engine
from sqlalchemy.orm import create_session
from configuration import PostgresConfiguration as PGS

class
PostgresDatabase:
def __init__(self):
self.connection_string = r'postgresql://{}:{}@{}:{}/{}'.format(
PGS.DB_USER,
PGS.DB_PASSWORD,
PGS.DB_HOST.split(":")[0],
PGS.DB_HOST.split(":")[1],
PGS.DB_NAME)
self.engine = create_engine(self.connection_string, echo=True, label_length=30)
self.session = create_session(self.engine)
self.database_name = PGS.DB_NAME

def get_session(self):
"""
Retrieves current session.
:return: current session.
"""
return self.session

def close_session(self):
"""
Closes all open sessions.
"""
self.session.close_all()

def execute_query(self, query, commit=True):
"""
Executes query passes as a parameter.
:param query: passed query to be executed.
:param commit: if True session will be committed
:return query execution results
"""
if commit:
self.session.begin()
results = self.session.execute(query)
self.session.commit()
else:
results = self.session.execute(query)
return results

Mike Bayer

unread,
Jan 15, 2020, 7:24:52 PM1/15/20
to noreply-spamdigest via sqlalchemy
there's no way to tell from here, information like the stack trace, and how the tests are being run (like what test runner, any multiprocessing in use, etc.) would be places to start.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Mirek Morek

unread,
Jan 16, 2020, 9:42:32 AM1/16/20
to sqlalchemy
Ok, I see that. Could you give me some hint how could I provide the useful stack trace? What tool should I use?
To unsubscribe from this group and stop receiving emails from it, send an email to sqlal...@googlegroups.com.

Mike Bayer

unread,
Jan 16, 2020, 10:00:25 AM1/16/20
to noreply-spamdigest via sqlalchemy
whatever tool you are using is telling you there is a ResourceClosedError.  That error message should be accompanied by a stack trace.    if you're running unit tests in Python, as opposed to  you're on some remote system and looking in log files, these should be very obvious as every unit test runner is going to be dumping out stack traces.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages