Failure recovery optimization is one important way for enhancing efficiency of long running transaction (LRT) processing. In this paper, aiming at the efficiency problem of LRT failure recovery, LHM (long running transaction hierarchical model), a hierarchical model for LRTs, is established, which divides LRTs into a series of sub-transactions in different levels. LHM supports versatile transaction properties of LRT and provides techniques for processing branch and loop structures of LRTs. Based on LHM, LHFR (LRT hierarchical failure recovery), a hierarchical failure recovery algorithm is proposed. This algorithm uses methods of compensation and functional equivalent replacement. It supports auto-recovery of failures during the execution of LRTs. LHFR algorithm can guarantee long businesss semantic atomicity property and durability property. By restricting the compensation scope in lower level of complex LRTs, LHFR limits the quantity of sub-transactions to be compensated. Thus, it reduces unnecessary loss of time and enhances the efficiency of failure recovery. Also presented is a comprehensive simulation, which confirms the accuracy and high efficiency of LHFR algorithm. Experiment results show that LHFR can reduce the time required for failure recovery. The results also illustrate that LHFR can decrease the probability of manual intervention required by sub-transactions that are unable to compensate.