2

I'm getting this exception when trying to create SQL query. I'm trying use session in thread's run() method.

Hibernate exception org.hibernate.HibernateException: Not able to obtain connection
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:113)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:88)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1162)
at org.hibernate.loader.Loader.doQuery(Loader.java:390)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.doList(Loader.java:1593)
at org.hibernate.loader.Loader.list(Loader.java:1577)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:112)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1414)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:153)
  • 3
    How do you know it's still in memory? – Sotirios Delimanolis Dec 06 '13 at 15:10
  • 4
    Assuming it is still in memory, did you nullify or remove all references to the object? – Pandacoder Dec 06 '13 at 15:11
  • Because i'm fallowing memory performance in Task Manager – Karen Simonyan Dec 06 '13 at 15:12
  • 2
    In addition to the the very good answers you already got, note that `System.gc()` is does not invoke GC. It's a hint to the VM that this would be a good time to collect garbage, but it guarantees neither that GC runs at that point nor that it won't run at some other point. – kviiri Dec 06 '13 at 15:16
  • I wouldn't say Task Manager would be an accurate view to see if your object is still in memory - run your application through a debugger to track the lifetime of it. – Fallso Dec 06 '13 at 15:19

4 Answers4

5

Manual memory management is not available in Java. The object will automatically become eligible to be recycled when it goes out of scope. What is holding onto your object? There must be a class, or another object referencing it for it not to be recycled by the GC.

Fallso
  • 1,231
  • 1
  • 9
  • 17
  • 2
    To be exact the object *might* get recycled, but there is no guarantee that it will. – Nicola Musatti Dec 06 '13 at 15:14
  • 1
    True - perhaps I should say it automatically becomes eligible for garbage collection... – Fallso Dec 06 '13 at 15:17
  • also considere dereferencing, it can tremendouslly increase the speed of the garbage collector and it's a general good pratice. – Kiwy Dec 06 '13 at 15:17
1

A core concept of Java is that you have no control (which also means that you should not care) when Objects are removed from the RAM. Generally, the Garbace-Collector will pick up an Object that is no longer referenced to by anything. WHen exactly this happens is not exactly predictable though.

So in short: You can not do that. Objects that stay in the RAM though are still referenced by something.

LionC
  • 3,068
  • 1
  • 21
  • 31
  • 1
    It would be nice if the downvoter leaves a comment to give me a chance to improve myself in the future :-) – LionC Dec 06 '13 at 15:18
0

I would refer to this article: When does System.gc() do anything

You can actually try and call the Garbage Collector but Java is a stubborn animal and will only actually collect garbage when it feels the garbage needs to be collected.

Community
  • 1
  • 1
DanK
  • 2,282
  • 5
  • 16
  • 35
-1

In Java, an object will be shortlisted for garbage collection when is no longer possible to be reached from any active reference.

You can facilitate this by assigning its reference to null:

obectRef = null;

But, this is no guarantee that it will actually be deleted.

james_dean
  • 1,377
  • 3
  • 24
  • 34