Is it possible to determine (through a query or some other means) if someone is already using a specific MySQL database before a new connection attempt to it is made? I have a database where I would like to restrict the number of users to one at a time. (even one instance of each user, not necessarily two different users at once)
My reason for this is to prevent any read/write conflicts between multiple instances of a program that works with this database. Due to the way the program works, if changes are made in Instance A they would not immediately show up in Instance B until the data gets refreshed. Right now there is no way to sync these in real time. It would just be easier to prevent Instance B from starting until Instance A is finished) In my situation, both the client app and database run on localhost but it is possible for the database to be remote.
Can this be done? If so, is there an easy way to do it through Qt?
As of MySQL 5.0.3, you can limit the number of simultaneous connections to the server by an account. You can then detect if the account is in use because additional connection attempts will be refused by the server (either with error 1203
ER_TOO_MANY_USER_CONNECTIONSor error 1226ER_USER_LIMIT_REACHED).More about resource limits:
To set resource limits for an account, use a
GRANTstatement. Provide aWITHclause that names each resource to be limited.MAX_USER_CONNECTIONSis an integer representing the maximum number of simultaneous connections by the account.To modify or remove limits for an account, use a
GRANT USAGEstatement at the global level (GRANT USAGE ON *.* TO 'usera'@'localhost' WITH ...). This modifies only the limit value(s) specified and leaves the account otherwise unchanged.An “account” corresponds to a single row in the
mysql.usertable.'usera'@'localhost'and'usera'@'%.example.com'are distinct accounts.