为catch子句中已存在的SqlTransaction RollBack实现错误处理的最佳方法是什么?我的代码大致如下:
using (SqlConnection objSqlConn = new SqlConnection(connStr)) {
objSqlConn.Open();
using (SqlTransaction objSqlTrans = objSqlConn.BeginTransaction()) {
try {
// code
// more code
// and more code
}
catch (Exception ex) {
// What happens if RollBack() has an exception?
objSqlTrans.Rollback();
throw ex;
}
}
}
我相信我的应用程序在try块中有一个异常,后者又被catch块捕获,然后尝试了RollBack。但是,我看到的错误说明了SqlTransaction.ZombieCheck(),这让我想知道RollBack()本身是否也引发了异常。那么,我需要在RollBack()中实现某种类型的错误处理吗?我该怎么做并设法保持将执行放入catch块的异常?
编辑 - 我的所有代码:
using (SqlConnection objSqlConn = new SqlConnection(connStr)) {
objSqlConn.Open();
// Begin Transaction
using (SqlTransaction objSqlTrans = objSqlConn.BeginTransaction()) {
try {
// Create file in db (which in turn creates it on disk according to where the
// ...FileStream points)
SqlCommand objSqlCmd = new SqlCommand("usp_FileAdd", objSqlConn, objSqlTrans);
objSqlCmd.CommandType = CommandType.StoredProcedure;
// Sql parameter - report name
SqlParameter objSqlParam1 = new SqlParameter("@ObjectID", SqlDbType.Int);
objSqlParam1.Value = objID;
// Sql out parameter - returns the file path
SqlParameter objSqlParamOutput = new SqlParameter("@filepath", SqlDbType.VarChar, -1);
objSqlParamOutput.Direction = ParameterDirection.Output;
// Add Sql parameters to command obj
objSqlCmd.Parameters.Add(objSqlParam1);
objSqlCmd.Parameters.Add(objSqlParamOutput);
// Execute command object
objSqlCmd.ExecuteNonQuery();
// Path to the FileStream
string path = objSqlCmd.Parameters["@filepath"].Value.ToString();
// Reset command object to get FileStream
objSqlCmd = new SqlCommand(
"SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()",
objSqlConn,
objSqlTrans);
// Execute command object
Object obj = objSqlCmd.ExecuteScalar();
if (obj != DBNull.Value) {
// Byte array representing the FileStream
byte[] fsBytes = (byte[])obj;
SqlFileStream sqlFS = new SqlFileStream(path, fsBytes, FileAccess.Write);
using (FileStream fs = fi.OpenRead()) {
//byte[] b = new byte[1024];
byte[] b = new byte[4096];
int read;
fs.Seek(0, SeekOrigin.Begin);
while ((read = fs.Read(b, 0, b.Length)) > 0) {
sqlFS.Write(b, 0, read);
}
}
sqlFS.Close();
}
// Commit the transaction
objSqlTrans.Commit();
}
catch (Exception ex) {
objSqlTrans.Rollback();
throw ex;
}
}
}
答案 0 :(得分:1)
这个片段应如下所示:
using (SqlConnection objSqlConn = new SqlConnection(connStr)) {
objSqlConn.Open();
using (SqlTransaction objSqlTrans = objSqlConn.BeginTransaction()) {
try {
// code
// more code
// and more code
}
catch (Exception ex) {
// What happens if RollBack() has an exception?
try {
objSqlTrans.Rollback();
} catch (Exception ex2) {
/* can't roll back -- db gone? db will do it for us since we didn't commit. */
}
throw;
}
}
}
编辑:想想看,在这种特殊情况下根本不需要整个try / catch,因为用未提交的事务关闭连接会回滚事务,所以块看起来像这样:
using (SqlConnection objSqlConn = new SqlConnection(connStr)) {
objSqlConn.Open();
using (SqlTransaction objSqlTrans = objSqlConn.BeginTransaction()) {
// code
// more code
// and more code
}
}
答案 1 :(得分:1)
你已经有了
using (SqlTransaction objSqlTrans = objSqlConn.BeginTransaction())
如果尚未提交,则当使用块结束时,这将导致事务回滚。
所以我会完全删除catch块。
至于回滚失败时会发生什么,我首先要认识到这是一个非常糟糕的情况,并遵循Eric Lippert关于类似问题的建议。 here