我想让一个父对象删除自己,它是单个事务范围中的子对象。我还想在这两种情况下检查要删除的对象是否存在,以及用户是否有权访问该对象。考虑以下代码:
我得到服务器上的 MSDTC 不可用异常。反正有没有通过我的服务方法传递连接?
请看下面的例子:
// 类 Flight, FlightService, FlightDao // 类 Pilot, PilotService, PilotDao
// FlightService
public void deleteFlight(Flight flight) {
FlightDao flightDao = new FlightDao();
Flight existingFlight = flightDao.findById(flight.Id);
if (existingFlight != null) {
using (TransactionScope scope = new TransactionScope()) {
try {
PilotService.Instance.deletePilot(flight.Pilot);
flightDao.delete(flight);
} catch (Exception e) {
log.Error(e.Message, e);
throw new ServiceException(e.Message, e);
}
scope.Complete();
}
}
}
// PilotService
public void deleteFlight(Pilot pilot) {
PilotDao pilotDao = new PilotDao();
Pilot existingPilot = pilotDao.findById(pilot.Id); // THIS LINE RIGHT HERE THROWS EXCEPTION
if (existingPilot != null) {
using (TransactionScope scope = new TransactionScope()) {
try {
pilotDao.delete(pilot);
} catch (Exception e) {
log.Error(e.Message, e);
throw new ServiceException(e.Message, e);
}
scope.Complete();
}
}
}