Z
Z
Zturoo2015-01-13 17:29:31
MySQL
Zturoo, 2015-01-13 17:29:31

Java: What's the best way to design a MySQL (JDBC) database class?

It is required to create a DataBankManager class, with the help of which other classes (let's call them Process1, Process2) will save data to the database / read data from there. Moreover, these other processes will work in parallel (in the sense of multithreading). So the question is: what is the best way to implement the DataBankManager? So far, I've come up with a few options:

  1. In the constructor of the DataBankManager class, we connect to the database. This connection (java.sql.Connection) lives as long as the DataBankManager class lives. We pass a reference to our object to the Process1, Process2 classes, and they save / read data using class methods
    public void saveSomeData(Data data) {
        PreparedStatement pstmt = connection.prepareStatement("INSERT ...");
        pstmt.executeBatch();
        pstmt.close();
    }

  2. The same, but only the Connection is created at the beginning of the saveSomeData method and closed at the end of its execution
  3. The connection to the database is opened and closed by static methods of the DataBankManager class, saving data is also done by static methods. Then there is no need to create an object of the DataBankManager class at all.
  4. Or maybe use the singleton design pattern?

How would be more correct? How is it usually implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai, 2015-01-13
@j_wayne

Conenction Pool
habrahabr.ru/post/101342

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question