提交 a0139abf 编写于 作者: T Thomas Risberg

added a DuplicatKeyException catagory for SQLException translation (SPR-5125)

上级 d9f5a7a6
...@@ -32,6 +32,7 @@ import org.springframework.dao.DataIntegrityViolationException; ...@@ -32,6 +32,7 @@ import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DeadlockLoserDataAccessException; import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.dao.PermissionDeniedDataAccessException; import org.springframework.dao.PermissionDeniedDataAccessException;
import org.springframework.dao.TransientDataAccessResourceException; import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.InvalidResultSetAccessException; import org.springframework.jdbc.InvalidResultSetAccessException;
...@@ -224,6 +225,10 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep ...@@ -224,6 +225,10 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep
logTranslation(task, sql, sqlEx, false); logTranslation(task, sql, sqlEx, false);
return new InvalidResultSetAccessException(task, sql, sqlEx); return new InvalidResultSetAccessException(task, sql, sqlEx);
} }
else if (Arrays.binarySearch(this.sqlErrorCodes.getDuplicateKeyCodes(), errorCode) >= 0) {
logTranslation(task, sql, sqlEx, false);
return new DuplicateKeyException(buildMessage(task, sql, sqlEx), sqlEx);
}
else if (Arrays.binarySearch(this.sqlErrorCodes.getDataIntegrityViolationCodes(), errorCode) >= 0) { else if (Arrays.binarySearch(this.sqlErrorCodes.getDataIntegrityViolationCodes(), errorCode) >= 0) {
logTranslation(task, sql, sqlEx, false); logTranslation(task, sql, sqlEx, false);
return new DataIntegrityViolationException(buildMessage(task, sql, sqlEx), sqlEx); return new DataIntegrityViolationException(buildMessage(task, sql, sqlEx), sqlEx);
......
...@@ -41,6 +41,8 @@ public class SQLErrorCodes { ...@@ -41,6 +41,8 @@ public class SQLErrorCodes {
private String[] invalidResultSetAccessCodes = new String[0]; private String[] invalidResultSetAccessCodes = new String[0];
private String[] duplicateKeyCodes = new String[0];
private String[] dataIntegrityViolationCodes = new String[0]; private String[] dataIntegrityViolationCodes = new String[0];
private String[] permissionDeniedCodes = new String[0]; private String[] permissionDeniedCodes = new String[0];
...@@ -112,6 +114,14 @@ public class SQLErrorCodes { ...@@ -112,6 +114,14 @@ public class SQLErrorCodes {
return this.invalidResultSetAccessCodes; return this.invalidResultSetAccessCodes;
} }
public String[] getDuplicateKeyCodes() {
return duplicateKeyCodes;
}
public void setDuplicateKeyCodes(String[] duplicateKeyCodes) {
this.duplicateKeyCodes = duplicateKeyCodes;
}
public void setDataIntegrityViolationCodes(String[] dataIntegrityViolationCodes) { public void setDataIntegrityViolationCodes(String[] dataIntegrityViolationCodes) {
this.dataIntegrityViolationCodes = StringUtils.sortStringArray(dataIntegrityViolationCodes); this.dataIntegrityViolationCodes = StringUtils.sortStringArray(dataIntegrityViolationCodes);
} }
......
...@@ -21,8 +21,11 @@ ...@@ -21,8 +21,11 @@
<property name="badSqlGrammarCodes"> <property name="badSqlGrammarCodes">
<value>-007,-029,-097,-104,-109,-115,-128,-199,-204,-206,-301,-408,-441,-491</value> <value>-007,-029,-097,-104,-109,-115,-128,-199,-204,-206,-301,-408,-441,-491</value>
</property> </property>
<property name="duplicateKeyCodes">
<value>-803</value>
</property>
<property name="dataIntegrityViolationCodes"> <property name="dataIntegrityViolationCodes">
<value>-407,-530,-531,-532,-543,-544,-545,-603,-667,-803</value> <value>-407,-530,-531,-532,-543,-544,-545,-603,-667</value>
</property> </property>
<property name="dataAccessResourceFailureCodes"> <property name="dataAccessResourceFailureCodes">
<value>-904,-971</value> <value>-904,-971</value>
...@@ -45,8 +48,11 @@ ...@@ -45,8 +48,11 @@
<property name="badSqlGrammarCodes"> <property name="badSqlGrammarCodes">
<value>42802,42821,42X01,42X02,42X03,42X04,42X05,42X06,42X07,42X08</value> <value>42802,42821,42X01,42X02,42X03,42X04,42X05,42X06,42X07,42X08</value>
</property> </property>
<property name="duplicateKeyCodes">
<value>23505</value>
</property>
<property name="dataIntegrityViolationCodes"> <property name="dataIntegrityViolationCodes">
<value>22001,22005,23502,23503,23505,23513,X0Y32</value> <value>22001,22005,23502,23503,23513,X0Y32</value>
</property> </property>
<property name="dataAccessResourceFailureCodes"> <property name="dataAccessResourceFailureCodes">
<value>04501,08004,42Y07</value> <value>04501,08004,42Y07</value>
...@@ -63,8 +69,11 @@ ...@@ -63,8 +69,11 @@
<property name="badSqlGrammarCodes"> <property name="badSqlGrammarCodes">
<value>42000,42001,42101,42102,42111,42112,42121,42122,42132</value> <value>42000,42001,42101,42102,42111,42112,42121,42122,42132</value>
</property> </property>
<property name="duplicateKeyCodes">
<value>23001</value>
</property>
<property name="dataIntegrityViolationCodes"> <property name="dataIntegrityViolationCodes">
<value>22003,22012,22025,23000,23001</value> <value>22003,22012,22025,23000</value>
</property> </property>
<property name="dataAccessResourceFailureCodes"> <property name="dataAccessResourceFailureCodes">
<value>90046,90100,90117,90121,90126</value> <value>90046,90100,90117,90121,90126</value>
...@@ -78,6 +87,9 @@ ...@@ -78,6 +87,9 @@
<property name="badSqlGrammarCodes"> <property name="badSqlGrammarCodes">
<value>-22,-28</value> <value>-22,-28</value>
</property> </property>
<property name="duplicateKeyCodes">
<value>-104</value>
</property>
<property name="dataIntegrityViolationCodes"> <property name="dataIntegrityViolationCodes">
<value>-9</value> <value>-9</value>
</property> </property>
...@@ -93,8 +105,11 @@ ...@@ -93,8 +105,11 @@
<property name="badSqlGrammarCodes"> <property name="badSqlGrammarCodes">
<value>-201,-217,-696</value> <value>-201,-217,-696</value>
</property> </property>
<property name="duplicateKeyCodes">
<value>-239,-268,-6017</value>
</property>
<property name="dataIntegrityViolationCodes"> <property name="dataIntegrityViolationCodes">
<value>-239,-268,-692,-11030</value> <value>-692,-11030</value>
</property> </property>
</bean> </bean>
...@@ -108,8 +123,11 @@ ...@@ -108,8 +123,11 @@
<property name="permissionDeniedCodes"> <property name="permissionDeniedCodes">
<value>229</value> <value>229</value>
</property> </property>
<property name="duplicateKeyCodes">
<value>2601,2627</value>
</property>
<property name="dataIntegrityViolationCodes"> <property name="dataIntegrityViolationCodes">
<value>544,2601,2627,8114,8115</value> <value>544,8114,8115</value>
</property> </property>
<property name="cannotAcquireLockCodes"> <property name="cannotAcquireLockCodes">
<value>1222</value> <value>1222</value>
...@@ -123,8 +141,11 @@ ...@@ -123,8 +141,11 @@
<property name="badSqlGrammarCodes"> <property name="badSqlGrammarCodes">
<value>1054,1064,1146</value> <value>1054,1064,1146</value>
</property> </property>
<property name="duplicateKeyCodes">
<value>1062</value>
</property>
<property name="dataIntegrityViolationCodes"> <property name="dataIntegrityViolationCodes">
<value>630,839,840,893,1062,1169,1215,1216,1217,1451,1452,1557</value> <value>630,839,840,893,1169,1215,1216,1217,1451,1452,1557</value>
</property> </property>
<property name="dataAccessResourceFailureCodes"> <property name="dataAccessResourceFailureCodes">
<value>1</value> <value>1</value>
...@@ -144,8 +165,11 @@ ...@@ -144,8 +165,11 @@
<property name="invalidResultSetAccessCodes"> <property name="invalidResultSetAccessCodes">
<value>17003</value> <value>17003</value>
</property> </property>
<property name="duplicateKeyCodes">
<value>1</value>
</property>
<property name="dataIntegrityViolationCodes"> <property name="dataIntegrityViolationCodes">
<value>1,1400,1722,2291,2292</value> <value>1400,1722,2291,2292</value>
</property> </property>
<property name="dataAccessResourceFailureCodes"> <property name="dataAccessResourceFailureCodes">
<value>17002,17447</value> <value>17002,17447</value>
...@@ -168,8 +192,11 @@ ...@@ -168,8 +192,11 @@
<property name="badSqlGrammarCodes"> <property name="badSqlGrammarCodes">
<value>03000,42000,42601,42602,42622,42804,42P01</value> <value>03000,42000,42601,42602,42622,42804,42P01</value>
</property> </property>
<property name="duplicateKeyCodes">
<value>23505</value>
</property>
<property name="dataIntegrityViolationCodes"> <property name="dataIntegrityViolationCodes">
<value>23000,23502,23503,23505,23514</value> <value>23000,23502,23503,23514</value>
</property> </property>
<property name="dataAccessResourceFailureCodes"> <property name="dataAccessResourceFailureCodes">
<value>53000,53100,53200,53300</value> <value>53000,53100,53200,53300</value>
...@@ -196,8 +223,11 @@ ...@@ -196,8 +223,11 @@
<property name="badSqlGrammarCodes"> <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> <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>
<property name="duplicateKeyCodes">
<value>2601</value>
</property>
<property name="dataIntegrityViolationCodes"> <property name="dataIntegrityViolationCodes">
<value>233,423,511,515,530,547,2601,2615,2714</value> <value>233,423,511,515,530,547,2615,2714</value>
</property> </property>
<property name="transientDataAccessResourceCodes"> <property name="transientDataAccessResourceCodes">
<value>921,1105</value> <value>921,1105</value>
......
...@@ -27,6 +27,7 @@ import org.springframework.dao.DataAccessException; ...@@ -27,6 +27,7 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DeadlockLoserDataAccessException; import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.InvalidResultSetAccessException; import org.springframework.jdbc.InvalidResultSetAccessException;
...@@ -39,6 +40,7 @@ public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase { ...@@ -39,6 +40,7 @@ public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase {
static { static {
ERROR_CODES.setBadSqlGrammarCodes(new String[] { "1", "2" }); ERROR_CODES.setBadSqlGrammarCodes(new String[] { "1", "2" });
ERROR_CODES.setInvalidResultSetAccessCodes(new String[] { "3", "4" }); ERROR_CODES.setInvalidResultSetAccessCodes(new String[] { "3", "4" });
ERROR_CODES.setDuplicateKeyCodes(new String[] {"10"});
ERROR_CODES.setDataAccessResourceFailureCodes(new String[] { "5" }); ERROR_CODES.setDataAccessResourceFailureCodes(new String[] { "5" });
ERROR_CODES.setDataIntegrityViolationCodes(new String[] { "6" }); ERROR_CODES.setDataIntegrityViolationCodes(new String[] { "6" });
ERROR_CODES.setCannotAcquireLockCodes(new String[] { "7" }); ERROR_CODES.setCannotAcquireLockCodes(new String[] { "7" });
...@@ -64,6 +66,12 @@ public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase { ...@@ -64,6 +66,12 @@ public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase {
checkTranslation(sext, 7, CannotAcquireLockException.class); checkTranslation(sext, 7, CannotAcquireLockException.class);
checkTranslation(sext, 8, DeadlockLoserDataAccessException.class); checkTranslation(sext, 8, DeadlockLoserDataAccessException.class);
checkTranslation(sext, 9, CannotSerializeTransactionException.class); checkTranslation(sext, 9, CannotSerializeTransactionException.class);
checkTranslation(sext, 10, DuplicateKeyException.class);
SQLException dupKeyEx = new SQLException("", "", 10);
DataAccessException dksex = sext.translate("task", "SQL", dupKeyEx);
assertTrue("Not instance of DataIntegrityViolationException",
DataIntegrityViolationException.class.isAssignableFrom(dksex.getClass()));
// Test fallback. We assume that no database will ever return this error code, // Test fallback. We assume that no database will ever return this error code,
// but 07xxx will be bad grammar picked up by the fallback SQLState translator // but 07xxx will be bad grammar picked up by the fallback SQLState translator
......
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.dao;
/**
* Exception thrown when an attempt to insert or update data
* results in violation of an primary key or unique constraint.
* Note that this is not necessarily a purely relational concept;
* unique primary keys are required by most database types.
*
* @author Thomas Risberg
*/
public class DuplicateKeyException extends DataIntegrityViolationException {
/**
* Constructor for DuplicateKeyException.
* @param msg the detail message
*/
public DuplicateKeyException(String msg) {
super(msg);
}
/**
* Constructor for DuplicateKeyException.
* @param msg the detail message
* @param cause the root cause from the data access API in use
*/
public DuplicateKeyException(String msg, Throwable cause) {
super(msg, cause);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册