views:

38

answers:

1

In a web app I've created in zend framework, I'm creating a new database for every new client that registers. This query of course is rather heavy and time consuming since we need to create a database, create some 10 tables and put some data in the tables.

We are using one large SQL file that we read in and exec().

After those queries, with another mysql connection in ZF, I need to insert a new record in one of the tables that have been created in the previous step.

This is where it fails: when the first query isn't finished yet and I try to insert data in on of the tables that are being created, I get an error "table xxxx doesn't exist".

All this happens in a fraction of a second, but I cannot find a way to "wait" for the first large query to have finished.

putting a sleep(2) command before the second statement solved this problem, but that's not really the way we want to play.

Also, we cannot use transaction since we are using CREATE DATABASE and other statements that cannot be used with transactions.

Thanks a lot for your advice! I'm curious how you guys solve this.

+2  A: 

One database per client is a can of worms that you almost certainly want to eliminate before it gets out of hand. Use a single database with a client_id field in each table to indicate which client the record belongs to. (This implies the addition of a new table to hold the client records themselves.)

Alex Howansky
we've thought this over and we need separate databases for our clients not only as an extra layer of data security but also to make sure we can restore backups easily on a per-user basis.
Jorre
I will second this comment, new tables per client sounds like a recipe for disaster. That being said, sleep(2) is probably your best bet to be honest.
Matt Wheeler
can you clarify why a new database per client is a recipe for disaster?
Jorre
off topic, but check out the stackoverflow podcast where FogBugz is also using a seperate database per client: [50:45] on https://stackoverflow.fogbugz.com/default.asp?W24218
Jorre
@Jorre Clearly, it depends on the scenario. Working within the comment size limit here, I can say: Performing basic maintenance is significantly more complicated, as evidenced by the fact that the OP is already running into problems in this area. Instead of adding a client by merely inserting a row, you have to create a new db, create tables, create indexes, assign permissions, etc. In the future, any tiny little change that you need to make to your schema becomes multiplied by the number of databases you have. You also lose the ability to easily aggregate data across customers.
Alex Howansky