A database connection, a stream, a file descriptor, a socket are all finite resources.
The operating system you are running on allows so many threads - 1 MB of overhead per thread. You are limited by available RAM. The same goes for file descriptors and sockets.
Database connections are interesting because they are connected to the client and server. If the gc client is connected, what tells the server to close the connection? If you fail to complete the lock at the end of the blocks, you will soon find out that connections with heavy load will work on your database server.
Finishing is not the right way. Do not depend on the virtual machine to call it. Write a close() method and call it in the finally block when your method executes with the resource. Close in the narrowest volume.
duffymo
source share