Reputation: 3302
The scenario is that I want to handle the exception should the DB go down.
My assumption was that the DataAccessException would be thrown. However instead when I bring the DB down the exception that I get from the repository method is CannotCreateTransactionException. Is this right.
My application is a Spring CommandLine application and below is the code of How I access it
CustomerRepository cr = appContext.getBean(CustomerRepository.class);
cr.exists(customerId);
It works as expected but when the DB is down why is it throwing CannotCreateTransactionException. Or is my understanding of DataAccessException wrong.
When does DataAccessException come and when does CannotCreateTransactionException come?
Upvotes: 1
Views: 1383
Reputation: 3883
If you check the source code, you will see that DataAccessException
and its sub classes, are used to handle all kinds of error when you access you data in table, but not DB. Like the record is locked by other transaction, there will be some kind of DataAccessException
.
If you stop your DB during accessing database, the error maybe related with the database driver and datasource lib based on that. For example, whether you have a connection pool.
So, I think you can not specify which Exception will be thrown when the DB down.
Upvotes: 1