当前位置:网站首页>The unique index exception of the dream database in the Xinchuang environment cannot intercept the duplicatekeyexception
The unique index exception of the dream database in the Xinchuang environment cannot intercept the duplicatekeyexception
2022-07-19 16:18:00 【Heartsuit】
background
After the migration reaches the dream database , We found the unique index exception in our global exception interception DuplicateKeyException Cannot be intercepted normally , The original error message of the database is thrown directly to the front end , Extremely user-friendly .
Abnormal information
If the unique index exception is not intercepted , By default MySQL And Daydream database The exception information of is as follows :
MySQL abnormal :Error updating database. Cause: java.sql. SQLIntegrityConstraintViolationException: Duplicate entry ‘111’ for key ‘XXX’
Reach a dream 8 abnormal :Error updating database. Cause: dm.jdbc.driver. DMException: Violation of table [YYY] Uniqueness constraint
Global exception interception
stay SpringBoot Pass through RestControllerAdvice annotation , Realize the unified encapsulation of exception response . May refer to : Full stack development rear end scaffold :SpringBoot Integrate MybatisPlus Code generation , Pagination , Snowflake algorithm , Unified response , Abnormal interception ,Swagger3 Interface document
The following is the interception of database unique index exceptions , United return : The number cannot be repeated .
@RestControllerAdvice
public class GlobalExceptionHandler
{
/** * Unique index exception */
@ExceptionHandler(DuplicateKeyException.class)
public AjaxResult handleDuplicateKeyException(DuplicateKeyException e)
{
return AjaxResult.error(" The number cannot be repeated ");
}
}
Problem analysis
Spring Encapsulate and translate the exceptions of the mainstream database , about DuplicateKeyException Can intercept , But when it comes to domestic databases , For example, this is Dameng 8, Then its abnormal information Spring I don't know you .
DuplicateKeyException Production process
Spring JDBC The module will execute when a database exception occurs org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator#doTranslate Method , Put different databases errorCode mapping , Conversion to a custom framework exception .

SQLErrorCodes generation
Spring At startup, the resource file org/springframework/jdbc/support/sql-error-codes.xml Generate org.springframework.jdbc.support.SQLErrorCodes object , When an exception occurs in the program, it will be parsed according to the error code and mapped to Spring Defined database exception .
Solution
stay IDEA Double click directly in Shift, Input sql-error-codes You can quickly locate this XML file .

among sql-error-codes.xml There is a suggestive description in :“Can be overridden by definitions in a sql-error-codes.xml file - in the root of the class path.” , Tell us that this file can be overwritten , Directly copy the file to the corresponding module resources Under the table of contents , Add the error code mapping required by the corresponding database .
- Da Mengli
DuplicateKeyExceptionThe corresponding error code is-6602, You can check the official documents of Dameng .
sql-error-codes.xmlAfter modification, put it inresourcesCatalog
then , After throwing a unique index exception in daydream, it will be translated as DuplicateKeyException , Can be Spring Intercept .

