Good. Ideally, I took a slightly different approach and gave a different solution.
Instead of opening and closing the database, which literally happens for every client request, I made a singleton instance of the class and made one open connection, which will be reused until the server process is alive.
In short:
I have getConnection() inside a Database class
public Connection getConnection() throws Exception { try { String connectionURL = "jdbc:mysql://localhost:3306/someDatabase"; Class.forName("com.mysql.jdbc.Driver").newInstance(); if (connection == null) { connection = DriverManager.getConnection(connectionURL, "someUser", LOCAL_MYSQL_PASSWORD); } return connection; } catch (Exception e) { } }
The Database class is single-point, so reusing the same class and reusing the same connection.
I tested this with show processList and it gave about 100 connections. If I donβt close the connection, and even if I close the connection, the actual process is unlikely to be killed, but instead it will be used when the same client requests the same request, but if you have 15 requests, each of them will have separate processes, so closing and opening the connection for me was not the best solution.
In doing so, I have one single process, which is the responsible layer for requests.
Android Developer
source share