简体   繁体   中英

Ensure that a user (or a database was read) is used only by one application

Imagine that I have 100 users list in the database and I have 100 or more console applications that are using those users to log in somewhere. I need to use a unique user per application, I must not use the same user more than one application. How can I solve this issue, what is the best practice or pattern for that? That means a record has to read only one application/service I have also some requirements/restrictions as I mentioned below.

  1. Each application/service an instance of a microservice. So, it can be scale-down or scale-up. It means I cannot set a user to a specific application/service in a config file.
  2. Users in the database can be disabled or removed in the future or added new users. So user count is not fixed.

You didn't say what database you use or what database access technology stack. In general, you need the following:

  • a field in the database that signals whether a row is in use
  • each application:
    • locks the table
    • grabs a row that is not in use
    • updates the in-use flag to "currently used"
    • releases the lock on the table
    • uses this user in the program
    • makes sure that it releases the in-use flag when it's done

Any decent database and any decent driver should be able to do that.

If I don't misunderstand you, you need a class that handles 1:1 relationships between two classes. Maybe you can find an answer here?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM