Spring之 JDBC 異常


JDBC異常抽象

Spring會將數據操作的異常轉換為DataAccessException

Spring之 JDBC 異常

 

解析錯誤碼

  1. SQLErrorCodeSQLExceptionTranslator
  2. ErrorCode定義
  • org/springframework/jdbc/support/sql-error-codes.xml
  • classpath下的sql-error-codes.xml(定制)

org/springframework/jdbc/support/sql-error-codes.xml

Default SQL error codes for well-known databases. Can be overridden by definitions in a “sql-error-codes.xml” file in the root of the class path.

<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>
</bean>
<bean id="MySQL" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="databaseProductNames">
<list>
<value>MySQL</value>
<value>MariaDB</value>
</list>
</property>
<property name="badSqlGrammarCodes">
<value>1054,1064,1146</value>
</property>
<property name="duplicateKeyCodes">
<value>1062</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>630,839,840,893,1169,1215,1216,1217,1364,1451,1452,1557</value>
</property>
<property name="dataAccessResourceFailureCodes">
<value>1</value>
</property>
<property name="cannotAcquireLockCodes">
<value>1205</value>
</property>
<property name="deadlockLoserCodes">
<value>1213</value>
</property>
</bean>

org.springframework.jdbc.support.SQLErrorCodes

public class SQLErrorCodes {
@Nullable
private String[] databaseProductNames;
private boolean useSqlStateForTranslation = false;
private String[] badSqlGrammarCodes = new String[0];
private String[] invalidResultSetAccessCodes = new String[0];
private String[] duplicateKeyCodes = new String[0];
private String[] dataIntegrityViolationCodes = new String[0];
private String[] permissionDeniedCodes = new String[0];
private String[] dataAccessResourceFailureCodes = new String[0];
private String[] transientDataAccessResourceCodes = new String[0];
private String[] cannotAcquireLockCodes = new String[0];
private String[] deadlockLoserCodes = new String[0];
private String[] cannotSerializeTransactionCodes = new String[0];
@Nullable
private CustomSQLErrorCodesTranslation[] customTranslations;
@Nullable
private SQLExceptionTranslator customSqlExceptionTranslator;
}

定制錯誤碼

項目路徑

└── src
├── main
│ ├── java
│ │ └── me
│ │ └── zhongmingmao
│ │ └── jdbcexception
│ │ ├── CustomDuplicateKeyException.java
│ │ └── JdbcExceptionApplication.java
│ └── resources
│ ├── application.properties
│ ├── schema.sql
│ └── sql-error-codes.xml
└── test
└── java
└── me
└── zhongmingmao
└── jdbcexception
└── JdbcExceptionApplicationTests.java

sql-error-codes.xml

src/main/resources/sql-error-codes.xml

<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>
<!-- 定制:錯誤碼為23001或23505時,不會拋出Spring的DuplicateKeyException,而是拋出CustomDuplicateKeyException -->
<property name="customTranslations">
<bean class="org.springframework.jdbc.support.CustomSQLErrorCodesTranslation">
<property name="errorCodes" value="23001,23505"/>
<property name="exceptionClass" value="me.zhongmingmao.jdbcexception.CustomDuplicateKeyException"/>
</bean>
</property>
</bean>
</beans>

CustomDuplicateKeyException

public class CustomDuplicateKeyException extends DuplicateKeyException {
public CustomDuplicateKeyException(String msg) {
super(msg);
}
public CustomDuplicateKeyException(String msg, Throwable cause) {
super(msg, cause);
}
}

單元測試

@RunWith(SpringRunner.class)
@SpringBootTest
public class JdbcExceptionApplicationTests {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test(expected = CustomDuplicateKeyException.class)
public void testThrowCustomDuplicateKeyException() {
jdbcTemplate.execute("INSERT INTO PERSON (ID, NAME) VALUES ('1', 'zhongmingmao')");
jdbcTemplate.execute("INSERT INTO PERSON (ID, NAME) VALUES ('1', 'zhongmingwu')");
}
}

最后,

小編在這里分享一些學習資料,不止spring的異常問題,還有關於分布式,微服務,性能優化,Spring,MyBatis的等源碼知識點的錄像視頻還有各種基礎學習資料,及面試題資料哦!希望對大家在Java的學習中能夠起到幫助!我是小架,一個專注於java學習的人,以后會有更多的新內容新知識點帶給大家!

Spring之 JDBC 異常

 

Spring之 JDBC 異常

 

需要資料請關注我哦,加我的交流群772300343獲取!

我是小架,感謝大家一直以來的關注!

我們下篇文章見!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM