I have hibernate all over several apps (great tool!) and I wanted to use it to grab a connection for a single admin type servlet to look at metadata on a DB during development. So I wrote a line like this...
Code:
Connection connection = HibernateFactory.getInstance().getFactory().openSession().connection();
On my test app, it was great, but then with more data I got..
Code:
WARNING: unclosed connection
and eventually at a random point later
Code:
No operations allowed after connection closed
The answer? Garbage Collection!
In the code above, the session is not kept as a variable, thus eventually gets garbage collected at a random point later. At that point the connection is closed and everything goes bad.... The answer is easy
Code:
Session session = HibernateFactory.getInstance().getFactory().openSession();
Connection connection = session.connection();
Then all is good.... So even on a simple admin tool, I must be discplined!