Try fix connection disconnect by server
This commit is contained in:
		
							parent
							
								
									81f67c1420
								
							
						
					
					
						commit
						d4d0c3155d
					
				@ -103,8 +103,9 @@ mySQL::mySQL(const string _path, const string _username, const string _password,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
   drv = get_mysql_driver_instance();
 | 
					   drv = get_mysql_driver_instance();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   bot = async(launch::async, [&]{
 | 
					   bot = async(launch::async, [&](){
 | 
				
			||||||
      while (runBot) {
 | 
					      while (runBot) {
 | 
				
			||||||
 | 
					         sleep(1);
 | 
				
			||||||
         while (available>con.size() && runBot) {
 | 
					         while (available>con.size() && runBot) {
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
               Connection* new_con_ptr = create_con();
 | 
					               Connection* new_con_ptr = create_con();
 | 
				
			||||||
@ -122,11 +123,19 @@ mySQL::mySQL(const string _path, const string _username, const string _password,
 | 
				
			|||||||
               cout << except << endl;
 | 
					               cout << except << endl;
 | 
				
			||||||
            }     
 | 
					            }     
 | 
				
			||||||
         }
 | 
					         }
 | 
				
			||||||
         usleep(1000); // za smanjenje zauzeća procesora
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					         for (int i=0; i<con.size() && runBot; i++) {
 | 
				
			||||||
 | 
					            if (!con[i]->isValid()) {
 | 
				
			||||||
 | 
					               cout << "Connection is not valid, remove it from pool!" << endl;
 | 
				
			||||||
 | 
					               io.lock();
 | 
				
			||||||
 | 
					               con.erase(con.begin()+i);
 | 
				
			||||||
 | 
					               io.unlock();
 | 
				
			||||||
 | 
					               i--;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					         }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
   });
 | 
					   });  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -297,18 +306,17 @@ void mySQL::getColumns(const string _table, vector<string> &_columns, Connection
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
Connection* mySQL::shift_con() {
 | 
					Connection* mySQL::shift_con() {
 | 
				
			||||||
   while (true) {
 | 
					   while (true) {
 | 
				
			||||||
      io.lock();
 | 
					      while(con.size()) {
 | 
				
			||||||
      for (int i=0; i<con.size(); i++) {
 | 
					         io.lock();
 | 
				
			||||||
         Connection* con_ptr = con[0];
 | 
					         Connection* con_ptr = con[0];
 | 
				
			||||||
         con.pop_front();
 | 
					         con.pop_front();
 | 
				
			||||||
         if (con_ptr->isValid()) {
 | 
					         if (con_ptr->isValid()) {
 | 
				
			||||||
            io.unlock();
 | 
					            io.unlock();
 | 
				
			||||||
            return con_ptr;
 | 
					            return con_ptr;
 | 
				
			||||||
         } else {
 | 
					         } 
 | 
				
			||||||
            --i;
 | 
					         io.unlock();
 | 
				
			||||||
         }
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      io.unlock();
 | 
					      usleep(1000);
 | 
				
			||||||
   }
 | 
					   }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,7 @@ int main() {
 | 
				
			|||||||
        mySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5);
 | 
					        mySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // sleep(3600*10);
 | 
					        // sleep(3600*10);
 | 
				
			||||||
        sleep(30);
 | 
					        sleep(20);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        auto start = high_resolution_clock::now();
 | 
					        auto start = high_resolution_clock::now();
 | 
				
			||||||
@ -99,24 +99,27 @@ int main() {
 | 
				
			|||||||
        // t6.join();
 | 
					        // t6.join();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//        one by one
 | 
					//        one by one
 | 
				
			||||||
        // try {
 | 
					        try {
 | 
				
			||||||
        //     sqlQA test_qa;
 | 
					            sqlQA test_qa;
 | 
				
			||||||
        //     test_qa.select().from("records").where("enabled = 1");
 | 
					            test_qa.select().from("records").where("enabled = 1");
 | 
				
			||||||
        //     mydb.exec(test_qa);
 | 
					            mydb.exec(test_qa);
 | 
				
			||||||
        //     test_qa.print(true);
 | 
					            test_qa.print(true);
 | 
				
			||||||
        // } catch (const string err) {
 | 
					        } catch (const string err) {
 | 
				
			||||||
        //     cout << err << endl;
 | 
					            cout << err << endl;
 | 
				
			||||||
        // }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        sleep(20);
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        // try {
 | 
					        try {
 | 
				
			||||||
        //     sqlQA test_qa;
 | 
					            sqlQA test_qa;
 | 
				
			||||||
        //     test_qa.select().from("users");
 | 
					            test_qa.select().from("users");
 | 
				
			||||||
        //     mydb.exec(test_qa);
 | 
					            mydb.exec(test_qa);
 | 
				
			||||||
        //     test_qa.print(true);
 | 
					            test_qa.print(true);
 | 
				
			||||||
        // } catch (const string err) {
 | 
					        } catch (const string err) {
 | 
				
			||||||
        //     cout << err << endl;
 | 
					            cout << err << endl;
 | 
				
			||||||
        // }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        sleep(20);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            sqlQA test_qa;
 | 
					            sqlQA test_qa;
 | 
				
			||||||
@ -143,7 +146,7 @@ int main() {
 | 
				
			|||||||
        cout << "Jebi ga" << endl;
 | 
					        cout << "Jebi ga" << endl;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    sleep(600);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										
											BIN
										
									
								
								test/test.o
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								test/test.o
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user