Skip to content

Spring Data JDBC throws DbActionExecutionException, which is the correct exception handling? #1086

New issue

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

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

Already on GitHub? # to your account

Closed
cypher256 opened this issue Nov 16, 2021 · 2 comments
Assignees
Labels
status: duplicate A duplicate of another issue

Comments

@cypher256
Copy link

cypher256 commented Nov 16, 2021

I think the way to use PersistenceExceptionTranslator is better, is there any problem with it?
Related issue: #831 save() method is raising a DbActionExecutionException on unique key violation

// Catch DbActionExecutionException and resolve by instanceof.
try {
	--- Access Spring Data JDBC Repository ---
} catch (DbActionExecutionException e) {
	if (e.getCause() instanceof DuplicateKeyException) {
		//
	} else if (e.getCause() instanceof OptimisticLockingFailureException) {
		//
	} else {
		throw e;
	}
}
// Define an Exception Translator bean to convert DbActionExecutionException to DataAccessException.
@Bean
public PersistenceExceptionTranslator exceptionTranslator() {
	return new PersistenceExceptionTranslator() {
		@Override
		public DataAccessException translateExceptionIfPossible(RuntimeException e) {
			if (e instanceof DataAccessException) {
				return (DataAccessException) e;
			}
			if (e.getCause() instanceof DataAccessException) {
				return (DataAccessException) e.getCause();
			}
			return null;
		}
	};
}

try {
	--- Access Spring Data JDBC Repository ---
} catch (DuplicateKeyException e) {
	//
} catch (OptimisticLockingFailureException e) {
	//
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 16, 2021
@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 14, 2022
@schauder
Copy link
Contributor

I agree we should support the use of exception translators. Therefore the JdbcAggregateTemplate should invoke the exception translation handling.

@schauder
Copy link
Contributor

As part of #1956 we decided to drop the DbActionExecutionException.

So the normal Spring exception handling for JdbcTemplate will take over.

@schauder schauder added status: duplicate A duplicate of another issue and removed type: enhancement A general enhancement labels Mar 14, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants