Hi all,
Assume we have two entity class.
Class1:
Name: House
Property:
ID:int
Name:String
Desktops:Desktop[]
Class2:
Name: Desktop
And we have two corresponding Database Managers: HouseDBManager and
DesktopManager
If we want to insert a House instance into database, we need to call
HouseDBManager#Insert(house).
And the general step is inserting ID and Name into database, then insert
Desktops though DesktopManager.
HouseDBManager#Insert(House house){
Execute "Insert into House values(ID,NAME)"
foreach ( Desktop desktop in house.Desktops ){
desktopManager.Insert(desktop);
}
}
My question is how to ensure thread-safety? If the house holds five
desktops, inserting the first 4 runs successfully, but inserting the last
one fails. Since inserting desktop is achieved by DesktopManager,
transaction will already be committed after inserting each desktop, how can
we rollback the inserting on the first four desktops.
Maybe sharing an IDbTransaction and IDbCommand between different managers is
a feasible solution. But it looks strange and I want to know whether there
is better solution to achieve this goal.
Thank you.