Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix amount of connecitons #570

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions prediction_market_agent_tooling/tools/db/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ def __new__(cls, api_keys: APIKeys | None = None) -> "DBManager":
return cls._instances[url_hash]

def __init__(self, api_keys: APIKeys | None = None) -> None:
if hasattr(self, "_initialized"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me why self._initialized = True is needed.
I expected DBManager to be a singleton, if that's the case, then this shouldn't be necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, my words exactly!

I asked o1-preview to fix whatever is wrong with DBManager, and these changes are what it did. And now it seems it works 🤷

Screenshot by Dropbox Capture

return
sqlalchemy_db_url = (api_keys or APIKeys()).sqlalchemy_db_url
self._engine = create_engine(
sqlalchemy_db_url.get_secret_value(),
json_serializer=json_serializer,
json_deserializer=json_deserializer,
pool_size=10,
pool_recycle=3600,
echo=True,
pool_size=2,
)
self.cache_table_initialized: dict[str, bool] = {}
self._initialized = True

@contextmanager
def get_session(self) -> Generator[Session, None, None]:
Expand All @@ -52,6 +53,7 @@ def get_connection(self) -> Generator[Connection, None, None]:
def create_tables(
self, sqlmodel_tables: Sequence[type[SQLModel]] | None = None
) -> None:
# Determine tables to create
if sqlmodel_tables is not None:
tables_to_create = []
for sqlmodel_table in sqlmodel_tables:
Expand All @@ -67,9 +69,10 @@ def create_tables(
tables_to_create = None

# Create tables in the database
with self.get_connection() as connection:
SQLModel.metadata.create_all(connection, tables=tables_to_create)
connection.commit()
if tables_to_create is None or len(tables_to_create) > 0:
with self.get_connection() as connection:
SQLModel.metadata.create_all(connection, tables=tables_to_create)
connection.commit()

# Update cache to mark tables as initialized
if tables_to_create:
Expand Down
Loading