title: singleton database connection
body: <p>I want to retrieve the value from database "details" for that i want to create only one connection with DB, if connection exists don't make a new connection and continue with the previous one. for that i have tried with following:</p>
  
  <pre><code>import MySQLdb
  import re
  import os
  
  class Find:
  
      def __init__(self,addr):
          self.addr = addr
  
  
      dsn = {
      'username': 'xyz',
      'password': 'xyz',
      'hostname': 'localhost',
      'database': 'details'
      }
      the_database_connection = False
  
      def connect(self,dsn):
          '''This function saves the database connection, so if invoke this again, it gives you the same one, rather than making a second connection.'''
          global the_database_connection
          if not the_database_connection:
              try:
                  the_database_connection = MySQLdb.connect(
                      host=dsn['hostname'],
                      user=dsn['username'],
                      passwd=dsn['password'],
                      db=dsn['database'])
              # so modifications take effect automatically
                  the_database_connection.autocommit(True)
              except MySQLdb.Error, e:
                  print ("Couldn't connect to database. MySQL error %d: %s" %(e.args[0], e.args[1]))
          return the_database_connection
          x=conn.cursor()
          sql = "select * from persons where address = %s" % addr
          x.execute(sql)
          rows = x.fetchall()
          for row in rows:
              print row
  
  if __name__ == "__main__":
      a = Find(addr = "new street")
      a.connect()
  </code></pre>
  
  <p>But this showing error: a.connect takes 2 argument one is define...
  how can i define the above dsn.</p>