<dependency>
<groupId>com.twitter</groupId>
<artifactId>finagle-mysql_2.11</artifactId>
<version>21.11.0</version>
</dependency>
import com.twitter.finagle.mysql.Client;
import com.twitter.finagle.mysql.TransactionBuilder;
import com.twitter.finagle.mysql.Query;
import com.twitter.finagle.mysql.Result;
Client client = Mysql.client
.withCredentials("<username>", "<password>")
.newRichClient("<host>:<port>");
Query query = Query.apply("SELECT * FROM my_table");
Future<Result> resultFuture = client.read(query);
resultFuture.onSuccess(result -> {
// ...
});
resultFuture.onFailure(throwable -> {
// ...
});
TransactionBuilder transaction = client.transactionBuilder();
transaction
.addQuery(Query.apply("INSERT INTO my_table (col1, col2) VALUES (?, ?)", value1, value2))
.addQuery(Query.apply("UPDATE my_table SET col3 = ? WHERE col1 = ?", value3, value1))
.commit()
.onSuccess(result -> {
})
.onFailure(throwable -> {
transaction.rollback();
});
Pool<Proxy<ClientEndpoint, ClientEndpoint>, Client> pool = Mysql.client
.withCredentials("<username>", "<password>")
.newRichClient("<host>:<port>")
.toBuilder()
.pool();
Future<Client> clientFuture = pool.apply();
clientFuture.onSuccess(client -> {
// ...
});
clientFuture.onFailure(throwable -> {
// ...
});