You need to be wary when the resources used are not Java resources. A database Connection is owned and managed by the database, the Class just gives you access to that database resource. If you don't close the connection then the Java class can be garbage collected, but the Database may not be able to tell that the connection is no longer in use, which may result in database resources being wasted or even leak.
Other places to be careful are File and IO operations, and also when using Graphics contexts as these are system resources.
Joined: Aug 25, 2009
Thanks for your reply
so if connection is not closed it will remain open for a specified time or forever
and it will consume our heap space also... M i right..??
as i m new to java..
sorry if i asked foolish one..??
There is no easy answer for that. For databases it will be up to each engine, for system resources I have no idea, but consider in both cases that if you are performing operations very quickly (eg double buffered graphics or a busy web site) it would probably be easy to use up all of the resources before they became available again.
Yes, because the exact effect is not always given, you will have to assume:
- If I don't return a connection, I will occupy that resource for a very long time, a time not manageable or predictable by me.