I created a game server in Python that connects to PostgreSQL db using psycopg2. I saw examples, I saw that when creating a connection to the database, you should close the connection when creating queries, for example, for each client:
con = psycopg2.connect (database = 'testdb', user = 'janbodnar')
cur = con.cursor ()
.
.
.
con.close ()
Well, when I start my server, I have this:
Inside my class
def __init __ (self):
con = psycopg2.connect (database = 'testdb', user = 'janbodnar')
cur = con.cursor ()
def query(self):
cur.execute ("DROP TABLE IF EXISTS Cars")
That is, I use the same connection object for all requests from all clients and never close the connection, it looks better than opening and closing connections for each client, my server seems to work well. do you think about it is it well done? not to do?. Thanks you
source
share