Skip to main content


Showing posts from July 25, 2016

Best Practices in Hibernate with Java

Session Management with Hibernate

he best practice is in most cases session-per-request. That is, open a session in the beginning of handling a request, and close it in the end. You can do that in a ServletFilter, for example. Having one session for the entire application is bad, because it will accumulate a lot of entities in its 1st level cache, which is a memory leak. It may also produce undeterministic results when multiple clients use it at the same time
Its always better to open a new session for every request, and close the session once the request is processed. Like Session session = HibernateUtil.getSessionFactory().openSession(); instead of Session session = HibernateUtil.getSessionFactory().getCurrentSession(); If we use the getCurrentSession() method , tansaction.commit() / rollback() closes the connection. Avoid many-to-many association
“Many to many” usage is discouraged when a simple bidirectional “many-to-one”/“one-to-many” will do the job. In particular, a many-to-many associa…

Spring Best Pratices

Best Practices For Spring XML Configurations Springis a powerful Java application framework, used in a wide range of Java applications. It provides enterprise services to Plain Old Java Objects (POJOs). Spring uses dependency injection to achieve simplification and increase testability. Spring beans, dependencies, and the services needed by beans are specified in configuration files, which are typically in an XML format. The XML configuration files, however, are verbose and unwieldy. They can become hard to read and manage when you are working on a large project where many Spring beans are defined.
Some of best practices for Spring XML configurations can be followed are  as below. Some of them are morenecessarypractices than best practices.
1. Avoid using autowiringSpring can autowire dependencies through introspection of the bean classes so that you do not have to explicitly specify the bean properties or constructor arguments. Bean properties can be autowired either by property names …