Search code examples
ajaxhibernatecoldfusion-10

Concurrent coldfusion ORM database requests fail


I'm new to coldfusion and am trying out the ORM (Hibernate, I believe, which I don't know at all)

I came across a problem when trying to call two CF pages in rapid succession. The code on the two pages is super simple:

getAppointments.cfm:

<cfscript>
    ORMReload();
    appointments = serializeJSON(EntityLoad("Appointment"));
</cfscript>

<cfoutput>#appointments#</cfoutput>

getRooms.cfm:

<cfscript>
    ORMReload();
    rooms = serializeJSON(EntityLoad("Room"));
</cfscript>

<cfoutput>#rooms#</cfoutput>

The code that I use to call them is jQuery/AJAX:

var appointments;
var rooms;

$(document).ready(function () {
    loadAppointments();
    loadRooms();
});

function loadAppointments() {
    $.ajax({
        type: 'get',
        url: 'getAppointments.cfm'
    }).done(function (response) {
        appointments = JSON.parse(response);
    }).fail(function (response) {
        var message = response.status + " - " + response.statusText;
        alert("Failed to load appointments: " + message);
    });
}

function loadRooms() {
    $.ajax({
        type: 'get',
        url: 'getRooms.cfm'
    }).done(function (jsonString) {
        rooms = JSON.parse(jsonString);
    }).fail(function (response) {
        var message = response.status + " - " + response.statusText;
        alert("Failed to load rooms: " + message);
    })
}

If I set a breakpoint to pause execution before loadRooms() is called, all is well. If I let the code run straight through I get a 500 error, so it's pretty obvious that I'm running into some sort of concurrency issue with the ORM due to the asynchronous nature of the AJAX calls.

I'm running CF on IIS (localhost), with an SQL Server database.

None of the tutorials on CF that I've seen covers this kind of scenario. I know I could defer execution inside the JS functions, but that would only be masking the underlying problem.

Can anybody point me towards a solution? Is there something similar to a C# lock available in CF?


Solution

  • You are applying ORMReload() with each call which is causing to reload all entities, clear orm cache etc.

    Please read about ORMReload(), it should only be used once whenever you make changes in your entity CFCs.