DB2. Resources. Overhead.

I want to point out a potential “gotcha” to anyone moving from the traditional LAMP (Linux, Apache, MySQL, PHP) stack to PHP on the IBM System i + DB2 (is there an acronym for that yet? iADP?).

PHP loves MySQL… so much so that MySQL has always received favorable treatment. Although I love MySQL too, I was a bit upset when that close relationship cost me a lot of time with my new datasource. Lets be honest, one of the larger overheads in your application is the connection it has to make to the database - although it usually isn’t noticeable, it still isn’t as fast as the code itself. However, if your PHP programs are constantly calling db2_connect you are certainly going to see some overhead…

Generally this wont affect you - simply because you shouldn’t be making multiple calls to db2_connect in your code. In my case, I was developing on a Linux + MySQL server at first, and was using mysql_connect wrapped within a class following the Singleton pattern. In my tests on the Linux machine I saw no problems, and it seemed to work as expected (anytime my application requested the connection - the class returned that same open database resource link). However, when I ported it to the System i and DB2 I saw my same programs go from < 1 second execution to 6 seconds! Here is the gotcha:

“If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.” –PHP Help Manual

Do not let them fool you - although db2_connect looks very similar to mysql_connect in the manual - it will not emulate this feature! Unfortunately the egg was on my face, simply because the Singleton class I thought was working - turned out to have a slight mistype in it. Now, you are probably thinking that my application was horribly written to have such a performance impact from a few extra DB2 connect calls… well, that wasn’t the case - because at most I may have checked for a db connection at approximately 4-5 locations during runtime. But what I discovered from IBM (and from error logs) is that opening a second connection (or link resource) in PHP can cause a lot of errors. This is also a problem with other CGI interfaces (confirmed by IBM support).

Although this slight difference in connection implementation cost me many an hour, it did help me find a problem in design. Good or bad, take it as you will - but take note and ensure you’re not wasting precious resources =)

Comments are closed.