- The modified
sql-error-codes.xmlfile
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "https://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!-- - 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. - - If the Database Product Name contains characters that are invalid - to use in the id attribute (like a space) then we need to add a property - named "databaseProductName"/"databaseProductNames" that holds this value. - If this property is present, then it will be used instead of the id for - looking up the error codes based on the current database. -->
<beans>
<bean id="DB2" name="Db2" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="databaseProductName">
<value>DB2*</value>
</property>
<property name="badSqlGrammarCodes">
<value>-007,-029,-097,-104,-109,-115,-128,-199,-204,-206,-301,-408,-441,-491</value>
</property>
<property name="duplicateKeyCodes">
<value>-803</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>-407,-530,-531,-532,-543,-544,-545,-603,-667</value>
</property>
<property name="dataAccessResourceFailureCodes">
<value>-904,-971</value>
</property>
<property name="transientDataAccessResourceCodes">
<value>-1035,-1218,-30080,-30081</value>
</property>
<property name="deadlockLoserCodes">
<value>-911,-913</value>
</property>
</bean>
<bean id="Derby" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="databaseProductName">
<value>Apache Derby</value>
</property>
<property name="useSqlStateForTranslation">
<value>true</value>
</property>
<property name="badSqlGrammarCodes">
<value>42802,42821,42X01,42X02,42X03,42X04,42X05,42X06,42X07,42X08</value>
</property>
<property name="duplicateKeyCodes">
<value>23505</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>22001,22005,23502,23503,23513,X0Y32</value>
</property>
<property name="dataAccessResourceFailureCodes">
<value>04501,08004,42Y07</value>
</property>
<property name="cannotAcquireLockCodes">
<value>40XL1</value>
</property>
<property name="deadlockLoserCodes">
<value>40001</value>
</property>
</bean>
<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>
<!-- https://help.sap.com/saphelp_hanaplatform/helpdata/en/20/a78d3275191014b41bae7c4a46d835/content.htm -->
<bean id="HDB" name="Hana" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="databaseProductNames">
<list>
<value>SAP HANA</value>
<value>SAP DB</value>
</list>
</property>
<property name="badSqlGrammarCodes">
<value>
257,259,260,261,262,263,264,267,268,269,270,271,272,273,275,276,277,278,
278,279,280,281,282,283,284,285,286,288,289,290,294,295,296,297,299,308,309,
313,315,316,318,319,320,321,322,323,324,328,329,330,333,335,336,337,338,340,
343,350,351,352,362,368
</value>
</property>
<property name="permissionDeniedCodes">
<value>10,258</value>
</property>
<property name="duplicateKeyCodes">
<value>301</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>461,462</value>
</property>
<property name="dataAccessResourceFailureCodes">
<value>-813,-709,-708,1024,1025,1026,1027,1029,1030,1031</value>
</property>
<property name="invalidResultSetAccessCodes">
<value>-11210,582,587,588,594</value>
</property>
<property name="cannotAcquireLockCodes">
<value>131</value>
</property>
<property name="cannotSerializeTransactionCodes">
<value>138,143</value>
</property>
<property name="deadlockLoserCodes">
<value>133</value>
</property>
</bean>
<bean id="HSQL" name="Hsql" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="databaseProductName">
<value>HSQL Database Engine</value>
</property>
<property name="badSqlGrammarCodes">
<value>-22,-28</value>
</property>
<property name="duplicateKeyCodes">
<value>-104</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>-9</value>
</property>
<property name="dataAccessResourceFailureCodes">
<value>-80</value>
</property>
</bean>
<bean id="Informix" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="databaseProductName">
<value>Informix Dynamic Server</value>
</property>
<property name="badSqlGrammarCodes">
<value>-201,-217,-696</value>
</property>
<property name="duplicateKeyCodes">
<value>-239,-268,-6017</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>-692,-11030</value>
</property>
</bean>
<bean id="MS-SQL" name="SqlServer" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="databaseProductName">
<value>Microsoft SQL Server</value>
</property>
<property name="badSqlGrammarCodes">
<value>156,170,207,208,209</value>
</property>
<property name="permissionDeniedCodes">
<value>229</value>
</property>
<property name="duplicateKeyCodes">
<value>2601,2627</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>544,8114,8115</value>
</property>
<property name="dataAccessResourceFailureCodes">
<value>4060</value>
</property>
<property name="cannotAcquireLockCodes">
<value>1222</value>
</property>
<property name="deadlockLoserCodes">
<value>1205</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,3572</value>
</property>
<property name="deadlockLoserCodes">
<value>1213</value>
</property>
</bean>
<bean id="Oracle" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="badSqlGrammarCodes">
<value>900,903,904,917,936,942,17006,6550</value>
</property>
<property name="invalidResultSetAccessCodes">
<value>17003</value>
</property>
<property name="duplicateKeyCodes">
<value>1</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>1400,1722,2291,2292</value>
</property>
<property name="dataAccessResourceFailureCodes">
<value>17002,17447</value>
</property>
<property name="cannotAcquireLockCodes">
<value>54,30006</value>
</property>
<property name="cannotSerializeTransactionCodes">
<value>8177</value>
</property>
<property name="deadlockLoserCodes">
<value>60</value>
</property>
</bean>
<bean id="PostgreSQL" name="Postgres" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="useSqlStateForTranslation">
<value>true</value>
</property>
<property name="badSqlGrammarCodes">
<value>03000,42000,42601,42602,42622,42804,42P01</value>
</property>
<property name="duplicateKeyCodes">
<value>21000,23505</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>23000,23502,23503,23514</value>
</property>
<property name="dataAccessResourceFailureCodes">
<value>53000,53100,53200,53300</value>
</property>
<property name="cannotAcquireLockCodes">
<value>55P03</value>
</property>
<property name="cannotSerializeTransactionCodes">
<value>40001</value>
</property>
<property name="deadlockLoserCodes">
<value>40P01</value>
</property>
</bean>
<bean id="Sybase" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="databaseProductNames">
<list>
<value>Sybase SQL Server</value>
<value>Adaptive Server Enterprise</value>
<value>ASE</value> <!-- name as returned by jTDS driver -->
<value>SQL Server</value>
<value>sql server</value> <!-- name as returned by jTDS driver -->
</list>
</property>
<property name="badSqlGrammarCodes">
<value>101,102,103,104,105,106,107,108,109,110,111,112,113,116,120,121,123,207,208,213,257,512</value>
</property>
<property name="duplicateKeyCodes">
<value>2601,2615,2626</value>
</property>
<property name="dataIntegrityViolationCodes">
<value>233,511,515,530,546,547,2615,2714</value>
</property>
<property name="transientDataAccessResourceCodes">
<value>921,1105</value>
</property>
<property name="cannotAcquireLockCodes">
<value>12205</value>
</property>
<property name="deadlockLoserCodes">
<value>1205</value>
</property>
</bean>
<!-- Support Dameng database error code -->
<bean id="DM" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="databaseProductNames">
<list>
<!-- There are spaces in the data source name, which cannot be like Oracle As directly beanId -->
<value>DM DBMS</value>
</list>
</property>
<property name="duplicateKeyCodes">
<value>-6602</value>
</property>
</bean>
</beans>
Reference
If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!
边栏推荐
- 无心剑中译狄兰·托马斯《不要温顺地走进那个良夜》
- 中信证券网上开户安全吗?开户的流程是什么?
- 腾讯出品的良心产品?翻译君功能详细体验
- 亿学学堂赠送的账户是哪家证券公司呢?开户安全吗
- MySQL - unique key constraint for table fields
- LeetCode 300:最长递增子序列
- Typescript中对象扩展之动态添加方法与属性修改
- Solve the problem of "license manager error -8" after matlab installation (the personal test is valid)
- CH549/CH548学习笔记6 - 读取芯片ID
- A few lines of code, let black and white old photos regain their vitality!
猜你喜欢

