• Post Reply Bookmark Topic Watch Topic
  • New Topic

Closing any type of "connection" - DB connection, File connectors etc ?  RSS feed

 
David Blaine
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we have to close them ? What are the problems we will face if we don't ?
I know that in case of DB's we could reach the max connection limit set by the DB.
But, why close the file connectors ?

Are there any other types of connectors, besides file and DB ?

 
Mauro Trevigno
Ranch Hand
Posts: 99
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

I found the following:
http://www.coderanch.com/t/301364/JDBC/databases/close-DB-connection


If we don't close a DB connection, we get something called a "connection leak." This drains resources from the system. If you are using a connection pool, it can also prevent you from obtaining a connection later on.


Will decrease your performance.

-Mauro
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

What do you mean by closing 'file connectors'?

In Java, we deal with file with the help of input and output streams. It is good practice to close all the streams to the file after desired operation is complete.

The reason being:
1) It flushes the data to the file - so that if other process reads the file, it will have latest data.
2) It detaches current JVM process from the file - so that if other process (say process x) attempts to check if the file is already being handled by some process, then the process x will get 'all clear' (that is, the file is not being handled by other process). This is very useful in case of critical files (e.g. OS configuration file etc.)

I hope this helps.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:

2) It detaches current JVM process from the file - so that if other process (say process x) attempts to check if the file is already being handled by some process, then the process x will get 'all clear' (that is, the file is not being handled by other process).



A sidenote: some platforms will let you open a file which is held open by another process. But anyway, it is good the release the resources not needed any longer.

The OS might have different (per-process and global) limits on the number of the open file descriptors. To support an open file the OS needs to reserve memory etc.

It is better to close open files, sockets, pipes. etc .(resources maintained by the OS) if not needed any longer.
 
David Blaine
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all.

@Anayonkar Shivalkar - I think this could be an example of what you said - If your java code is reading/using an excel file already, then a person should be able to open it, but NOT edit it in excel until the java code "gives up the lock" on that file. Makes sense ?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David - yes, that's a good example. These things behave differently on different operating systems and for different programs, but that sort of thing is not uncommon.

Another issue is that your operating system may get unhappy if you have too many open files at once - it's a drain on system resources, and there may be a limit set on how many you can have at once. Usually this number is fairly high (in the thousands) but if you're doing something that opens a LOT of files, it can be helpful to close each one promptly when you're done with it.

In general, for many of the times you use a resource and forget to close it, there may not be any negative consequence. At least, not right away. But then something will happen later on, and it may be hard to debug what is going on, and eventually it turns out that it's because you didn't close that thing you were supposed to close. And so it's much easier to just get in the habit of closing things when you're done with them, always. So you can spend your time debugging other errors, instead.

David Blaine wrote:Are there any other types of connectors, besides file and DB ?

Sockets and JMS come to mind. I'm sure there are others. You could look at the java.io.Closeable interface, and see all the classes that implement it.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 7 has a new feature called the try-with-resources statement, which helps you to make it easier to automatically close connections, even in the case of an exception.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!