How to run asynchronous / non-blocking MySQL queries in the Play platform?

Just start the game. The documentation talks about how Play can run asynchronously.

But how to run MySQL queries when starting Play asynchronously? Regular MySQL queries are blocked, right? So this will not work.

Node.js has its own non-blocking MySQL clients for exactly this purpose, but I cannot find anything like it for Play.

How to execute MySQL queries in an asynchronous Play application?

+6
source share
4 answers

take a look at this link Asynchronous tasks in the gaming infrastructure.

+3
source

Play services run in a separate thread and release the main HTTP thread. Then the main HTTP stream begins, where it stopped when Job (wrapped in a Promise object) returns after completion.

Therefore, the main HTTP stream is not supported and may be available to handle other incoming HTTP requests.

+2
source

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.

0
source

You should use a non-blocking MySQL connector. For instance. https://code.google.com/p/async-mysql-connector/

0
source

Source: https://habr.com/ru/post/904469/


All Articles