STM32+A4988控制步进电机

Ch549/ch548 learning notes 2 - system clock

Anaconda的基本使用与在Pycharm中使用

深度学习环境配置Pytorch

Vscode下Latex 图片引用时 编译报错Recipe terminated with error. Retry building the project.

CH549/CH548学习笔记3 - UART

JVM personal learning notes

CH549/CH548学习笔记4 - Timer

Ch549/ch548 learning notes 8 - USB device interrupt processing

CH549/CH548学习笔记2 - 系统时钟
随机推荐
Pwnthebox, Web: Double - S
【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单
数学建模--从自然走向理性之路--数学建模概论(学习笔记1)
Identity server 4 uses openid connect to add user authentication (3)
How to rewrite hashcode method correctly?
Okaleido或杀出NFT重围,你看好它吗?
中信证券网上开户安全吗?开户的流程是什么?
腾讯出品的良心产品?翻译君功能详细体验
MySQL -调整列的约束
Force buckle 565 Array nesting
BFD of HCNP Routing & Switching
Liquibase learning 3 - loglevel log level, common commands
ROS与STM32通信的实现
初识convolution(卷积)+matlab对离散卷积过程的直观认识
MySQL - adjust column constraints
【单片机仿真项目】LED 闪烁灯(proteus原理图+keil代码)
Ch549/ch548 learning notes 1 - hardware design
PwnTheBox,Web:Double-S
同花顺软件炒股线上怎么免费吗??开户安全吗?
[MCU simulation project] the nixie tube displays 0 to 5 in turn (Proteus schematic +keil code)