--- a/controllers/mysql.js
+++ b/controllers/mysql.js
@@ -3,16 +3,35 @@
 var mysql =  require('mysql');
 var db =     require ('../config/db');
 var token =  require ('../backend/token/tokenGenerator');
-var con =    require ('../backend/server');
+var dbpool =    require ('../backend/server');
 var email =  require('./emailer');
 var logger = require('../config/logger.js'); 
 
+
+function handlerDB (query, cb){
+	dbpool.getConnection(function(err, conn){
+		if(err){
+			logger.error("handlerDB",err);
+			cb(true, err);
+		}
+		logger.info("handlerDB","connected as id ", conn.threadId);
+		conn.query(query,function(err, rows){
+			conn.release();
+			if(!err){
+				cb(false, rows)
+			}
+		})
+		conn.on('error', function(err){
+			logger.error("handlerDB","error on database connection");
+		})
+	})
+}
 /*
 Insert vApp
 */
 module.exports.insertVapp= function(body, callback){
 	var sqlQuery = "SELECT appID FROM registration where appID = '"+body.appID+"'";
-	con.query(sqlQuery, function (err, result) {
+	handlerDB(sqlQuery, function(err, result){
 		if (err){ 
 			callback(false,err);
 		}
@@ -20,6 +39,31 @@
 			if(result.length == 0 && body.appID != "null"){
 				var newToken = token.token_generator();
 				var sqlQuery = "INSERT INTO registration ( token, appID, developerID) VALUES ('"+newToken+"','"+body.appID+"','"+body.developerID+"')";
+				
+				handlerDB(sqlQuery, function(err, result){
+					if(err){
+						callback(false, result)
+					}else{
+						email.sendEmailToken(body.developerID, newToken);
+						callback(true,{token: newToken});
+					}
+				})
+			}
+			else{
+				callback(false,"App is already registered or AppID is Null");
+			}
+		}
+	})
+	/*
+	con.query(sqlQuery, function (err, result) {
+		if (err){ 
+			callback(false,err);
+		}
+		else{
+			if(result.length == 0 && body.appID != "null"){
+				var newToken = token.token_generator();
+				var sqlQuery = "INSERT INTO registration ( token, appID, developerID) VALUES ('"+newToken+"','"+body.appID+"','"+body.developerID+"')";
+			
 				con.query(sqlQuery, function (err, result) {
 					if (err){
 						callback(false,result);
@@ -29,12 +73,13 @@
 						callback(true,{token: newToken});
 					}
 				});
+				
 			}
 			else{
 				callback(false,"App is already registered or AppID is Null");
 			}
 		}
-	});
+	});*/
 }
 
 /*
@@ -42,7 +87,7 @@
 */
 module.exports.insertVappByDeveloperid= function(body, callback){
 	var sqlQuery = "SELECT appID FROM registration where appID = '"+body.appID+"'";
-	con.query(sqlQuery, function (err, result) {
+	handlerDB(sqlQuery, function(err, result){
 		if (err){ 
 			callback(false,err);
 		}
@@ -50,6 +95,32 @@
 			if(result.length == 0 && body.appID != "null"){
 				var newToken = token.token_generator();
 				var sqlQuery = "INSERT INTO registration ( token, appID, developerID) VALUES ('"+newToken+"','"+body.appID+"','"+body.developerID+"')";
+				
+				handlerDB(sqlQuery, function(err, result){
+					if(err){
+						callback(false, result);
+					}else{
+						email.sendEmailToken(body.developerID, newToken); 
+						callback(true, {token: newToken})
+					}
+				})
+			}
+			else{
+				callback(false,"App is already registered or AppID is Null");
+			}
+		}
+	})
+	/*
+	con.query(sqlQuery, function (err, result) {
+		if (err){ 
+			callback(false,err);
+		}
+		else{
+			if(result.length == 0 && body.appID != "null"){
+				var newToken = token.token_generator();
+				var sqlQuery = "INSERT INTO registration ( token, appID, developerID) VALUES ('"+newToken+"','"+body.appID+"','"+body.developerID+"')";
+				
+
 				con.query(sqlQuery, function (err, result) {
 					if (err){ 
 						callback(false,result);
@@ -64,6 +135,7 @@
 			}
 		}
 	});
+	*/
 }
 
 /*
@@ -73,14 +145,23 @@
 	date = new Date().toISOString().slice(0, 19).replace('T', ' ');
 	strSubject = data.subject + " = " + data.subjectValue;
 	var sqlQuery = "INSERT INTO notificationlist ( emailTo, subject, date, token) VALUES ('"+data.emailTo+"','"+strSubject+"', '"+date+"', '"+data.token+"')";
-	con.query(sqlQuery, function (err, result) {
+	
+	handlerDB(sqlQuery, function(err, result){
+		if (err){
+			callback(false,err);
+		}
+		else{
+			callback(true, result);
+		}
+	})
+	/*con.query(sqlQuery, function (err, result) {
 		if (err){
 			callback(false,err);
 		}
 		else{
 			callback(true, data);
 		}
-	});
+	});*/
 }
 
 /*
@@ -88,7 +169,23 @@
 */
 module.exports.insertNotificationRules= function(body, callback){
 	var sql = "SELECT token FROM registration where appID = '"+body.appID+"'";
-	con.query(sql, function (err, res) {
+	handlerDB(sql, function(err, res){
+		if (err){
+			callback(false,err);
+		}
+		else{
+			var sqlQuery = "INSERT INTO ruleslist ( description, parameter, conditionValue, controlValue, notificationType, token) VALUES ('"+body.description+"','"+body.parameter+"','"+body.conditionValue+"','"+body.controlValue+"','"+body.notificationType+"','"+res[0].token+"')";
+			handlerDB(sqlQuery, function(err, result){
+				if (err){ 
+					callback(false,err);
+				}
+				else {
+					callback(true);
+				}
+			});
+		}
+	})
+	/*con.query(sql, function (err, res) {
 		if (err){
 			callback(false,err);
 		}
@@ -103,7 +200,7 @@
 				}
 			});
 		}
-	});
+	});*/
 }
 
 /*
@@ -112,7 +209,7 @@
 module.exports.insertStatistics = function(result, subjectValue, subject, rulesid, callback){
 	date = new Date().toISOString().slice(0, 19).replace('T', ' ');
 	var sqlQuery = "INSERT INTO statisticslist ( date, result, subjectValue, subject, rulesid) VALUES ('"+date+"','"+result+"', '"+subjectValue+"', '"+subject+"', '"+rulesid+"')";
-	con.query(sqlQuery, function (err, rows) {
+	handlerDB(sqlQuery, function(err, result){
 		if (err) {
 			logger.info("Update Values for Statistics: " + err);
 			callback(false,err);
@@ -121,7 +218,18 @@
 			logger.info("Update Values for Statistics: Success");
 			callback(true, subject);
 		}
-	});
+	})
+	/*con.query(sqlQuery, function (err, rows) {
+		if (err) {
+			logger.info("Update Values for Statistics: " + err);
+			callback(false,err);
+		}
+		else{
+			logger.info("Update Values for Statistics: Success");
+			callback(true, subject);
+		}
+	});
+	*/
 }
 
 /*
@@ -129,7 +237,7 @@
 */
 module.exports.retrieveApp= function(developerid,callback){
 	var sqlQuery = "SELECT * FROM registration where developerID = '"+developerid+"'";
-	con.query(sqlQuery, function (err, result) {
+	handlerDB(sqlQuery, function(err,result){
 		if (err){
 			callback(false,err);
 		}
@@ -141,7 +249,20 @@
 				callback(true,result);
 			}
 		}
-	});
+	})
+	/*con.query(sqlQuery, function (err, result) {
+		if (err){
+			callback(false,err);
+		}
+		else {
+			if(result.length == 0){
+				callback(false,"DeveloperID incorrect");
+			}
+			else{
+				callback(true,result);
+			}
+		}
+	});*/
 }
 
 /*
@@ -149,6 +270,38 @@
 */
 module.exports.retrieveNotificationList= function(appid,callback){
 	var sql = "SELECT token FROM registration where appID = '"+appid+"'";
+	handlerDB(sql, function(err, res){
+		if (err){
+			callback(false,err);
+		}
+		else{
+			if(res.length == 0){
+				callback(false,"AppID incorrect");
+			}
+			else{
+				var sqlQuery = "SELECT emailTo, subject, date, token FROM notificationlist where token = '"+res[0].token+"'";
+				handlerDB(sqlQuery, function(err, rows){
+					if (err){ 
+						callback(false,err);
+					}
+					else {
+						callback(true,rows);
+					}
+				})
+				/*
+				con.query(sqlQuery, function (err, rows) {
+					if (err){ 
+						callback(false,err);
+					}
+					else {
+						callback(true,rows);
+					}
+				});
+				*/
+			}
+		}
+	})
+	/*
 	con.query(sql, function (err, res) {
 		if (err){
 			callback(false,err);
@@ -170,6 +323,7 @@
 			}
 		}
 	});
+	*/
 }
 
 /*
@@ -177,6 +331,38 @@
 */
 module.exports.retrieveRulesList= function(appid,callback){
 	var sql = "SELECT token FROM registration where appID = '"+appid+"'";
+	handlerDB(sql, function(err, res){
+		if (err){
+			callback(false,err);
+		}
+		else{
+			if(res.length == 0){
+				callback(false,"AppID incorrect");
+			}
+			else{
+				var sqlQuery = "SELECT * FROM ruleslist where token = '"+res[0].token+"'";
+				handlerDB(sqlQuery, function(err, rows){
+					if (err){
+						callback(false,err);
+					}
+					else{
+						callback(true,rows);
+					}
+				})
+				/*
+				con.query(sqlQuery, function (err, rows) {
+					if (err){
+						callback(false,err);
+					}
+					else{
+						callback(true,rows);
+					}
+				});
+				*/
+			}
+		}
+	})
+	/*
 	con.query(sql, function (err, res) {
 		if (err){
 			callback(false,err);
@@ -198,6 +384,7 @@
 			}
 		}
 	});
+	*/
 }
 
 /*
@@ -231,7 +418,7 @@
 	var percentage = null;
 
 	var sqlQuery = "SELECT * FROM statisticslist where rulesID = '"+ruleid+"'";
-	con.query(sqlQuery, function (err, rows) {
+	handlerDB(sqlQuery, function(err, rows){
 		if (err) {
 			callback(false,err);
 		}
@@ -260,7 +447,39 @@
 
 			callback(true, rows, {totalNotifications: totalNotifications, totalNotificationsApplyByRules: totalNotificationsApplyByRules, averageValue: averageValue, percentage: percentage});
 		}
-	});
+	})
+	/*
+	con.query(sqlQuery, function (err, rows) {
+		if (err) {
+			callback(false,err);
+		}
+		else{
+			totalNotifications = rows.length;
+			
+			if(totalNotifications == 0){
+				totalNotifications = 0;
+				totalValue = 0;
+				totalNotificationsApplyByRules = 0;
+				averageValue = 0.0;
+				percentage = 0.0;
+			}else{
+				for (var i = 0; i < totalNotifications; i++){
+					if(rows[i].result == "true"){
+						totalNotificationsApplyByRules++;
+						totalValue = parseFloat(totalValue + parseFloat(rows[i].subjectValue));
+					}
+					else{
+						totalValue = parseFloat(totalValue + parseFloat(rows[i].subjectValue));
+					}
+				}
+				averageValue = (totalValue / totalNotifications).toFixed(2);
+				percentage = ((parseInt(totalNotificationsApplyByRules) * 100 ) /  parseInt(totalNotifications)).toFixed(2);
+			}
+
+			callback(true, rows, {totalNotifications: totalNotifications, totalNotificationsApplyByRules: totalNotificationsApplyByRules, averageValue: averageValue, percentage: percentage});
+		}
+	});
+	*/
 }
 
 /*
@@ -268,6 +487,15 @@
 */
 module.exports.getRulesListByToken = function(token, callback){
 	var sqlQuery = "SELECT * FROM ruleslist where token = '"+token+"'";
+	handlerDB(sqlQuery, function(err, ruleslist){
+		if (err) {
+			callback(false,err);
+		}
+		else{
+			callback(true, ruleslist);
+		}
+	})
+	/*
 	con.query(sqlQuery, function (err, ruleslist) {
 		if (err) {
 			callback(false,err);
@@ -276,6 +504,7 @@
 			callback(true, ruleslist);
 		}
 	});
+	*/
 }
 
 /*
@@ -283,7 +512,7 @@
 */
 module.exports.checkIfTokenExists = function(token, callback){
 	var sql = "SELECT token FROM registration where token = '"+token+"'";
-	con.query(sql, function (err, res) {
+	handlerDB(sql, function(err, res){
 		if (err){
 			callback(false);
 		}
@@ -295,7 +524,22 @@
 				callback(false, "Invalid Token");
 			}
 		}
-	});
+	})
+	/*
+	con.query(sql, function (err, res) {
+		if (err){
+			callback(false);
+		}
+		else{
+			if(res.length == 1){
+				callback(true, res);
+			}
+			else{
+				callback(false, "Invalid Token");
+			}
+		}
+	});
+	*/
 }
 
 /*
@@ -303,7 +547,44 @@
 */
 module.exports.deleteVapp= function(appid, callback){
 	var sql = "SELECT token FROM registration where appID = '"+appid+"'";
-	con.query(sql, function (err, res) {
+	handlerDB(sql, function(err, res){
+		if (err){ 
+			callback(false,err);
+		}
+		else{
+			if(res.length == 0){
+				callback(false,"AppID incorrect");
+			}
+			else{
+				var sqlNotifcations = "DELETE FROM notificationlist where token = '"+res[0].token+"'";
+				handlerDB(sqlNotifcations, function(err, rows){
+					if (err) {
+						callback(false,err);
+					}
+					else{
+						var sqlRules = "DELETE FROM ruleslist where token = '"+res[0].token+"'";
+						handlerDB(sqlRules, function(err, rows){
+							if (err) {
+								callback(false,err);
+							}
+							else{
+								var sqlQuery = "DELETE FROM registration where token = '"+res[0].token+"'";
+								con.query(sqlQuery, function (err, rows) {
+									if (err){ 
+										callback(false,err);
+									}
+									else{
+										callback(true, rows);
+									}
+								});
+							}
+						})
+					}
+				})
+			}
+		}
+	})
+	/*con.query(sql, function (err, res) {
 		if (err){ 
 			callback(false,err);
 		}
@@ -339,7 +620,7 @@
 				});
 			}
 		}
-	});
+	});*/
 }
 
 /*
@@ -347,7 +628,7 @@
 */
 module.exports.deleteRule= function(rulesid, callback){
 	var sqlQuery = "DELETE FROM ruleslist where rulesID = '"+rulesid+"'";
-	con.query(sqlQuery, function (err, rows) {
+	handlerDB(sqlQuery, function(err, rows){
 		if (err) {
 			callback(false,err);
 		}
@@ -359,5 +640,20 @@
 				callback(true,rows);
 			}
 		}
-	});
+	})
+	/*
+	con.query(sqlQuery, function (err, rows) {
+		if (err) {
+			callback(false,err);
+		}
+		else{
+			if(rows.length == 0){
+				callback(false,"RuleID incorrect");
+			}
+			else{
+				callback(true,rows);
+			}
+		}
+	});
+	*/
 }