Add on error callback
This commit is contained in:
		
							parent
							
								
									777ee53355
								
							
						
					
					
						commit
						27074f9269
					
				
							
								
								
									
										4
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							@ -59,6 +59,8 @@
 | 
			
		||||
        "stop_token": "cpp",
 | 
			
		||||
        "streambuf": "cpp",
 | 
			
		||||
        "cinttypes": "cpp",
 | 
			
		||||
        "typeinfo": "cpp"
 | 
			
		||||
        "typeinfo": "cpp",
 | 
			
		||||
        "any": "cpp",
 | 
			
		||||
        "variant": "cpp"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -106,6 +106,7 @@ class MySQL {
 | 
			
		||||
    bool run_engin = true;
 | 
			
		||||
    future<void> periodic_engin;
 | 
			
		||||
    periodical_engine engine_type;
 | 
			
		||||
    function<void(const string&)> on_error;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Open one database server connection
 | 
			
		||||
@ -165,6 +166,11 @@ class MySQL {
 | 
			
		||||
    */
 | 
			
		||||
    void set_connect_trys(const uint32_t _trys);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *  Set callback on error
 | 
			
		||||
     */
 | 
			
		||||
    void set_on_error(function<void(const string&)> _on_error);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Execute the SQL statement
 | 
			
		||||
    */
 | 
			
		||||
 | 
			
		||||
@ -42,8 +42,9 @@ Connection* marcelb::mysql::MySQL::create_connection() {
 | 
			
		||||
         }
 | 
			
		||||
      }
 | 
			
		||||
      catch (const SQLException &error) {
 | 
			
		||||
         cout << error.what() << endl;
 | 
			
		||||
         // on_error -- ako se ikad impelementira pozovi ga ovdje!
 | 
			
		||||
         if (on_error) {
 | 
			
		||||
            on_error(error.what() + string(", SQL state: ") + error.getSQLState() + string(", Error code: ") + to_string(error.getErrorCode()));
 | 
			
		||||
         }
 | 
			
		||||
         usleep(reconnectSleep);
 | 
			
		||||
         connect_trys == unlimited ? trys : trys++;
 | 
			
		||||
      }         
 | 
			
		||||
@ -78,7 +79,9 @@ bool marcelb::mysql::MySQL::disconnect_connection(Connection* connection) {
 | 
			
		||||
         status = !connection->isClosed();
 | 
			
		||||
      } 
 | 
			
		||||
      catch (const SQLException &error) {
 | 
			
		||||
         cout << error.what() << endl;
 | 
			
		||||
         if (on_error) {
 | 
			
		||||
            on_error(error.what() + string(", SQL state: ") + error.getSQLState() + string(", Error code: ") + to_string(error.getErrorCode()));
 | 
			
		||||
         }
 | 
			
		||||
         status = true;
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
@ -101,6 +104,10 @@ void marcelb::mysql::MySQL::set_connect_trys(const uint32_t _trys) {
 | 
			
		||||
   connect_trys = _trys;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void  marcelb::mysql::MySQL::set_on_error(function<void(const string&)> _on_error) {
 | 
			
		||||
   on_error = _on_error;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Connection* marcelb::mysql::MySQL::occupy_connection() {
 | 
			
		||||
   unique_lock<mutex> lock(io);
 | 
			
		||||
   while (connection_pool.empty()) {
 | 
			
		||||
@ -136,8 +143,6 @@ marcelb::mysql::MySQL::~MySQL() {
 | 
			
		||||
      run_engin = false;
 | 
			
		||||
      periodic_engin.get();
 | 
			
		||||
   } else {
 | 
			
		||||
      // ne bi bilo loše ubiti periodic nekako?!
 | 
			
		||||
      // iako disconnecta može periodic connect napraviti!!!
 | 
			
		||||
      run_engin = false;
 | 
			
		||||
   }
 | 
			
		||||
   
 | 
			
		||||
@ -147,15 +152,22 @@ marcelb::mysql::MySQL::~MySQL() {
 | 
			
		||||
 | 
			
		||||
void marcelb::mysql::MySQL::periodic_maintenance() {
 | 
			
		||||
   for (size_t i = 0; i < pool_size && run_engin; i++) {
 | 
			
		||||
      Connection *conn = occupy_connection();
 | 
			
		||||
      if (conn->isValid()) {
 | 
			
		||||
         release_connection(conn);
 | 
			
		||||
      } else {
 | 
			
		||||
         if (!conn->isClosed()){
 | 
			
		||||
               conn->close();
 | 
			
		||||
      try {
 | 
			
		||||
         
 | 
			
		||||
         Connection *conn = occupy_connection();
 | 
			
		||||
         if (conn->isValid()) {
 | 
			
		||||
            release_connection(conn);
 | 
			
		||||
         } else {
 | 
			
		||||
            if (!conn->isClosed()){
 | 
			
		||||
                  conn->close();
 | 
			
		||||
            }
 | 
			
		||||
            Connection *n_conn = create_connection();
 | 
			
		||||
            release_connection(n_conn);
 | 
			
		||||
         }
 | 
			
		||||
      } catch (const SQLException &error) {
 | 
			
		||||
         if (on_error) {
 | 
			
		||||
            on_error(error.what() + string(", SQL state: ") + error.getSQLState() + string(", Error code: ") + to_string(error.getErrorCode()));
 | 
			
		||||
         }
 | 
			
		||||
         Connection *n_conn = create_connection();
 | 
			
		||||
         release_connection(n_conn);
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
@ -17,6 +17,10 @@ int main() {
 | 
			
		||||
        // MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 10, periodical_engine::external);
 | 
			
		||||
        MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5);
 | 
			
		||||
 | 
			
		||||
        mydb.set_on_error( [](const string& error) {
 | 
			
		||||
            cout << error << endl;
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        // periodic mysql_maintenance ( [&mydb] () {
 | 
			
		||||
        //     mydb.periodic_maintenance();
 | 
			
		||||
        // }, MYSQL_PERIODIC_INTERNAL_TIME);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user