Hi,
I rewrote the support for multiple dbs in ErlyDB in trunk. For info on
how to use it, refer to erlydb:code_gen/3. Here's a quick tutorial:
First, create a "test2" database and create a 'dog' table in it. Then,
add the following 'dog' module to the music app:
-module(dog).
-erlydb_options([{pool_id, p2}]).
The pool_id option tells ErlyDB that this module belongs to the
connection pool 'p2', not the default pool.
Next, start the shell and enter the following:
%% Start a 10 connection pool to with the default pool id to the
"test" database.
> erlydb:start(mysql, [{hostname, "localhost"}, {username, "root"}, {password,
> "password"}, {database, "test"}, {pool_size, 10}]).
%% Create 3 new connections to the "test2" database. These connections
belong to the pool 'p2'.
> erlydb_mysql:connect(p2, "localhost", undefined, "root", "password", "test2",
> undefined, true, false).
> erlydb_mysql:connect(p2, "localhost", undefined, "root", "password", "test2",
> undefined, true, false).
> erlydb_mysql:connect(p2, "localhost", undefined, "root", "password", "test2",
> undefined, true, false).
%% compile the app, specifying additional pool ids that should be used
during ErlyDB code generation.
> erlyweb:compile("/Users/yariv/apps/music", [{erlydb_driver, {mysql, [],
> [p2]}}, {auto_compile, true}, debug_info]).
Now, you can use both 'dog' and 'musician' in your app. They use
different database connection pools, but it's transparent in your
code. Try
> musician:find().
> dog:find().
It should work as usual. The only obvious restriction is that modules
from different connection pools can't have relations to each other.
You can even assign different modules to different erlydb drivers, but
I haven't tested that feature yet. That will be covered in the next
tutorial :)
I see transparent support for multiple connection pools as one of
ErlyWeb's greatest strengths, which will become more important over
time as servers grow in number of CPUs. In Rails, AFAIK, you can
connect to multiple databases, but every process maintains its own set
of connection. If you run multiple multi-core web servers with
multiple Rails processes on each core, your database will be
overwhelmed with too connections. ErlyWeb's DB drivers use Erlang
concurrency to enable elegant -- and scalable -- connection pooling to
multiple databases. (If you want to see how it works, check out the
source for the erlang-mysql-driver.) With Erlang, you can have tens of
thousands of Yaws processes that share a few dozen db connections.
Also, transactions "just work." This is critical for scalable apps,
especially ones that use sharding (check out
http://highscalability.com).
Cheers,
Yariv
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"erlyweb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---