TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-06-2008, 04:01 PM   #1 (permalink)
The Wanderer
 
Join Date: Apr 2008
Location: Cloud 9
Posts: 19
Thanks: 0
Jenski is on a distinguished road
Help Object Aggregation

I'm using a Database Connection class in a web app I am writing as I find myself calling the same functions often.

I was looking into using Aggregation in order to achieve this when I came across this article:

Page 2 - Object Interaction in PHP: Introduction to Aggregation, part 1

But it says:

Quote:
If we talk about performance issues, the advantages of aggregation mainly come from its lower overload, since most of the time only one object is shared by other objects. However, this advantage might be discarded in the case of having a class for database connection shared by other multiple classes. You may run into difficulties if multiple database connections are established to the same server, causing a noticeable detriment to the system, particularly if your site is attracting many visitors.
I was wondering if I could get around this using PHP5s features by cloning the object then unsetting it each time i'm using it, or if there any other alternatives?

Thanks

Jen
Jenski is offline  
Reply With Quote
Old 08-06-2008, 09:15 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

I'm not sure if this is exactly what you're asking, but you're system should be programmed in such a way that every time a unique visitor accesses a page, there should only ever be one distinct connection to the database (not multiple connections for the same visitor). If you're opening a connection three times for a single page view, there's something wrong - a hundred visitors, a hundred connections, there's no avoiding that, and your server will have to be able to handle the load.
-m
delayedinsanity is offline  
Reply With Quote
Old 08-07-2008, 09:53 AM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

I'm not quite sure why aggregation per se would be the cause of this, but bad design would.
If your system is managing to open multiple database connections per execution there is defiantly something wrong.

A singleton DB connection class would combat this somewhat:
PHP Code:
<?php
class DBConn
{
    private static 
$pDB;
    const 
HOST 'localhost';
    const 
USER 'root';
    const 
PASS '';
    
/*
     * Creates a single mysql connection
     */
    
public static function connect()
    {
        if(!
is_resource(self::$pDB))
        {
            
self::$pDB mysql_connect(self::HOSTself::USERself::PASS
                or 
self::throwException('Cannot connect to the database');
        }
        return 
self::$pDB;
    }
    
/*
     * Added so that i can throw exceptions using 'or' on function calls
     * I.E. mysql_connect([params]) or self::throwException([message])
     */
    
public static function throwException($szMsg)
    {
        throw new 
Exception($szMsg);
    }
    
/*
     * cannot create new object from a static class
     */
    
private function __construct() {}
    private function 
__clone() {}
}
To show it in action (sort of):
PHP Code:
class test
{
    private 
$pDb;
    
    public function 
__construct()
    {
        
$this->pDb DBConn::connect();
    }
}
try{
    
$lol = new test();
}catch(
Exception $e){
    echo 
$e->getMessage();

also just a note, mysql_connect will reuse an existing connection if possible (if the same args are passed in, according to the manual) so this method may be on the surface unnecessary, for mysql at least.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:42 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design