Skip to content

Getting started

David D'Amico edited this page Dec 14, 2021 · 3 revisions

Just start by instantiating a Credentials object.

Credentials credentials = Credentials.builder()
  .host("yourHostName")
  .password("yourPassword")
  .user("yourUsername")
  .schema("yourDatabase")
  .pool("yourPoolName")
  //.port(3306) you can avoid specifying the port (default is 3306)
  .build();

Working Asynchronously

If you are working synchronously, you can skip this section.

Implementing Executor

In order to work, the AsyncDatabase class needs an implementation of the Executor interface. Internally, each data access operation is a task, implemented via Runnable tasks.
Such tasks are executed by your Executor implementation.

A very simple implementation:

Executor exe = (command) -> new Thread(command).start();

If the environment you are working with has strictly rules for async computation, then you may need to use its scheduler.

For example:

Executor exe = YourEnvironmentScheduler::schedule;

Here are some examples if you are working with Minecraft plugin-related projects.

Spigot:

Executor exe = (command) -> Bukkit.getScheduler().runTaskAsynchronously(plugin, command);

Bungeecord:

Executor exe = (command) -> getProxy().getScheduler().runAsync(plugin, command);

Velocity:

Executor exe = (command) -> yourProxyServer.getScheduler().buildTask(plugin, command).schedule();

If you are using Sponge, you can get a ready-to-use Executor service called SpongeExecutorService:

SpongeExecutorService exe = Sponge.getScheduler().createAsyncExecutor(plugin);

Read more about task scheduling on Sponge.

Instantiating AsyncDatabase

Finally, we are ready to use the AsyncDatabase class to access the database. You can get its instance by doing:

AsyncDatabase asyncDb = Mystral.newAsyncDatabase(credentials, exe);

The AsyncDatabase class has a lot of methods, which can perform data access operations given callback objects. Anyway, you don't need to do such complicated things: these methods are heavily overloaded. Each overload gives different combination of parameters, until we get methods which don't need callback objects, because default callback implementations are already provided internally.

Remember that every data access method returns a CompletableFuture object. You must invoke the whenComplete method when accessing the result(s), which could be null.

Working synchronously

If you need to work synchronously, just use the Database class.

Database db = Mystral.newDatabase(credentials);

This class' usage is almost as similar as its asynchronous counterpart. The main differences are listed in the table below.


Differences between usages
Sync Async
What does it returns? The raw result. It could be null. A never null CompletableFuture object which wraps the result (it could be null).
How do I handle exceptions? Exceptions in sync usage are unchecked. You don't have to use a try-catch block if it isn't needed. You can handle exceptions inside the whenComplete method.
Who carries out the operations? The thread where methods are called from. The Executor implementation.