0

I was wondering why do you have to explicitly close PreparedStatements in Java. When you close the session should they not be closed automatically ?

We recently had a situation at work where apps deployed on bluemix had memory issues and the reason given was that prepared statements were not getting closed. That being said,

  1. Does bluemix not have a feature where it automatically clears memory of last session?
  2. Should all resources tied to an object get released automatically with the session?
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
test dummy
  • 81
  • 4
  • 11
  • You should always close resources that you opened. And yes, in general closing a connection will close the associated statements. However, if you're using a connection pool, that might not always be the case (or at least actual closing might be deferred until (much) later). – Mark Rotteveel Jul 03 '20 at 14:03
  • @MarkRotteveel Then in that case, wouldnt bluemix erase resources itself? There has to be a failsafe somewhere.. No ? – test dummy Jul 03 '20 at 14:06
  • @MarkRotteveel Also 2 things: You said that they will be closed.. just later ? So they are still getting closed. Correct ? And the post you linked to said that the issue only exists with primitive data pooling types? What does that mean? If you establish a connection with hibernate but use java prepared statements. Is that still a primitive pool type ? – test dummy Jul 03 '20 at 14:08
  • It all depends on the exact nature of your connection configuration, libraries used, etc. But in general, you cannot simply detect that resources are unused and can be 'erased', especially not if they are still strongly referenced from a connection, otherwise the garbage collection would have taken care of it. – Mark Rotteveel Jul 03 '20 at 14:16

0 Answers0