Example usage for org.springframework.dao.support PersistenceExceptionTranslator translateExceptionIfPossible

List of usage examples for org.springframework.dao.support PersistenceExceptionTranslator translateExceptionIfPossible

Introduction

In this page you can find the example usage for org.springframework.dao.support PersistenceExceptionTranslator translateExceptionIfPossible.

Prototype

@Nullable
DataAccessException translateExceptionIfPossible(RuntimeException ex);

Source Link

Document

Translate the given runtime exception thrown by a persistence framework to a corresponding exception from Spring's generic org.springframework.dao.DataAccessException hierarchy, if possible.

Usage

From source file:org.seasar.doma.boot.autoconfigure.DomaAutoConfigurationTest.java

@Test
public void testSQLExceptionTranslator() {
    this.context.register(DomaAutoConfiguration.class, DataSourceAutoConfiguration.class);
    this.context.refresh();
    PersistenceExceptionTranslator translator = this.context.getBean(PersistenceExceptionTranslator.class);
    {/* w w w.  ja  v  a2  s  . c o m*/
        // Translated by SQLErrorCodeSQLExceptionTranslator
        DataAccessException dataAccessException = translator.translateExceptionIfPossible(new JdbcException(
                Message.DOMA2008, new SQLException("Acquire Lock on H2", "SqlState", 50200, null)));
        assertThat(dataAccessException, is(instanceOf(CannotAcquireLockException.class)));
    }
    {
        // Translated by SQLExceptionSubclassTranslator(fallback)
        DataAccessException dataAccessException = translator.translateExceptionIfPossible(
                new JdbcException(Message.DOMA2008, new SQLTimeoutException("Timeout", "SqlState", -1, null)));
        assertThat(dataAccessException, is(instanceOf(QueryTimeoutException.class)));
    }
    {
        // Translated by SQLStateSQLExceptionTranslator (fallback)
        DataAccessException dataAccessException = translator.translateExceptionIfPossible(
                new JdbcException(Message.DOMA2008, new SQLException("With check violation", "44", -1, null)));
        assertThat(dataAccessException, is(instanceOf(DataIntegrityViolationException.class)));
    }
}