开发者

MySql's 'CREATE DATABASE IF NOT EXISTS' misbehaves when database has live connections

We have some initialization code that makes sure that all the needed resources are in place,

i.e.: creates those resources (files, directories, databases, tables, etc.) if missing, and does nothing otherwise.

But, when a database is in use (i.e. exists and already has live connections), a new app that goes through the initialization code waits forever.

The reason is t开发者_运维知识库hat 'CREATE DATABASE IF NOT EXISTS' is kinda stupid: it tries to own the database, even though it exists and nothing should be done.

Is there a way to check whether a database already exists, that does not involve such a lock?


SELECT EXISTS(
    SELECT 1
    FROM INFORMATION_SCHEMA.SCHEMATA
    WHERE SCHEMA_NAME = 'MyDatabaseName'
) AS e

Returns 1 if database exists (and the user executing the query has the appropriate privileges to be able to see it, blah blah blah); 0 otherwise


Just assume that it does. If it doesn't, your system is horribly broken. It will be caught by SQL errors when you try to execute queries.


Are you sure? Have you a reproducible-bug test-case? If so, please report it as as a bug.

I certainly see no such behaviour as described.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