我需要为 NHibernate 方言注册自定义异常。我已经实施并
注册了 ISqlExceptionConverter,如 NHibernate 测试中所示。但
当代码中抛出异常时,它不会被转换。我的转换代码
甚至不打电话。
我的代码非常简单:
try
{
using (ISession sess = OpenSession())
using (ITransaction tx = sess.BeginTransaction())
{
....
sess.Save(obj); // invalid object scheduled for inserting
.....
tx.Commit(); // exception raises here
}
}
catch (UniquenessViolationException ex)
{
// never came here, since exception was not converted and is of type
HibernateException
}
我的 ISqlExceptionConverter 实现:
public class SqlExceptionConverter : ISQLExceptionConverter
{
public Exception Convert(AdoExceptionContextInfo exInfo)
{
var sqlEx = ADOExceptionHelper.ExtractDbException
(exInfo.SqlException) as SqlException;
if (sqlEx != null)
{
if (sqlEx.Number == 2627)
return new UniquenessViolationException(exInfo.Message, sqlEx,
exInfo.Sql);
}
return SQLStateConverter.HandledNonSpecificException
(exInfo.SqlException, exInfo.Message, exInfo.Sql);
}
也许我错过了什么?
您需要注册异常转换器。
In code:
configuration.SetProperty(
Environment.SqlExceptionConverter,
typeof(SqlExceptionConverter).AssemblyQualifiedName);
在配置文件中:
<property name="sql_exception_converter">
Name.Space.SqlExceptionConverter, MyAssembly
</property>
直到现在我才尝试这个,只是查看了代码。希望它有效,我也需要它:-)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)