SPRNET-1215 - Add DuplicateKeyException to DAO exception hierachy

This commit is contained in:
markpollack
2009-06-04 04:02:33 +00:00
parent 1c1ea1403e
commit ca0c1282a1
5 changed files with 223 additions and 33 deletions

View File

@@ -39,23 +39,25 @@ namespace Spring.Data.Common
public class ErrorCodes
{
#region Fields
private String[] databaseProductNames;
private string[] databaseProductNames;
private String[] badSqlGrammarCodes = new String[0];
private string[] badSqlGrammarCodes = new String[0];
private String[] invalidResultSetAccessCodes = new String[0];
private string[] invalidResultSetAccessCodes = new String[0];
private String[] dataAccessResourceFailureCodes = new String[0];
private string[] dataAccessResourceFailureCodes = new String[0];
private String[] permissionDeniedCodes = new String[0];
private string[] permissionDeniedCodes = new String[0];
private String[] dataIntegrityViolationCodes = new String[0];
private string[] dataIntegrityViolationCodes = new String[0];
private String[] cannotAcquireLockCodes = new String[0];
private string[] cannotAcquireLockCodes = new String[0];
private String[] deadlockLoserCodes = new String[0];
private string[] deadlockLoserCodes = new String[0];
private String[] cannotSerializeTransactionCodes = new String[0];
private string[] cannotSerializeTransactionCodes = new String[0];
private string[] duplicateKeyCodes = new string[0];
// CustomErrorCodesTranslation[] customTranslations;
#endregion
@@ -144,13 +146,14 @@ namespace Spring.Data.Common
get { return cannotSerializeTransactionCodes; }
set { cannotSerializeTransactionCodes = value; }
}
#endregion
#region Methods
public string[] DuplicateKeyCodes
{
get { return duplicateKeyCodes; }
set { duplicateKeyCodes = value; }
}
#endregion
}
}

View File

@@ -0,0 +1,141 @@
#region License
/*
* Copyright <20> 2002-2005 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.
*/
#endregion
#region Imports
using System;
using System.Runtime.Serialization;
using Spring.Dao;
#endregion
namespace Spring.Data
{
/// <summary>
/// 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.
/// </summary>
/// <author>Thomas Risberg</author>
/// <author>Mark Pollack (.NET)</author>
[Serializable]
public class DuplicateKeyException : DataIntegrityViolationException
{
#region Fields
/// <summary>
/// SQL that led to the problem
/// </summary>
private string sql;
#endregion
#region Constructor (s)
/// <summary>
/// Initializes a new instance of the <see cref="DuplicateKeyException"/> class.
/// </summary>
public DuplicateKeyException()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DuplicateKeyException"/> class.
/// </summary>
/// <param name="message">A message about the exception.</param>
public DuplicateKeyException(string message): base(message)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DuplicateKeyException"/> class.
/// </summary>
/// <param name="message">A message about the exception.</param>
/// <param name="inner">The inner exception.</param>
public DuplicateKeyException(string message, Exception inner): base(message, inner)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DuplicateKeyException"/> class.
/// </summary>
/// <param name="task">name of the current task.</param>
/// <param name="sql">The offending SQL statment</param>
/// <param name="ex">The root cause.</param>
public DuplicateKeyException(string task, String sql, Exception ex) : base(task + "; Duplicate key for SQL [" + sql + "]; " + ex.Message, ex)
{
this.sql = sql;
}
/// <summary>
/// Creates a new instance of the
/// <see cref="DuplicateKeyException"/> class.
/// </summary>
/// <param name="info">
/// The <see cref="System.Runtime.Serialization.SerializationInfo"/>
/// that holds the serialized object data about the exception being thrown.
/// </param>
/// <param name="context">
/// The <see cref="System.Runtime.Serialization.StreamingContext"/>
/// that contains contextual information about the source or destination.
/// </param>
protected DuplicateKeyException(SerializationInfo info, StreamingContext context) : base(info, context) { }
#endregion
#region Properties
/// <summary>
/// Gets the SQL that caused the exception
/// </summary>
/// <value>The SQL that caused the exception.</value>
public string Sql
{
get
{
return sql;
}
}
#endregion
#region Methods
#endregion
#region ISerializable Members
/// <summary>
/// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"/>
/// with information about the exception.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is a null reference (<see langword="Nothing"/> in Visual Basic).</exception>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue( "sql", sql );
base.GetObjectData( info, context );
}
#endregion
}
}

View File

