Loading ...
Sorry, an error occurred while loading the content.

Persistent MySQL Connection????

Expand Messages
  • k_berov
    Hi all. Could anybody tell me how to setup and handle persistent connections using DBI::mysql? I am not allowed to use Apache::DBI. I tried: sub
    Message 1 of 4 , Apr 26, 2004
    • 0 Attachment
      Hi all.
      Could anybody tell me how to setup and handle persistent connections
      using DBI::mysql?
      I am not allowed to use Apache::DBI.
      I tried:

      sub Application_OnStart{
      $dbh = DBI->connect(
      "DBI:mysql:database=mydatabase","me","mypassword",
      {
      PrintError => 1, # warn( ) on errors
      RaiseError => 0, # don't die on error
      AutoCommit => 1, # commit executes immediately
      }

      )|| sub{$DBI::errstr;undef $dbh};
      }

      #and then

      sub Application_OnEnd{
      sub{if($dbh){$dbh->disconnect} };
      }

      #A chek

      sub check_connection_and_connect_if_needed{
      if (!$dbh){
      $dbh = DBI->connect(
      "DBI:mysql:database=f8","f8","qazwsx",
      {
      PrintError => 1, # warn( ) on errors
      RaiseError => 0, # don't die on error
      AutoCommit => 1, # commit executes immediately
      }

      )|| sub{$DBI::errstr;undef $dbh};
      }
      }

      sub Script_OnStart{

      check_connection_and_connect_if_needed();
      #.....
      }

      Where to put

      $Server->RegisterCleanup(sub{if($dbh){$dbh->disconnect} });
      if the server is restarted or killed

      Is there a standart way
      or som tweaking?

      Thank in advance.



      ---------------------------------------------------------------------
      To unsubscribe, e-mail: asp-unsubscribe@...
      For additional commands, e-mail: asp-help@...
    • Josh Chamas
      ... Use Script_OnStart, which gets run each script request, since you need the database connection initilized per process. A simple method might look like:
      Message 2 of 4 , Apr 26, 2004
      • 0 Attachment
        k_berov wrote:
        > Hi all.
        > Could anybody tell me how to setup and handle persistent connections
        > using DBI::mysql?
        > I am not allowed to use Apache::DBI.
        > I tried:
        >
        > sub Application_OnStart{
        > $dbh = DBI->connect(
        > "DBI:mysql:database=mydatabase","me","mypassword",
        > {
        > PrintError => 1, # warn( ) on errors
        > RaiseError => 0, # don't die on error
        > AutoCommit => 1, # commit executes immediately
        > }
        >
        > )|| sub{$DBI::errstr;undef $dbh};
        > }
        >

        Use Script_OnStart, which gets run each script request, since you need
        the database connection initilized per process. A simple method might look like:

        use vars qw($dbh);
        sub Script_OnStart {
        unless($dbh && eval { $dbh->ping }) {
        $dbh = DBI->connect(...);
        }
        }

        >
        > Where to put
        >
        > $Server->RegisterCleanup(sub{if($dbh){$dbh->disconnect} });
        > if the server is restarted or killed
        >

        Well, if you want the connect to be "persistent", then don't do
        this anywhere. But if you want the connection disconnected each
        request, then you can do this just after the unless{} block above
        in the Script_OnStart, but then you can get rid of the unless block
        altogether, and just have the DBI->connect part since you are not
        doing persistent connections.

        > Is there a standart way
        > or som tweaking?
        >

        I would say that using Apache::DBI is standard, but this is fine too.

        Regards,

        Josh

        ________________________________________________________________________
        Josh Chamas, Founder | NodeWorks - http://www.nodeworks.com
        Chamas Enterprises Inc. | NodeWorks Directory - http://dir.nodeworks.com
        http://www.chamas.com | Apache::ASP - http://www.apache-asp.org



        ---------------------------------------------------------------------
        To unsubscribe, e-mail: asp-unsubscribe@...
        For additional commands, e-mail: asp-help@...
      • k_berov
        Thak you very much!!! So, if I understood clearly, I have to never disconnect. But doesn t it mean that i will leave an opened connection to MySql when Apache
        Message 3 of 4 , Apr 26, 2004
        • 0 Attachment
          Thak you very much!!!
          So, if I understood clearly, I have to never disconnect.

          But doesn't it mean that i will leave an opened connection
          to MySql when Apache is stopped or my application crashes?
          My application is just one of the many on the ISP where I am hosted.
          Or I am $dbh->ping-ing the same connection?

          Thanks!!!

          --- In apache-asp@yahoogroups.com, Josh Chamas <josh@c...> wrote:
          > k_berov wrote:
          > > Hi all.
          > > Could anybody tell me how to setup and handle persistent connections
          > > using DBI::mysql?
          > > I am not allowed to use Apache::DBI.
          > > I tried:
          > >
          > > sub Application_OnStart{
          > > $dbh = DBI->connect(
          > > "DBI:mysql:database=mydatabase","me","mypassword",
          > > {
          > > PrintError => 1, # warn( ) on errors
          > > RaiseError => 0, # don't die on error
          > > AutoCommit => 1, # commit executes immediately
          > > }
          > >
          > > )|| sub{$DBI::errstr;undef $dbh};
          > > }
          > >
          >
          > Use Script_OnStart, which gets run each script request, since you need
          > the database connection initilized per process. A simple method
          might look like:
          >
          > use vars qw($dbh);
          > sub Script_OnStart {
          > unless($dbh && eval { $dbh->ping }) {
          > $dbh = DBI->connect(...);
          > }
          > }
          >
          > >
          > > Where to put
          > >
          > > $Server->RegisterCleanup(sub{if($dbh){$dbh->disconnect} });
          > > if the server is restarted or killed
          > >
          >
          > Well, if you want the connect to be "persistent", then don't do
          > this anywhere. But if you want the connection disconnected each
          > request, then you can do this just after the unless{} block above
          > in the Script_OnStart, but then you can get rid of the unless block
          > altogether, and just have the DBI->connect part since you are not
          > doing persistent connections.
          >
          > > Is there a standart way
          > > or som tweaking?
          > >
          >
          > I would say that using Apache::DBI is standard, but this is fine too.
          >
          > Regards,
          >
          > Josh
          >
          > ________________________________________________________________________
          > Josh Chamas, Founder | NodeWorks - http://www.nodeworks.com
          > Chamas Enterprises Inc. | NodeWorks Directory - http://dir.nodeworks.com
          > http://www.chamas.com | Apache::ASP - http://www.apache-asp.org
          >
          >
          >
          > ---------------------------------------------------------------------
          > To unsubscribe, e-mail: asp-unsubscribe@p...
          > For additional commands, e-mail: asp-help@p...


          ---------------------------------------------------------------------
          To unsubscribe, e-mail: asp-unsubscribe@...
          For additional commands, e-mail: asp-help@...
        • Josh Chamas
          ... Doing a disconnect() would be nice of you, since that would free up one MySQL server side connection thread for other customers, but you would have a
          Message 4 of 4 , Apr 26, 2004
          • 0 Attachment
            k_berov wrote:
            > Thak you very much!!!
            > So, if I understood clearly, I have to never disconnect.
            >
            > But doesn't it mean that i will leave an opened connection
            > to MySql when Apache is stopped or my application crashes?
            > My application is just one of the many on the ISP where I am hosted.
            > Or I am $dbh->ping-ing the same connection?
            >

            Doing a disconnect() would be nice of you, since that would free up
            one MySQL server side connection thread for other customers, but you would
            have a slight performance benefit from not doing the disconnect
            since you would not have to connect on each request if your site
            is very busy. In a shared environment, I would not do the persistent
            connection & I would disconnect, as you would be a good citizen then.

            When Apache is stopped or crashes, a persistent MySQL connection
            will automatically end with the apache/mod_perl process that
            was connected to it.

            Regards,

            Josh
            ________________________________________________________________________
            Josh Chamas, Founder | NodeWorks - http://www.nodeworks.com
            Chamas Enterprises Inc. | NodeWorks Directory - http://dir.nodeworks.com
            http://www.chamas.com | Apache::ASP - http://www.apache-asp.org



            ---------------------------------------------------------------------
            To unsubscribe, e-mail: asp-unsubscribe@...
            For additional commands, e-mail: asp-help@...
          Your message has been successfully submitted and would be delivered to recipients shortly.