Question

I have lot of separate functions and I'm bound to do a session.clear() upon transaction.commit() in each of them to kind of release memory.

I might be wrong but I think, session.clear() in hibernate is not called by default upon transaction.commit().

I would like to know if there is any way I could probably configure hibernate to do a auto session clear upon each transaction.commit(), without ever doing a session.clear()?

Any other solution for the above problem would be highly appreciated.

Was it helpful?

Solution

It's called session-per-transaction pattern: you create a session when you begin transaction, and close it after commit (note that cleaning a session and committing a transaction has almost the same effect as closing a session, especially if you use connection pool).

If you use Hibernate in standalone environment, you need to configure contextual sessions with hibernate.current_session_context_class = thread. In this case contextual session (obtained by getCurrentSession()) will be closed automaticlly upon transaction commit.

If you use EJB or Spring Framework, you can use transaction management facilities provided by these frameworks. They support this pattern as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top