-1

Hi guys I'm new here and this is my first post. I have a project with log4php and I can't make a persistent connection happen.

Is it possible to establish a persistent connection (pconnect) with the LoggerAppenderPDO through the configuration in log4php? Is it possible at all?

I've tried to add 'options' to the following configuration, but it doesn't work:

array(
    'appenders' => array(
        'default' => array(
            'class' => 'LoggerAppenderPDO',
            'params' => array(
                'dsn' => "mysql:host=localhost;dbname=logdb",
                'user' => 'logger',
                'password' => 'admin',
                'options' => array(PDO::ATTR_PERSISTENT => true),
                'table' => 'log4php'
            )
        )
    ),
    'rootLogger' => array(
        'appenders' => array('default'),
    ),
);

Can somebody help me out? I would really appreciate it.

Sven
  • 69,403
  • 10
  • 107
  • 109

1 Answers1

0

The question is: "Why do you want a persistent connection?"

Log4PHP will only attempt to create ONE connection for logging during the request, so a persistent connection wouldn't make any difference during the request.

The second thing is: Persistent connections can only be created if PHP runs as an Apache module. All other situations do not support them.

Third: Persistent connections do more harm than good if not used correctly. One aspect is that for every apache child that eventually runs, the database must offer a permanent connection ability. So if you have Apache allowing 100 childs, your database must support at least 100 concurrent connections just for logging. And you probably need more connections because you do not only do logging, but also some useful stuff with the database. The other point is that if your script crashes, the connection stays open, which might mean that a lock is not released or a transaction is not canceled. And last but not least: Re-using a persistent connection uses it in the state it was left - which might not be what you think it is.

Fourth: Persistent connections to MySQL are not that much faster than regular connections, if at all. MySQL is optimized to support quick connection times (which might not be true for other databases).

So in the end, there isn't really much incentive left to use persistent connections. If you insist using them for vague "performance reasons", you should prove they are worth the hassle by measuring the impact they have on your application performance.

Update: I think I can add this performance comparison states that using persistent connections does NOT improve performance in any way. Your mileage may still vary.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • I noticed, that the page builds up slower because of the db connection. I believe it's because LoggerAppenderPDO tries to establish every time a db connection. If I turn this particular appender off the page appears quicker. I'm confident to pick the right design for the persistent connection. I just need to know how. Do you have any suggestions so I can see for myself? – user2836066 Oct 01 '13 at 20:48
  • You have no data to back your statement so far. Remember that logging is not only creating a database connection, but executing queries as well. These take up the majority of the time, so if you turn logging off, there is a time difference of course. Which storage engine are you using? MyISAM, InnoDB or perhaps ARCHIVE? – Sven Oct 01 '13 at 22:50
  • Hi Sven, I'm using InnoDB and am testing on my local machine right now. Well in fact I'm not even logging data. All I do is to get an instance of the logger object and to pass the configuration, so it establishes a connection to the db: Logger::getLogger('allLoggers')->configure($config); No logs happen at this time, only a connection. – user2836066 Oct 02 '13 at 16:11
  • Your code looks weird? Are you really first getting the logger and then try to configure it? That's the wrong way. You have to first configure the `Logger` object with a static call, then get the logger objects you want - and they are configured then. – Sven Oct 06 '13 at 23:26