Skip to content

Use a SQLExceptionTranslator bean if defined #43499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
OrangeDog opened this issue Dec 13, 2024 · 1 comment
Closed

Use a SQLExceptionTranslator bean if defined #43499

OrangeDog opened this issue Dec 13, 2024 · 1 comment
Labels
status: superseded An issue that has been superseded by another type: enhancement A general enhancement

Comments

@OrangeDog
Copy link
Contributor

If a SQLExceptionTranslator bean has been defined, it should be used in the autoconfiguration of JdbcTemplate and LocalContainerEntityManagerFactoryBean.

Otherwise, it is fiddly to guarantee that the translator has been set before the autoconfigured beans are fist used. I think this would do it, but it's going to slow down startup:

@RequiredArgsConstructor
public class SQLExceptionTranslatorBeanPostProcessor implements BeanPostProcessor {

    private final SQLExceptionTranslator translator;

    public @Nullable Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof AbstractEntityManagerFactoryBean emfb) {
            JpaDialect dialect = emfb.getJpaVendorAdapter().getJpaDialect();
            if (dialect instanceof HibernateJpaDialect hibernate) {
                hibernate.setJdbcExceptionTranslator(translator);
            }
        } else if (bean instanceof JdbcTemplate template) {
            template.setExceptionTranslator(translator);
        }
        return bean;
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 13, 2024
@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 13, 2024
@philwebb philwebb added this to the 3.5.x milestone Dec 13, 2024
@snicoll
Copy link
Member

snicoll commented Dec 16, 2024

Closing in favor of PR #43511

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Dec 16, 2024
@snicoll snicoll added the status: superseded An issue that has been superseded by another label Dec 16, 2024
@snicoll snicoll removed this from the 3.5.x milestone Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants