title: For MariaDB, is it necessary to create and close a connection on every function I use?
body: <p>im have some doubts about good practicing about MariaDB connections uses, i have created a library with some functions, so i create connection/Create cursor/.../close cursor/close connection each Function as it follow:</p>
  
  <pre><code>def Func1():
      cnx=mariadb.connect(host,user,pass,db)
      cursor=cnx.cursor()
      do...Query and something else
      cnx.commit()
      cursor.close()
      cnx.close()
  
  def Func 2():
      cnx=mariadb.connect(host,user,pass,db)
      cursor=cnx.cursor()
      do...Query and something else
      cnx.commit()
      cursor.close()
      cnx.close()
  ...
  def FuncN():
      cnx=mariadb
      ....
      cnx.close()
  
  Main:
      Func1()
      Func2()
  ... FuncN()
  </code></pre>
  
  <p>and i would like to know if i can save some lines  doing something like this:</p>
  
  <pre><code>cnx=mariadb.connect(host,user,pass,db)
  cursor=cnx.cursor()
  
  def Func1():
      do...Query and something else
  def Func2():
      do...Query and something else
  ....
  Main:
      Func1()
      Func2()
      ....FuncN()
      cnx.commit()
      cursor.close()
      cnx.close()
  </code></pre>
  
  <p>i just want to figure out how i can do my code shorter, im using python 2 by now btw...Thanks</p>