This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Comment

SessionFactory.getCurrentSession() can indeed be used to code DAOs against the plain Hibernate3 API. The main advantage of this is that you only rely on standard Hibernate API and don't have Spring dependencies in your DAOs. The main disadvantage is that such DAOs will throw plain HibernateException, not Spring's DataAccessException - which doesn't allow callers to handle exceptions in a generic manner. If the latter is important to you, use Spring's HibernateTemplate - which we generally recommend. See the ORM chapter in our reference manual for details.

Note that such a getCurrentSession() call does not open a Session for each call: It just accesses the current transactional Session, whose lifecycle is managed by the current transaction. Hence, you don't need to close it: just get a reference to it and access it, leaving the lifecycle to the transaction manager (either Spring's HibernateTransactionManager or JTA).