2
votes

I am porting an ASP.NET Webform application that runs with Spring.Net and NHibernate to ASP.NET MVC3. After creating a new project and verifying that Spring.Net can properly inject dependencies across projects in the same solution, I went on with the import of the service and persistence layer projects in the new solution.

My mappings now look somewhat ok (more about it at the end of this post) but when I start the application, I get the following error:

No persistence exception translators found in container. Cannot perform exception translation.

[ConfigurationErrorsException: Error creating context 'spring.root': No persistence exception translators found in container. Cannot perform exception translation.]

If I understand this well, it means that an exception was raised and that Spring.Net does not know how to map / handle it... is that correct?

Assuming that it came from NHibernate, and as explained in section 44.3.3 from the SpringFramework documentation related to NHibernate integration, I added an ExceptionTranslationProcessor to my mapping...

<object type="Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data"/>

... but it does not seem to do anything and I don't know where to look further. Anyone with a brilliant idea or a hint around?

Thanks in advance!


Here's some data for reference purpose:

To the web.config file I added the spring section group and the references to the xml definition files:

<configuration>
    <!-- Snip -->
    <configSections>
        <sectionGroup name="spring">            
            <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc3"/>
            <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
            <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
        </sectionGroup>
    </configSections>
    <spring>
        <context>
            <resource uri="file://~/Config/controllers.xml"/>
            <resource uri="assembly://Service/Service.Config/Service.xml"/>
            <resource uri="assembly://Persistence.Hibernate/Persistence.Hibernate.Config/Persistence.xml"/>
        </context>
    </spring>
    <!-- Snip -->
</configuration>

The PersistenceExceptionTranslationPostProcessor I added in the Persistence.xml file. Out of ideas, I also tried adding it to the other xml but it did not do anything.

2

2 Answers

3
votes

In addition to @Marijn s post, depending on your configuration sometimes the exception No persistence exception translators found in container. covers the real cause. Attaching to your process start and catching all spring.net exceptions (Ctrl+Alt+E / CLR Exceptions) usally reveals the root cause.

For instance a misconfigured SessionFactory even if a proper PersistenceExceptionTranslationPostProcessor is setup and registered to the context.

I usally remove the Repository attribute from my DAOs (which adds the exception translation via AOP and covers the error) to eliminate the need for the PersistenceExceptionTranslationPostProcessor resulting in the underlying error to show up.

In addition you can post the relevant parts of your web.config and Persistence.xml.

1
votes

You get this error when there isn't an object that implements IPersistenceExceptionTranslator in your container. When you have a Spring.Net+NHibernate application, then this would typically be Spring.Net's LocalSessionFactoryObject. The PersistenceExceptionTranslationPostProcessor inspects the context for registered IPersistenceExceptionTranslator instances.

You don't get this error on an NHibernate exception, but at container start-up. The problem is that the Spring container wants to do exception translation, but it cannot find a single exception translator in the container.

If you don't use Spring.net's LocalSessionFactoryObject (for whatever reason), you should register your own IPersistenceExceptionTranslator with the container. The interface is quite simple:

public interface IPersistenceExceptionTranslator
{
    DataAccessException TranslateExceptionIfPossible(Exception ex);
}

You can take the LocalSessionFactoryObject implementation of IPersistenceExceptionTranslator as an example.