Lebowski
Lebowski

Reputation: 618

Play framework, jquery ajax call conflict

Ok, my problem is simple,

My app is running with Play framework 2.1, and sometimes I need to make several ajax requests with jQuery, which are totally independent.

Something like this :

$.get('/url1', function(res){ ... });
$.get('/url2', function(res2){ ... });

In my Play controller I'm just doing :

public Result foo(){
    theObject = // retrieving the object...
    return ok(Json.toJson(theObject));
}

What happens is that sometimes (not always, and this is why it is weird), the server returns what I need. And the others times, it give me the same result in the two callbacks, as if the last request was overriding the result of the first.

I thought it was a jQuery problem, but when I log the Result in my Play controller, before returning it, it shows me that sometimes, the first url is never called and the second is called two times in a row (and vice versa, and randomly...).

I'm using MongoDB for the database.

It seems that Play is using the same thread to handle the request or something like that...

What do you think about it ? jQuery ? Play framework ? Mongo ? Who is guilty ?

Thanks in advance !

EDIT : I just tried to do the second call in a setTimeout() callback with a delay of...0 milliseconds, and the result of this is that the results are good and there seems to be no conflict anymore... But, you'll understand that I can't wrap all my calls in a separate setTimeout() callback... Why this doesn't happen anymore with a zero milliseconds timeout ? is this a closure/javascript context issue ?

Upvotes: 2

Views: 303

Answers (1)

Mira
Mira

Reputation: 21

In my team, we faced this same issue, and found a very similar question on Play issues: https://github.com/playframework/playframework/issues/1383

The solution on the issue fixed the problem for us so far:

Play Actions are stateful, and must be instantiated once per request. By default, beans are > singleton in Spring. You need to configure them to be prototype.

Upvotes: 2

Related Questions