title: Psycopg2 connection and cursor in a class are closed? And should I close them?
body: <p>I just have some question how to understand cursors and connections inside of a class.
  Everything is working fine but I dont understand how python handle the connections and the cursors...</p>
  <p>I have a class to interact with my database. In the insert methode I also put a &quot;check_connection&quot; method that simply print the cursor and connection objects.</p>
  <p>However I close them after using the insert methode. When I call the &quot;check_connection&quot; object after that my class object gives back objects at the same place. When I call the insert methode again new objects are created, i guess?</p>
  <p>Insert method:</p>
  <pre><code>def insert_new_join(self, user, invitedby):
          try:
              timest = dt.now().strftime('%Y-%m-%d %H:%M:%S')
              query = &quot;&quot;&quot; INSERT INTO user_invites (username, invitedby, dates) VALUES (%s,%s,%s) ON CONFLICT DO NOTHING&quot;&quot;&quot;
              values = (user, invitedby, timest,)
  
              self.connect()
              self.cursor.execute(query,values)
              self.con.commit()
              self.check_connection()
              return True
  
          except (Exception, psy.Error) as e :
              with open(&quot;errorlog.txt&quot;,'a') as f:
                  f.write(&quot;db insert_new_join: &quot;+dt.now().strftime('%Y-%m-%d %H:%M:%S')+&quot; &quot;+str(e)+&quot;\n&quot;)
              return False
          finally:
             self.disconnect()
  </code></pre>
  <p>Disconnect method:</p>
  <pre><code>def disconnect(self):
          if(self.cursor):
              self.cursor.close()
              print(&quot;PostgreSQL cursor is closed&quot;)
          if(self.con):
              self.con.close()
              print(&quot;PostgreSQL connection is closed&quot;)
  </code></pre>
  <p>Mytest file:</p>
  <pre><code>testdb = dbs.user_db(*getc.get_database_login())
  testdb.insert_new_join(&quot;test&quot;,&quot;test&quot;)
  testdb.check_connection()
  print(&quot;\nnew insert - methode call\n&quot;)
  testdb.insert_new_join(&quot;test&quot;,&quot;test&quot;)
  testdb.check_connection()
  </code></pre>
  <p>Thats my output (the 'xxx' are my database connection and normaly filled):</p>
  <pre><code>&lt;connection object at 0x000001B442496D00; dsn: 'xxx', closed: 0&gt;
  &lt;cursor object at 0x000001B442223900; closed: 0&gt;
  PostgreSQL cursor is closed
  PostgreSQL connection is closed
  &lt;connection object at 0x000001B442496D00; dsn: 'xxx', closed: 1&gt;
  &lt;cursor object at 0x000001B442223900; closed: -1&gt;
  
  new insert - methode call
  
  &lt;connection object at 0x000001B44253F040; dsn: 'xxx', closed: 0&gt;
  &lt;cursor object at 0x000001B442223740; closed: 0&gt;
  PostgreSQL cursor is closed
  PostgreSQL connection is closed
  &lt;connection object at 0x000001B44253F040; dsn: 'xxx', closed: 1&gt;
  &lt;cursor object at 0x000001B442223740; closed: -1&gt;
  </code></pre>
  <p>*self.connect() starts the connection with: psycopg2.connect() and check_connection() just print the connection and cursor object, the results you see in the output.</p>
  <p>So my questions:</p>
  <ol>
  <li>Why this calls make new objects? - or did they, I mean there new memmory addresses</li>
  <li>Is that the right way to close the connection and cursors?</li>
  <li>This calls will be in await function is there a possibility to let the connections open and check if they still work? - would do it like the closing with an if and an exception if the connection still open but doesnt work to create a new one, or is there the possibilty to not only check if its still open also it works ... Or Im complitly wrong and that mean if its open it works?</li>
  <li>Why there are diffrent return values of close for the cursor and the connection?</li>
  <li>Is there a new connection/cursor object created or why is it a new memory address? Is that right way to do (question 3)?</li>
  </ol>
  <p>Thank you guys... mostly I just want to understand, because it works but I try to learn to write clean code :D</p>