In general, SQL calls to the database are usually blocked and executed sequentially. Play has excellent support for asynchronous execution, which improves the performance of your application.
Sample working code for Play 2.0
public static Result slow() { Logger.debug("slow started"); // Start execution Promise<DataObject> userObject1 = SlowQuery.getUser(440); Promise<DataObject> userObject2 = SlowQuery.getCategory(420); // ... here execution is already in progress ... // Map to Promise Objects Promise<DataObject> res1 = userObject1.map(new Function<DataObject, DataObject>() { public DataObject apply(DataObject res) { Logger.debug("Got result (userObject1): " + res.toString()); return res; } }); Promise<DataObject> res2 = userObject2.map(new Function<DataObject, DataObject>() { public DataObject apply(DataObject res) { Logger.debug("Got result (userObject2): " + res.toString()); return res; } }); // here we wait for completion - this blocks userObject1.getWrappedPromise().await(); userObject2.getWrappedPromise().await(); // the result is available Logger.debug(res1.get().toString()); Logger.debug(res2.get().toString()); Logger.debug("slow finished"); return ok("done"); }
Feel free to improve the use of the community wiki function - I am sure that some parts can be reduced.
source share