回滚不工作coldfusion交易

时间:2017-08-03 16:44:22

标签: coldfusion transactions

我正在使用带有事务的cfscript,但即使我调用了回滚,它也会将记录保存在数据库中。我在try中调用回滚而不是catch,因为我需要在那里验证它。

这是我的代码:

transaction action="begin"{
try{
    rollbackAction = false;
    // Insert into CustomerBilling
    insertCustomerBilling = new query();
    insertCustomerBilling.setDatasource("rent");
    insertCustomerBilling.setName("insertCustomerBilling");
    insertCustomerBilling.addParam(name="CustomerID",   value="#CompanyID#",    cfsqltype="cf_sql_integer");
    insertCustomerBilling.addParam(name="Cost", value="#Cost#", cfsqltype="cf_sql_decimal");
    insertCustomerBilling.addParam(name="BillDate", value="#DateFormat(Now(), 'yyyy/mm/dd')#",  cfsqltype="cf_sql_datetime");
    insertCustomerBilling.addParam(name="PaidDate", value="#DateFormat(Now(), 'yyyy/mm/dd')#",  cfsqltype="cf_sql_datetime");
    result = insertCustomerBilling.execute(sql="INSERT INTO CustomerBilling " &
                        " (CustomerID, Cost, BillDate, Paid, PaidDate, PropertyID, BillTypeID) " &
                        " VALUES(:CustomerID, :Cost, :BillDate, 0, :PaidDate, 0, 1 )");

    GetCCExemption = new query();
    GetCCExemption.setDatasource("rent");
    GetCCExemption.setName("GetCCExemption");
    GetCCExemption.addParam(name="CompanyID",   value="#CompanyID#",    cfsqltype="cf_sql_integer");
    result = GetCCExemption.execute(sql="Select * From Billing_CCExemptions Where CompanyID = :CompanyID");
    rs = result.getResult();
    if(rs.recordCount <= 0){
        // Check if we inserted customer into Stripe_Customer
        GetStripeCustomer = new query();
        GetStripeCustomer.setDatasource("rent");
        GetStripeCustomer.setName("GetStripeCustomer");
        GetStripeCustomer.addParam(name="CompanyID",    value="#CompanyID#",    cfsqltype="cf_sql_integer");
        resultSC = GetStripeCustomer.execute(sql="Select * From Stripe_Customer Where CompanyID = :CompanyID");
        rsSC = resultSC.getResult();
        if(rsSC.recordCount > 0){
            // Charge
            charge = oStripe.chargeCustomer();
        } else{
            // Notify us we haven't inserted into Stripe_Customer
            mailerService = new mail();
            mailerService.setTo('test@test.com');
            mailerService.setFrom("support@test.com");
            mailerService.setSubject("Error");
            mailerService.setType("html");
            mailBody = "ERROR";
            mailerService.send(body=mailBody);
            transaction action="rollback"; 

            rollbackAction = true;
        }
    }
    if(!rollbackAction){
        transaction action="commit"; 
    }
} catch(any e){
    transaction action="rollback";  
}

}

有谁知道为什么回滚不起作用?

由于

1 个答案:

答案 0 :(得分:2)

改变这个:

    if(!rollbackAction){
        transaction action="commit"; 
    }
} catch(any e){
    transaction action="rollback";  

到这个

if(!rollbackAction){
    transaction action="commit"; 
}
else
    transaction action="rollback";  

只有try块遇到运行时错误才会执行catch块。运行时错误意味着“没有执行”。它们与不需要的结果无关。