Study notes @玩转Spring全家斗
The content of the course comes from the geek time playing Spring Family Bucket, invaded and deleted , the link is as follows
https://time.geekbang.org/course/intro/100023501
Fourth day
Spring's abnormal abstraction
DataAccessException
Spring will convert data operation exceptions to DataAccessExceptions,
no matter what data access method is used, the same exceptions can be used
How does Spring recognize those error codes?
Parse the error codes through the SQLErrorCodeSQLExceptionTranslator class
and define the ErrorCode of each database in it
•org/springframework/jdbc/support/sql-error-codes.xml
•Classpath下的sql-error-codes.x
Sample code: /resources/sql-error-codes.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="H2" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="badSqlGrammarCodes">
<value>42000,42001,42101,42102,42111,42112,42121,42122,42132</value>
</property>
<property name="duplicateKeyCodes">
<value>23001,23505</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>22001,22003,22012,22018,22025,23000,23002,23003,23502,23503,23506,23507,23513</value>
</property>
<property name="dataAccessResourceFailureCodes">
<value>90046,90100,90117,90121,90126</value>
</property>
<property name="cannotAcquireLockCodes">
<value>50200</value>
</property>
<property name="customTranslations">
<bean class="org.springframework.jdbc.support.CustomSQLErrorCodesTranslation">
<property name="errorCodes" value="23001,23505" />
<property name="exceptionClass"
value="geektime.spring.data.errorcodedemo.CustomDuplicatedKeyException" />
</bean>
</property>
</bean>
</beans>
DuplicateKeyException exception class
package geektime.spring.data.errorcodedemo;
import org.springframework.dao.DuplicateKeyException;
public class CustomDuplicatedKeyException extends DuplicateKeyException {
public CustomDuplicatedKeyException(String msg) {
super(msg);
}
public CustomDuplicatedKeyException(String msg, Throwable cause) {
super(msg, cause);
}
}
ErrorCodeDemoApplicationTests test class
package geektime.spring.data.errorcodedemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ErrorCodeDemoApplicationTests {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test(expected = CustomDuplicatedKeyException.class)
public void testThrowingCustomException() {
jdbcTemplate.execute("INSERT INTO FOO (ID, BAR) VALUES (1, 'a')");
jdbcTemplate.execute("INSERT INTO FOO (ID, BAR) VALUES (1, 'b')");
}
}
You can see that in the test class, the desired exception is the DuplicateKeyException class, and the result is passed.
As long as the exception thrown is a subclass of DataAccessException, otherwise Spring will report an error and cannot throw an exception.