1

I have a php page with several databases. I keep reading that multiple databases is bad and you should combine them all into one, but I want to keep things separate for better organization.

So let's say I stay with multiple databases. Is that bad? Now I'm seeing problem with mysql_max_connections. So I'm looking into singleton methods for getting connections. But the singleton classes seem to be only for one database connection. Is that the case? Do I need to create a separate singleton class for each database?

Clarification: I anticipate the databases getting fairly large. I already have several hundred tables on one database and more are added with new content. I have heard that you don't want more then several thousand tables in any particular database. Maybe the best answer is to get everything combined somehow, but it would take a major overhaul so I would like to stick with multiple databases. I just want to know what is the most efficient way to organize connections for multiple databases.

1
  • 1
    clarify your question. Also if you are 'organizing' your databases this way, you really need to re-think the approach. You would use multiple databases only in cases where you CANNOT do without combining them. Otherwise it creates complexity and wasted resources and inefficiencies (cannot do efficient joins, etc;) Commented Apr 10, 2011 at 22:06

3 Answers 3

1

Typical singleton looks like:

class Singleton{
    private static $instance;
    private function __construct(){}
    private function __clone(){}
    public function Instance(){
        if(!self::$instance){
            self::$instance = new self;
        }
        return self::$instance;
    }
}

But you can extend it by having multiple instances but slightly different from each other:

class MultiSingleton{
    private static $instance = array();
    private function __construct(){}
    private function __clone(){}
    public function Instance($index = 'default'){
        if(!isset(self::$instance[$index])){
            self::$instance[$index] = new MultiSingleton($index);
        }
        return self::$instance[$index];
    }
}

That way you can have one class to rule them all ;) and still follow singleton pattern

Sign up to request clarification or add additional context in comments.

1 Comment

This is a great answer to my original question! I'll have to save this technique in case I need it. However, I found that I wasn't going about things the correct way. Thanks for the answer though.
0

using multiple databases is fairly easy. mysql_connect() returns a Resource value, which Identifies the connection. You can add that value to, for example, mysql_query():

$db1 = mysql_connect("localhost");    
$db2 = mysql_connect("google.com");

$query1 = mysql_query("select ....", $db1);
$query2 = mysql_query("select ....", $db2);

Comments

0

I'm not sure I understood the question, what you mean for "so I'm looking into singleton methods for getting connections", I know what a singleton is but I really don't see what you are planning to do. More, I don't see how mysql_max_connections is related to all of this, it has quite nothing to do with what you ask.

I may suggest you to create a common interface to all the databases, like a wrapping class named "DB" (as a singleton, if you want), and define some methods to query the databases so that the callee doesn't have to "know" what DB to get the data from.

But all of this seems pointless...

EDIT: (see comments, also)

1) To significantly reduce the number of connections, use mysql_pconnect(). But read the docs, carefully, I know there are some concerns.

2) Generic code. It'll take a bunch of work, but it's worth. I do this way:

  • I create a query repository, with a bunch of queries everyone named with a sensitive caption and some SQL code like "SELECT foo, bar FROM foobar WHERE bar = '?'
  • I create a DB class with a function named query (or something like that), that accepts the query name, the query parameters as an array, and transparently builds the acual query string, executing it on the appropriate database for that query type!
  • The function returns some handle to an object that can iterate through the query results, pretty like mysql_fetch_array, but better.

If in the future you'll drop mysql for anything else, it'll work without changing callee code. The same if you merge the two databases.

EDIT (2): uh oh, it seems I've finally understood what you ask. Your databases (or better, schemata) are more than one, but the DBMS is just a single instance of MySQL running on localhost? If it's like this you can simply edit your queries. "SELECT * FROM database.table WHERE ..."

6 Comments

I have all my databases on localhost. I have too many connections open for each user and so sometimes it shows a max_connections error. I'm going to use singleton so I make sure I'm not creating new connections for the same database. Why do you say "all this seems pointless"? Can you give a pseudo-code example of a method to query databases so the callee doesn't know what DB to get data from?
Now it makes sense to me. OK, these are two separate questions. The first is how can you reduce the number of connections to the database, and this has nothing to do with a 'singleton', since PHP objects, as far as I know, usually don't keep on living after the script has been executed. You can use mysql_pconnect() to significantly reduce the no. of open connections to your server. Try it out and read the 'concerns' in the docs.
As for the second question (generic coding) I'll edit my answer
Your EDIT(2) is starting to draw the fog away from my understanding. :) Yes, these are all on localhost so I should be saying "schemata" instead of databases so as not to confuse. It's just that in phpMyAdmin they're all listed as separate databases. But you're right they're all the same connection info except for the name, so it makes sense to just specify the schema name on the query. Then why does the mysqli_connect need to specify the schema name? I mean I could add any one of my schemata and it's the same connection right?
Please note it's an optional parameter, and it's here because one's php script usually operate on a single schema at a time. Don't care about it, just connect an play with all the schemata you want to. And again, use mysql_pconnect to reduce the amount of connections to the database (read the docs before!). Finally, review your design to see if this multi-schema approach is really necessary.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.