Skip to content

Instantly share code, notes, and snippets.

@mp911de
Last active Oct 19, 2020
Embed
What would you like to do?
Spring Data R2DBC with MySQL via jasync-sql
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import io.r2dbc.spi.ConnectionFactory;
import java.nio.charset.StandardCharsets;
import com.github.jasync.r2dbc.mysql.JasyncConnectionFactory;
import com.github.jasync.sql.db.Configuration;
import com.github.jasync.sql.db.mysql.pool.MySQLConnectionFactory;
import com.github.jasync.sql.db.mysql.util.URLParser;
import reactor.test.StepVerifier;
import org.springframework.data.r2dbc.function.DatabaseClient;
/**
* @author Mark Paluch
*/
public class HelloMySQL {
public static void main(String[] args) {
Configuration configuration = URLParser.INSTANCE.parseOrDie("mysql://root:my-secret-pw@localhost:3306/mysql", StandardCharsets.UTF_8);
ConnectionFactory connectionFactory = new JasyncConnectionFactory(new MySQLConnectionFactory(configuration));
DatabaseClient client = DatabaseClient.create(connectionFactory);
client.execute()
.sql("SELECT * FROM help_topic ORDER BY name")
.fetch()
.all()
.collectList()
.doOnNext(it -> System.out
.println("Retrieved number of help topics: " + it.size()))
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.examle</groupId>
<artifactId>spring-data-r2dbc-mysql-jasync</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-bom</artifactId>
<version>Arabba-M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>1.0.0.gh-75-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.jasync-sql</groupId>
<artifactId>jasync-r2dbc-mysql</artifactId>
<version>0.9.32</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-libs-snapshot</id>
<url>https://repo.spring.io/libs-snapshot</url>
</repository>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
</project>
@digimon1740

This comment has been minimized.

Copy link

@digimon1740 digimon1740 commented Oct 19, 2020

is this using Connection pool or just making connection when call this api?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment