CodeIgniter Forums
HELP!! Database needs to be reconnected - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: HELP!! Database needs to be reconnected (/showthread.php?tid=4347)



HELP!! Database needs to be reconnected - El Forum - 11-19-2007

[eluser]Namulator[/eluser]
Hey guys,

I am using CI in a multi-threaded daemon which I have working fine, but here's my problem... The daemon acts similar to a telnet daemon, which allows multiple connections coming in. Upon connection, a new fork is created for that connection session. The database connections works the first connection, but when that forked process is finished, the database connection is severed for some reason. The next time I do a database call, the database connection is dropped, so I get an error saying that the database isn't available because connection has been dropped.

Is there a way that I can tell the database module to reconnect upon getting disconnected?... I have persistent connection enabled, but that does not change anything.

If there is a function call which I can do each time a new fork is done, so that the database for that fork is connected that would be perfect.

Can anyone help me with this?... Thanks in advance


HELP!! Database needs to be reconnected - El Forum - 11-19-2007

[eluser]John Fuller[/eluser]
You will probably get a better response by posting in a forum that caters to whatever database software you are using.


HELP!! Database needs to be reconnected - El Forum - 11-20-2007

[eluser]Namulator[/eluser]
The database in question is MySQL, and NO, going into a MySQL support forum will not help the problem, because with the PHP MySQL libraries, I can execute a Database Connection for each forked process. But because of CI, I cannot do that with the included database class within CI, which is why I am asking here.


HELP!! Database needs to be reconnected - El Forum - 11-20-2007

[eluser]Namulator[/eluser]
Well, after reading the source code for the various DB libs, I found out how to accomplish my goal, but I do believe there is one draw back. There is one extra connection attempt to the DB which occurs, then is dropped immediately. This occurs once during the initial process start, which is a waste of a connection, but perhaps I or someone here will find a way around that...

Here's what I have done...

------------------Stripped down code------------------
Code:
class DaemonLib{
  function Start() {
    $CI =& get_instance();
    $CI->load->database();
    $CI->db->close();
    $pid = pcntl_fork();
    if ($pid == -1) {
      if ($this->Debug==TRUE) echo "Failed to fork";
    } else {
      $CI->db->initialize();
      /* Do code calls within new process here */
    }
  }
}
------------------Stripped down code------------------

Because the first "$CI->load->database();" connection is not required for this library, I have to scrap the connection by calling the close call. Then, once the new thread has been forked, I can then re-initialize the connection, which is then working within that thread.


Does anyone know of a way to load the library, without having it connect upon loading the library?... I tried doing "$CI->load->library('database');" but that did not work.