0

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.
leo
  • 445
  • 8
  • 25
  • Why have the list? You could separate it out into a file per app, then the apps can't "accidentally" use the same user – CoderLee Feb 25 '20 at 13:40
  • Have you considered using multiple threads (one thread per user) instead of multiple applications? Synchronizing applications is more difficult that synchronizing threads. – Theodor Zoulias Feb 25 '20 at 13:41
  • @CoderLee thanks for your reply, I updated my question, I think you can find your question's answer there now. – leo Feb 25 '20 at 13:56
  • @TheodorZoulias thanks for your reply, I am using a kind of microservice architecture and I have updated my question. Please let me know if it is not clear still. – leo Feb 25 '20 at 13:58
  • What is the question 1) Allow only one user access the database. Make sure if a 2nd application by same user is locked out. Which is what the title says. 2) Have a separate log file for each user/application. Which is what the text says in your request. – jdweng Feb 25 '20 at 13:59
  • @leo What is your goal here? I don't understand why you want to do this. Some more clarity will help to give you better answers – CoderLee Feb 25 '20 at 14:03

2 Answers2

1

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.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
-1

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?

Nimetski
  • 13
  • 4