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.