Skip to content

Commit

Permalink
Update database.py
Browse files Browse the repository at this point in the history
- Fix the issue so many rollbacks
  • Loading branch information
BattlefieldDuck committed Nov 30, 2023
1 parent 6bc5b60 commit b390526
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions discordgsm/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pymongo import DeleteOne, MongoClient, UpdateMany, UpdateOne
import psycopg2
import psycopg2.pool
from psycopg2.extensions import connection
from dotenv import load_dotenv


Expand Down Expand Up @@ -147,7 +148,13 @@ def dispose(self):
def cursor(self):
if self.driver == Driver.PostgreSQL:
try:
conn = self.pool.getconn()
conn: connection = self.pool.getconn()

# Fix the issue so many ROLLBACKs
# The connection pool issues connection.rollback() when a connection is returned.
# https://docs.sqlalchemy.org/en/14/faq/connections.html#why-does-sqlalchemy-issue-so-many-rollbacks
conn.autocommit = True

cursor = conn.cursor()
except psycopg2.InterfaceError: # connection already closed
# Reconnect
Expand All @@ -162,14 +169,14 @@ def cursor(self):
return conn, cursor

def close(self, conn: sqlite3.Connection, cursor: sqlite3.Cursor, *, commit=False):
if commit:
conn.commit()

cursor.close()

if self.driver == Driver.PostgreSQL:
self.pool.putconn(conn)
else:
if commit:
conn.commit()

conn.close()

def transform(self, sql: str):
Expand Down

0 comments on commit b390526

Please sign in to comment.