@@ -141,7 +141,8 @@ namespace Spring.Data.Support
#endregion
/// <summary>
/// <summary>
/// Translate the given <see cref="System.SystemException"/> into a generic data access exception.
/// </summary>
/// <param name="task">A readable string describing the task being attempted.</param>
@@ -152,19 +153,61 @@ namespace Spring.Data.Support
/// <returns>
/// A <see cref="Spring.Dao.DataAccessException"/> appropriate for the supplied
/// <paramref name="exception"/>.
/// </returns>
public virtual DataAccessException Translate(string task, string sql, Exception exception)
/// </returns>
public virtual DataAccessException Translate(string task, string sql, Exception exception)
{
if (task == null)
{
task = "";
}
if (sql == null)
{
sql = "";
}
string errorCode = ExtractErrorCode(exception);
// First, try custom translation from overridden method.
DataAccessException dex = DoTranslate(task, sql, errorCode, exception);
if (dex != null)
{
// Specific exception match found.
return dex;
}
// Looking for a fallback...
if (log.IsDebugEnabled)
{
log.Debug("Unable to translate exception eith errorCode '" + errorCode + "', will use the fallback translator");
}
IAdoExceptionTranslator fallback = FallbackTranslator;
if (fallback != null)
{
return FallbackTranslator.Translate(task, sql, exception);
}
// We couldn't identify it more precisely.
return new UncategorizedAdoException(task, sql, errorCode, exception);
}
/// <summary>
/// Template method for actually translating the given exception.
/// given <see cref="System.SystemException"/> into a generic data access exception.
/// </summary>
/// <param name="task">A readable string describing the task being attempted.</param>
/// <param name="sql">The SQL query or update that caused the problem. May be null.</param>
/// <param name="errorCode">The error code.</param>
/// <param name="exception">The <see cref="System.Exception"/> encountered by the ADO.NET implementation.</param>
/// <returns>
/// A <see cref="Spring.Dao.DataAccessException"/> appropriate for the supplied
/// <paramref name="exception"/>.
/// </returns>
/// <remarks>
/// The passed-in arguments will have been pre-checked. furthermore, this method is allowed to
/// return <code>null</code> to indicate that no exception match has been found and that
/// fallback translation should kick in.
/// </remarks>
protected virtual DataAccessException DoTranslate(string task, string sql, string errorCode, Exception exception)
{
if (task == null)
{
task = "";
}
if (sql == null)
{
sql = "";
}
string errorCode = ExtractErrorCode(exception);
// First, try custom translation from overridden method.
DataAccessException dex = TranslateException(task, sql, errorCode, exception);
if (dex != null)
@@ -187,6 +230,11 @@ namespace Spring.Data.Support
{
LogTranslation(task, sql, errorCode, exception, false);
return new InvalidResultSetAccessException(task, sql, exception);
}
else if (Array.IndexOf(errorCodes.DuplicateKeyCodes, errorCode) >= 0)
{
LogTranslation(task, sql, errorCode, exception, false);
return new DuplicateKeyException(task, sql, exception);
}
else if (Array.IndexOf(errorCodes.DataAccessResourceFailureCodes, errorCode) >= 0)
{
@@ -220,11 +268,7 @@ namespace Spring.Data.Support
}
}
}
if (log.IsDebugEnabled)
{
log.Debug("Unable to translate exception eith errorCode '" + errorCode + "', will use the fallback translator");
}
return fallbackTranslator.Translate(task, sql, exception);
return null;
}
/// <summary>

View File

@@ -39,7 +39,7 @@
<Optimize>false</Optimize>
<RegisterForComInterop>false</RegisterForComInterop>
<RemoveIntegerChecks>false</RemoveIntegerChecks>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningLevel>4</WarningLevel>
<DebugType>full</DebugType>
<ErrorReport>prompt</ErrorReport>
@@ -147,6 +147,7 @@
<Compile Include="Data\Common\MultiDelegatingDbProvider.cs" />
<Compile Include="Data\Common\UserCredentialsDbProvider.cs" />
<Compile Include="Data\Config\DatabaseNamespaceParser.cs" />
<Compile Include="Data\DuplicateKeyException.cs" />
<Compile Include="Data\Core\AdoAccessor.cs" />
<Compile Include="Data\Core\AdoDaoSupport.cs" />
<Compile Include="Data\Core\AdoPlatformTransactionManager.cs" />

View File

@@ -1,7 +1,7 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<ProjectType>Local</ProjectType>
<ProductVersion>9.0.21022</ProductVersion>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{AE00E5AB-C39A-436F-86D2-33BFE33E2E40}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -155,6 +155,7 @@
<Compile Include="Data\Core\RowMapperResultSetExtractor.cs" />
<Compile Include="Data\Core\ServiceDomainPlatformTransactionManager.cs" />
<Compile Include="Data\Core\TxScopeTransactionManager.cs" />
<Compile Include="Data\DuplicateKeyException.cs" />
<Compile Include="Data\Generic\AdoDaoSupport.cs" />
<Compile Include="Data\Generic\IDataAdapterCallback.cs" />
<Compile Include="Data\Generic\DataAdapterDelegate.cs" />