I have two data sources: a legacy one (web service) and a database one. Now, when I process request, I made changes to both. In case of error, I want to rollback both.
try
{
legacy.Begin(); db.Begin();
legacy.MakeChanges(); db.MakeChanges();
}
except (Exception)
{
legacy.Rollback(); db.Rollback();
}
The problem is, what if legacy throws during Rollback (i.e. network error)? db.Rollback() won't be executed. And vice versa. The solution I see is:
legacy.Begin();
try
{
db.Begin();
try
{
legacy.MakeChanges(); db.MakeChanges();
}
except (Exception)
{
db.Rollback();
throw;
}
}
except (Exception)
{
legacy.Rollback();
throw;
}
Is it acceptable? Is there a better solution?