Java – why call close() on resultset and connection instances?

When I no longer need to use instances of resultset and connection in my program, why call on these two methods What about the close () method?

What is the danger (if any) of not doing so?

Solution

Here are two questions:

Database connection

Opening the database connection will consume resources on the database; It uses memory and database configuration to have the maximum number of connections, so you may increase the possibility of connections In addition, the status of the meeting is maintained, so you may encounter trouble because it is unexpectedly out of range

On the positive side, the prepared statements remain compiled and ready for use, so if you write and use SQL correctly, you can gain significant performance advantages by reusing the prepared statements However, doing so may complicate your code, so it is recommended to be cautious

In addition, obtaining connections is quite expensive, so this is why connection pools exist These disconnect the connections, but the client gets the connections, uses them, and then releases them back to the pool when complete

Result set

If you don't commit (close the result set), keeping the result set open will also keep some locks, so depending on your application, you can quickly hit deadlocks or serious life problems Whether you open the connection or not, always close your result set as soon as possible and release as many resources back to the database as possible

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>