From 597df69275d7eccf05a73258fc906071dc7a62d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Sat, 5 Sep 2015 17:09:01 +0200 Subject: [PATCH] Save mysqldb-connection as class atribute to make it usable for all decandant classes of MYSQL_RED --- mysql_red.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/mysql_red.py b/mysql_red.py index acc6161..35af34c 100644 --- a/mysql_red.py +++ b/mysql_red.py @@ -6,6 +6,9 @@ except ImportError: class MYSQL_RED: + #Save mysqldb-connection as class attribute to use only one in descendant classes + connection = False + def __init__( self, db_hostname, db_username, db_password, db_name ): """ Opens a connection to MySQL-DB @@ -13,11 +16,14 @@ class MYSQL_RED: @returns mysql-stream MySQL Connection """ - self.__connection = mysqldb.connect( host=db_hostname, user=db_username, passwd=db_password, db=db_name ) + # Connect to mysqldb only once + if( type( self ).connection == False ): + + type( self ).connection = mysqldb.connect( host=db_hostname, user=db_username, passwd=db_password, db=db_name ) - def close( self ): + def __del__( self ): """ - Before deleting instance, close connection to MySQL-DB + Before deleting class, close connection to MySQL-DB """ - self.__connection.close() + type( self ).connection.close()