T
T
TopMetaFizick_0102019-03-02 00:21:28
JDBC
TopMetaFizick_010, 2019-03-02 00:21:28

How to access the same instance of the control database class from different threads?

The bottom line is, I have a class (DBmanager) that works with the database (Adds, deletes, changes rows, checks for their presence, etc.). There is a second class, which is a server (ServerChat),
which, with each new connected user, creates a new thread to work with it. But here we have one DBmanager object, and it is created when the ServerChat class is created. Those. at any time, a call from different threads to an object of the DBmanager class can turn out ... How can I get around this, or am I misunderstanding something?

public class ServerChat {

    private DBManager dbManager;

    public ServerChat() {
        dbManager = new DBManager();
    }



 public void start(int port) {

        try (ServerSocket connectionListener = new ServerSocket(port)) {
            while (true) {
                Здесь слушается подключение и создается новый поток на каждого подключенного.
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the main method, an instance of the ServerChat class is created and the start method is called.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question