Why should we close the connection in JDBC? If we don't, what will happen

Communicate with a database in java, we often follow these steps:

  • download driver
  • get connection
  • create an expression or PreparedStatement

  • receive ResultSet

  • close connection

I'm confused that we have to close the connection, everyone says it is expensive to create a connection, so why can't we do this:

static
    {
        try
        {
            connection = DriverManager.getConnection(connectorURL,
                    user, password);
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

      

We just create the connection as a singleton and use it everywhere. Is not it? If I use it like this, what happens?

And if I don't close the connection, what happens?

In addition, we will use a connection pool, it will create some connections in the pool and we will get a connection from the pool, the connection in the pool also does not close, why if we are not using the pool, we need to follow the steps and close the connection if we are not are we using?

It's so confusing and I don't know what the principle is. Please help me. Thank.

+3


source to share


2 answers


If we don't close the connection, it will leak memory. If / until the application server / web server is disconnected, the connection will be activated even if the user is logged out.

There are additional reasons. Suppose the database server has 10 connections and 10 clients request a connection. If the database allows all of them and they are not closed after using them, the database server will not be able to provide connections for another request. For this reason, we need to close them - this is a must.



Moreover, it can lead to some mischievous actions regarding the integrity of the database.

+5


source


We just create the connection as a single one and use it everywhere. Is not it? If I use it like this, what happens?

In this case, you will only have one database_connection object. If the database query has a longer execution time, then other queries for that connection object must wait. So this is not the recommended approach.

And if I don't close the connection, what happens?When the connection is closed, the Statement and ResultSet objects will be closed automatically. The close () method is used to close the connection. If you forget to do this, it will leak memory in your application. For example: if your application has 10 database connections and 10 users are active at the same time. Later, 3 users logged out of the application, but since you did not implement the mechanism for closing the connection here, as a result these 3 connections will remain active. Your app will not present a new connection to a new user. So there is no increase. open connections to the database server will slow down the application. Therefore, it immediately releases the Connection object's database and JDBC resources, rather than waiting for them to be released automatically.

Also, we will use a connection pool, it will create some connections in the pool and we will get a connection from the pool, the connection in the pool also does not close, why, if we are not using the pool, we need to follow the steps and close the connection, if we are not using ? Connection pooling means that connections are reused rather than created every time a connection is requested.



"If the system provides a pool of connections, the search returns a connection from the pool if available. If the system does not provide a pool of connections, or if there are no connections available in the pool, the search creates a new connection. The application benefits from reusing the connection without any code changes. the connections in use from the pool behave the same as the newly created physical connections. The application establishes a connection to the database and access to the data works as usual. When the application is finished with the connection, the application explicitly closes the connection

A connection pool close event signals the pooling module to put the connection back into the connection pool for later reuse. "

_source: https://www.progress.com/tutorials/jdbc/jdbc-jdbc-connection-pooling

Your application borrows a connection from the pool, uses it, and then returns it to the pool, closing it. Connecting in a free pool for an extended period of time is not considered a problem.

0


source







All Articles