Manage DB disconnection
Benjamin Renard

Benjamin Renard commited on 2014-08-17 18:53:53
Showing 1 changed files, with 17 additions and 0 deletions.

... ...
@@ -25,8 +25,17 @@ class DB(object):
25 25
 			except Exception, e:
26 26
 				log.fatal('Error connecting to database : %s' % e)
27 27
 				return
28
+		else:
29
+			try:
30
+				self.con.ping(True)
31
+			except Exception, e:
32
+				self.con  = 0
33
+				return self.connect()
34
+			return True
35
+				
28 36
 
29 37
 	def do_sql(self,sql):
38
+		if self.connect():
30 39
 			try:
31 40
 				c=self.con.cursor()
32 41
 				c.execute(sql)
... ...
@@ -37,12 +46,14 @@ class DB(object):
37 46
 				return False
38 47
 
39 48
 	def select(self,sql):
49
+		if self.connect():
40 50
 			ret=self.do_sql(sql)
41 51
 			if ret!=False:
42 52
 				return ret.fetchall()
43 53
 			return ret
44 54
 
45 55
 	def login(self,email,password):
56
+		if self.connect():
46 57
 			ret=self.select("SELECT email,name,password FROM users WHERE email='%s' AND password='%s'" % (email,password))
47 58
 			log.debug(ret)
48 59
 			if ret:
... ...
@@ -58,6 +69,7 @@ class DB(object):
58 69
 			return { 'loginerror': 'Erreur inconnu' }
59 70
 
60 71
 	def subscribe(self,email,name,password):
72
+		if self.connect():
61 73
 			ret=self.select("SELECT count(*) as count FROM users WHERE email='%s'" % (email))
62 74
 			log.debug(ret)
63 75
 			if ret[0][0]!=0:
... ...
@@ -72,6 +84,7 @@ class DB(object):
72 84
 			return {'subscribeerror': u'Une erreur est survenue durant votre inscription :('}
73 85
 
74 86
 	def sync_group(self,email,groups):
87
+		if self.connect():
75 88
 			db_groups=self.get_group(email)
76 89
 			if db_groups!=False:
77 90
 				db_grouplist=group.GroupList()
... ...
@@ -85,8 +98,11 @@ class DB(object):
85 98
 				else:
86 99
 					return {'syncerror': 'Erreur en modifiant les informations de la base de donnees'}
87 100
 			return {'syncerror': 'Erreur inconnu'}
101
+		else:
102
+			return {'syncerror': u"Erreur de connexion à la base de données"}
88 103
 
89 104
 	def get_group(self,email):
105
+		if self.connect():
90 106
 			ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
91 107
 			if ret!=False:
92 108
 				if len(ret)==1:
... ...
@@ -97,6 +113,7 @@ class DB(object):
97 113
 				return False
98 114
 
99 115
 	def set_group(self,email,groups):
116
+		if self.connect():
100 117
 			ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
101 118
 			if ret!=False:
102 119
 				json_groups=json.dumps(groups)
103 120