1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved How would do I go about keeping mySQL connection open?

Discussion in 'Development' started by isPops, Jul 21, 2019.

  1. isPops

    isPops Silverfish

    Messages:
    19
    Right now im using a task which does not work and is probably causing lag.

    is there something I have to do because when players try to access the database the server crashes.
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    show da code you have
     
  3. KingOfTurkey38

    KingOfTurkey38 Spider

    Messages:
    7
    You can make a task that keeps pinging your Database
    PHP:
    class MySQLPingTask extends Task {

        
    /**
         * MySQLPingTask constructor.
         * @param Main $plugin
         */
        
    public function __construct(Main $plugin){
            
    $this->plugin $plugin;
        }

        
    /**
         * @param int $currentTick
         */
        
    public function onRun(int $currentTick){
            
    $plugin $this->plugin;
            if(!
    $plugin->getDb()->ping()){
                
    $plugin->openProvider();
            }
        }
    }
     
  4. isPops

    isPops Silverfish

    Messages:
    19
    I figured out myself last night and this is probably not the best way.

    What im doing is making mySQL do a query every 25 seconds. if this way is bad show me a better optimized way also I have not tried the one above.

    My Code:
    Code:
    public function onRun($currentTick) {
            $this->runTimes--;
    
            if($this->runTimes == 0)
            {
                $this->timeOutCheck();
            }
        }
    
        public function timeOutCheck()
        {
            $this->plugin->sql->query("SELECT * FROM users WHERE name = 'timeOut");
            $this->runTimes = 25;
            $this->plugin->getScheduler()->cancelTask($this->getTaskId());
            $this->plugin->getScheduler()->scheduleDelayedRepeatingTask($this, 25, 20);
        }
     
  5. isPops

    isPops Silverfish

    Messages:
    19
    wouldn't this cause lag calling it every tick ? or am i wrong
     
  6. KingOfTurkey38

    KingOfTurkey38 Spider

    Messages:
    7
    Make repeating task every 20x120 ticks (every 120 seconds if your server is always 20tps)
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.