avo has asked for the wisdom of the Perl Monks concerning the following question:

Good day monks,

I am working on a server/client program for transporting information between two mysql servers... (sort of replication). I've been wandering (and searching arround for samples) how to design the server part, because each of my clients works with different SQL database (on the server side). For example - Connection from Client 1 requires the POE server to work with client_1 database. At the same time - Simultenously Client 2 must work with client_2 database. I.e the server must have two DBI connections ...

I am currently executing DBI->connect and storing this per heap... but I am sure there is a better way to do asynchronous DBI ... for multiple DBs. Thanks a lot for your toughts.

Replies are listed 'Best First'.
Re: POE and DBI
by CountZero (Bishop) on Sep 11, 2005 at 14:37 UTC
    Ima::DBI allows easy encapsulation of all your connections on a database-to-database basis, each in its own object.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: POE and DBI
by rcaputo (Chaplain) on Sep 11, 2005 at 19:48 UTC
Re: POE and DBI
by pg (Canon) on Sep 11, 2005 at 15:56 UTC

    As all connections are created at the server side, the number of simultenous connections at any given time to a database could be well controled. In this case, you probably want a connection pool for each database on the server side.

    The connections can be kept open all the time (for example created at server start up time) for your first version. In later versions you can try to have better control over size of connections base on demand (even in this case, connections are not created per request, but rather base on the load of the application.)

      I would like to go for the on demand type... do you think that storing DB handlers in a hash for each client and then disconnecting the DBI upon client disconnect is a good start ?
Re: POE and DBI
by Ultra (Hermit) on Sep 12, 2005 at 06:05 UTC

    I don't think storing DBI objects on the heap is quite POEish, because I guess you application freezes until a DBI transaction finishes. The more DBI connections the more chances to freeze ;-).

    I would rather go with one of the four components Rocco told you already exist.

    Dodge This!
      At present my script is forking a child and doing DBI->connect for each child... and upon disconnect the child get's killed and DBI->dissconnects. I would like to keep the same scenario... which is not a mission having in mind the existing async DBI components for POE... I just don't have any experience with POE and DBI... Perhaps if someone can give me a link or a short example will help a lot, so I don't need to reinvent the wheel. BTW I read everything for both four modules, but I cant see a way to keep my db handlers... the way they are now ($result = $dbh->prepare()...). Thanks for your great response!

        Here you can find an example. I'm sure you can find others too ;)

        Dodge This!
Re: POE and DBI
by Anonymous Monk on Sep 11, 2005 at 22:43 UTC
    How about connecting to one main database and then in the query you do: SELECT * FROM database_name.table_name WHERE bla bla bla... So you only need one database connection per process...
Re: POE and DBI
by fmerges (Chaplain) on Sep 12, 2005 at 09:28 UTC

    Hi,

    You can easily make the approach using POE::Component::Server::TCP and POE::Component::SimpleDBI.

    Having one tcp server session that listen to incoming connections, then you can use a hash-map to know to which db connect each of the clients. Then you have 2 DBI session, one for each databases, then when a input comes from the socket, you check the remote client to know to which session to forward the DBI call.

    The hash-map is very simple, because you only need a map between remote-ip and dbi_session_name. For this map, I would suggest to create a config file using something like YAML.

    As you see, the DBI connections are shared inside POE, so that you don't need to create more sessions or pools of dbi connections

    Regards,

    |fire| at irc.freenode.net