V
V
Vladimir2020-02-26 23:32:40
MySQL
Vladimir, 2020-02-26 23:32:40

Is the application making queries to the database correctly?

Hello!
There is a java program that connects to a MySQL database via JDBC.

Creating a connection is hardly any different from 98% of implementations:

private static Connection createConnection() {

      try {
        Class.forName(controllerDB);
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }

      Connection connection = null;

      try {
        connection = DriverManager
        .getConnection(dbURL,db_user, db_password);

      } catch (SQLException e) {
        e.printStackTrace();
      }

      if (connection != null) {
        return connection;
      } else {
        return null;
      }
     }


Next, I have 12 methods that use this connection.
2 of them connect to the database on average 2000-3000 times per minute, the rest - from 900 to 1000 per minute in total.
In fact, this is the first time I have encountered such loads, maybe this is nothing at all and I can not worry until there are about 1 million requests per second :)

Actually, an example of use:
Method 1:
public static String[] one() throws SQLException {
     Statement statement;
     ResultSet result;
     Random random = new Random();
     
     int id = ((random.nextInt(max_number) + 1));
     String sId = Integer.toString(id);
     
     Connection conn = createConnection();
     String sql = "SELECT * FROM table WHERE ID=" + sId;
     
     String[] arr = new String[10];
     
     statement = conn.createStatement();
     result = statement.executeQuery(sql);
     
    while (result.next()){
       arr[0] = result.getString("string");
       arr[1] = result.getString("string");
       arr[2] = result.getString("string");
       arr[3] = result.getString("string");
       arr[4] = result.getString("string");
       arr[5] = result.getString("string");
       arr[6] = result.getString("string");
       arr[7] = result.getString("string");
       arr[8] = result.getString("string");
       arr[9] = result.getString("string");
       
     }
     statement.close();
     conn.close();
     
     return arr;
   }


Method 2:
static void two(int num1, String str1, String str2) throws SQLException {
     
     String insertTableSQL = "INSERT ... " +
                    "VALUES (?, ?, ?)"
                    + "ON DUPLICATE KEY UPDATE" 
                    + ".. .";
     Connection conn = createConnection();
     PreparedStatement insert = conn.prepareStatement(insertTableSQL);
     
     insert.setString(1, str1);
     insert.setInt(2, num1);
     insert.setString(3, str2);
     
     try {
          insert.executeUpdate();
        } catch (SQLException e) {
          e.printStackTrace();
        }
     
     insert.close();
     conn.close();
   }


Those. every time users interact, the application creates a connection, uses it, closes it, then creates, uses it, closes it, and so many times a minute :)

Is this a normal use or can this moment be optimized somehow?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-02-27
@Vlad_Nest

Use connection pools.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question