Few questions on JDBC coding:
Connection
at the beginning and keep it alive without close it until application exit? Why?PreparedStatement
is associated with Connection
, if my connection is not closed after each query, why not keep the PreparedStatement
alive and reuse it in other methods?PreparedStatement
each query, does the database knows it is the same PreparedStatement
and ignore unnecessary actions after the first time?PreparedStatement
is not create once and reuse many times statement? If yes, why need to close it each time?I know the call to close()
will release the resource. But If we know we are going to use it later, why release it and then request it again later?
How about a multi-client application? We need a connection pool and so we need to create and close Connection, Statement
, and PreparedStatement
each time?
You should explicitly close your Statement and PreparedStatement objects to be sure. ResultSet objects might also be an issue, but as they are guaranteed to be closed when the corresponding Statement/PreparedStatement object is closed, you can usually disregard it.
Closing PreparedStatement ObjectIf you close the Connection object first, it will close the PreparedStatement object as well. However, you should always explicitly close the PreparedStatement object to ensure proper cleanup.
You should explicitly close Statements , ResultSets , and Connections when you no longer need them, unless you declare them in a try -with-resources statement (available in JDK 7 and after). Connections to Derby are resources external to an application, and the garbage collector will not close them automatically.
JDBC Statement objects must always be closed by the application instead of allowing them to be closed by garbage collection.
Personally I'd use a pool as this will take care of all of the resource management for you. If your connection requirements change then you can easily modify the pool configuration. With a pool in place you can open/close connections and prepared statements according to best-practice and leave the resource management to the pool.
Typically, when using a pool:
Furthermore - depending on the pool implementation - it may be able to notify you when there are resource leaks making it easier to identify these sorts of problems in your code.
Take a look at the source of an example implementation like DBCP - it's quite interesting to see how they work.
1. Even if you have a single client, a connection pool may still be beneficial. Connecting to the database may take a significant time so doing it very often may slow down your application with slow network requests. Moreover as @teabot explains, a pool may help identifying if any connection is not being closed.
2. It is not a good idea to open a connection and leave it open forever for two reasons. First the connection may die if there is a temporary network interruption. The longer it is open the more likely that it is dead when required. Second, a failed transaction may leave the connection in a state not suitable for continuing operation. The best is usually to open a few connections, reuse them for five or ten minutes, then recycle them.
3. Depending on the database and the driver, the connection may have a prepared statement cache. Even if using a different connection, the RDBMS usually caches statements that are exactly the same including it parameters. Therefore SELECT * FROM table WHERE value=? as a prepared statement will be cached across connections, but if you specify the parameter value like SELECT * FROM table WHERE value='your_data' then probably it won't be cached server side.
4. As explained in 3, depends on the RDBMS implementation, do a benchmark.
5. There is no need to close and prepare again a statement which is going to be reused with different parameters. Just set again the parameters and execute.
For multiple clients, the database will always have a concurrent connection limit which is not usually any big number. If all the clients go through a webapp then a pool like DBCP is all right. But obviously it's undesirable to create a pool for each client with several connections open permanently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With