jhthorsen / mojo-mysql

Mojolicious and Async MySQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Document what happens when a database isn't specified

srchulo opened this issue · comments

The documentation doesn't explain what happens when no specific database is declared and a connection is made. I'm doing a join across two databases, so I didn't specify. Some things that are not clear to me from this:

  • Will connections still be cached? Will I get one connection per each database I ever query, kicking out connections for a previously used database?

  • Would it be more efficient to use a single Mojo::mysql instance specifically for each database for the queries that don't need to access two or more databases?

I have no idea. Want to contribute? Are you sure this isn’t a DBD::mysql question?

I'd be happy to contribute...but I don't know the answer either :) I guess it might be a DBD::mysql question, but I thought you might have more insight for what Mojo::mysql is doing under the hood when it caches connections. I guess I don't really know if there's a difference when declaring a db to start in the dsn string vs declaring no db. I don't know if it creates new connections for each database you connect to-- which would defeat the caching, or if can use one connection to connect to different databases on the same MySQL instance.

I don't see a usecase where I wouldn't specify a database. In case this is very confusing, I might simply change the API to requiring a database, or setting the database to "test" or something.

The reason I'm using it is because I need to join across to different databases. Although I agree it isn't normal, I think it is a valid use case.

Why can't you connect to one of the databases?

Thanks for the suggestion-- that worked! I didn't know you could connect to one database when you were going to then join with another one. I'll close this now.

@srchulo: I just made the database name optional in 1.08.

e8e65ca

Awesome! Thanks for letting me know :) Did you happen to figure out the answers to the questions above?

  • Will connections still be cached? Will I get one connection per each database I ever query, kicking out connections for a previously used database?

  • Would it be more efficient to use a single Mojo::mysql instance specifically for each database for the queries that don't need to access two or more databases?

Yes, connections are still cached. So you either have to make sure you change the database each time you get the $db object or not change the database at all.

And yes: Please do specify a database when creating a Mojo::mysql object or make sure all your queries have FROM database.table in them. I think the latter is kind of scary, since it's so easy to make a query without it and get confusing results.

Whatever you do, I would discourage changing databases with use cool_database, since there's no way to know which cached $db object you get.

commented

FWIW: You can use the connection event for setup that needs to happen for any new connection that will be made by the object, such as 'use foo'. But I'm not sure why you'd want to do that and not just specify a database to connect to.

Yeah, I'll just be specifying a database. But this is all good to know. Thanks!