Thursday, September 20, 2007

Multiple Persistence Units with Spring, JPA, Tomcat - Update

After making the setup I described in the previous blog entry, I faced an error. One of the databases which was the datasource for one of the persistence units defined, was not accessible at that time, and we were getting errors from Spring, causing the @PersistenceContext annotations not to be injected at all.


When the bean called org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
is defined in Spring application context, Spring was trying to set up all persistence units when even only one of them was accessed for an operation - "Login to the system" use case, for example. When one of the databases was not accessible, the SQLException was causing Spring to give up and not load the other persistence units as well. At that time we were using Spring version 2.0.6.

Going to Spring support forums for a solution to this problem, I couldn't find any help but saw that Spring 2.1-M4 was released and decided to give it a try. Changing spring jars with the new version, the error disappeared - I guess this issue was resolved in this version of Spring. Now if one database is not accessible, only the DAOs which needs that
@PersistenceContext is affected - other DAOs work as expected.

So, Spring 2.1-M4 or later should be used when more than one persistence unit will be defined.

No comments: