A question about CTransaction::UpdateCoins() in 0.8.6 The logic of this function is quite simple:
1, mark inputs spent
   Call coins.Spend()
2, add new outputs to database.
   Call inputs.SetCoins()

My question is in step1, it invalidate the old output because it has been spent.

But in function coins.Spend(), I only found it kick out the spent output, I didn't find  where the function update the database. If the old output is kicked, the database should also be updated, but coins.Spend() is only update the memory data structure.
Looks for pcoinsTip->Flush(), which batch updates the underlying database CCoinsView.

It is not called for every update as that would require many writes. The way it is implemented now means many outputs can be added and marked spent entirely without needing to hit disk. The result is a disk database that is slightly out of data with the memory version, which means more work to redo when a node crashes, but this case is rare, and having faster block relay is more important.