Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do you close the database (RAF) when exiting?

 
Elena Taube
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

do you close the database (RAF) when exiting the application?
Do we need to make it?

Thanks a lot for your comments!
 
Ali Hussain
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Elena Taube:
Hi all,

do you close the database (RAF) when exiting the application?
Do we need to make it?

Thanks a lot for your comments!

No I do not. I was considering to do this in finalize method but decided not to do so. More code == More bugs.
 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Elena and Ali,

I think it's always a good idea to close handles to the datasources.

 
Martin Sturzenhecker
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ali Hussain:

No I do not. I was considering to do this in finalize method but decided not to do so. More code == More bugs.


How many bugs may be coded into


?

cheers
martin
 
Ali Hussain
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


?

cheers
martin[/qb]<hr></blockquote>
Well IMHO every single line of code has the potential to introduce bugs. So my rule is: if a line of code is not required/used in my program, I do not write it Since I was talking about the close statement in the finalize method, what if I misspelled finalize (finalise) in my code. Additionally remember the exception handling as well, so now its more than one line.

Ok jokes aside. If someone can convince me that close() is not redundant and tell when and where to close it, I might consider it myself

Anyway check this thread (second last post by Nicky Bodentien - I'm 99% sure it's redundant because Sun already put it in FileChannelImpl.finalize() for us (and RandomAccessFile uses FileChannelImpl)):
http://www.coderanch.com/t/185459/java-developer-SCJD/certification/When-close-RandomAccessFile


[ December 28, 2005: Message edited by: Ali Hussain ]
[ December 28, 2005: Message edited by: Ali Hussain ]
 
HaoZhe Xu
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should consider the meaning of 'exiting', if your client side exits, it does not and cannot close the RAF, so only the actual program who opens it need to, I'm not sure what will happen if you close your program without close an opened file, do not take the risk, just close it and catch exception!

you also have to consider, for example, if you use something like RMI, provinde each client an instance of a class that can manipulate the database, if each client gets different instances, and all instances shares the same RAF, in this case, you cannot close the RAF when some clients exit (even if all clients exit), you'd better close it when Server exits, but need to carefully design your program so that clients that are still connecting won't get an ugly exception ;-)

forgive my poor English
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic