title: Sharing single POSTGRESQL connection between several method of same class
body: <p>I have this brick-wall, I am trying to overcome. However, I am taking this route (single connection sharing ) because the previous approach (multiple connection) failed me when I had to access a particular table with different connection object from another method. So basically, with singleton connection approach, see sample code below;</p>
  
  <pre><code>import sys, psycopg2
  
  
  class myclass():
     def __inti__(self):
        pass
  
  # Declaring DB connection
  
  def dbconn(self):
      try:
          connect_string = psycopg2.connect(host='localhost', database='',    user='',password='')
          self.con = connect_string.cursor()
      except:
          print('Connection Error')
  
      # return dbconn
      print(self.con, 'this is at dbconn method level')
      return self.con
  
  
  def registration(self):
      """ Detail of our Contacts is being collected"""
  
      print('Enter 1 or 2 to update Network Provider)
      update_data = int(input('Enter your 1 or 2 :'))
      if update_data == 1:
          network_name = input('Enter name of the Network Provider : ')
          # Calling db_conn method in order to established connection to DB SERVER
          try:
              local_con = myclass().dbconn()
              print(local_con, 'this is at registration method level')
  
          except:
              if local_con:
                  local_con.rollback()
  
              print('Error  Connection')
  
          # Sending the data to the database for permanent storage
  
          local_con.execute(
              "INSERT INTO network(network_name)VALUES(%s)", (network_name,))
          local_con.commit()
          print('Data Save properly')
  </code></pre>
  
  <p><strong>The above code resulted into this output from my console,</strong></p>
  
  <pre><code>You can update Network Provider, API Setting and Price here
  Enter 1 or 2 to update Network Provider
  Enter your 1 or 2 :1
  Enter name of the Network Provider : starcom Nigeria
  &lt;cursor object at 0x0000000002FD7EA0; closed: 0&gt; this is at dbconn method level
  &lt;cursor object at 0x0000000002FD7EA0; closed: 0&gt; this is at registration method level
  Traceback (most recent call last):
  File "/myswitch.py", line 258, in &lt;module&gt;
  new_provider.registration()
  File "/myswitch.py", line 53, in registration
  local_con.commit()
  AttributeError: 'psycopg2.extensions.cursor' object has no attribute   'commit'
  </code></pre>
  
  <p>from the above output, I understand that i can access (self.con) from dbconn() method in another method registration() of same class, however, the commit() attribute which i expected on the local variable under registration() failed with following code
       AttributeError: 'psycopg2.extensions.cursor' object has no attribute   'commit'</p>
  
  <p>Hence, transaction failed to save to the DB.
  Hope I can lead on how to resolve this issue.
  Thanks in advance</p>