Native Image with Micronaut 3.1 and MySQL

Context

best way to craft our new projects

MySQL

implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
implementation("io.micronaut.sql:micronaut-hibernate-jpa")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
runtimeOnly("mysql:mysql-connector-java")
datasources:
default:
url: jdbc:mysql://localhost/exampleDB?generateSimpleParameterMetadata=true&zeroDateTimeBehavior=convertToNull&verifyServerCertificate=false&useSSL=false
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: YOUR_MYSQL_PWD
schema-generate: CREATE_DROP
dialect: MYSQL
jpa:
default:
entity-scan:
packages: 'mn.data.mysql.domain'
properties:
hibernate:
bytecode:
provider: none
hbm2ddl:
auto: validate
show_sql: false
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ID")

private Long id;
@Column(name="NAME", nullable = false, unique = true)
@Size(max = 100)

private String name;
@Column(name="BIRTH_YEAR", nullable = false)
private Integer birthYear;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ID")

private Long id;
@Column(name="TITLE", unique = true)
@Size(max = 150)

private String title;
@Column(name="PUB_DATE")
private Instant pubDate;
@ManyToOne(targetEntity = Author.class)
@JoinColumn(name = "AUTHOR")

private Author author;

Flyway

Entity–relationship model for this vanilla example
-- V1.0__initial_schema.sql
CREATE TABLE author
(
ID BIGINT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(100) NOT NULL UNIQUE,
BIRTH_YEAR INT NOT NULL,
CONSTRAINT pk_author PRIMARY KEY (ID)
);
CREATE TABLE book
(
ID BIGINT NOT NULL AUTO_INCREMENT,
TITLE VARCHAR(150) NULL,
PUB_DATE datetime NULL,
AUTHOR BIGINT NULL,
CONSTRAINT pk_book PRIMARY KEY (ID)
);
ALTER TABLE book
ADD CONSTRAINT uc_book_title UNIQUE (TITLE);
ALTER TABLE book
ADD CONSTRAINT FK_BOOK_ON_AUTHOR FOREIGN KEY (AUTHOR) REFERENCES author (ID);
flyway:
datasources:
default:
enabled: true
baseline-on-migrate: true
locations: classpath:db/migration

TestContainers

testImplementation("org.testcontainers:mysql")
testImplementation("org.testcontainers:spock")
testImplementation("org.testcontainers:testcontainers")
datasources:
default:
url: jdbc:tc:mysql:8:///db
driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
testcontainers:
reuse:
enable: true
$ ./gradlew testBUILD SUCCESSFUL in 56s
5 actionable tasks: 3 executed, 2 up-to-date

GraalVM

dependencies {
...

compileOnly("org.graalvm.nativeimage:svm")
}
nativeImage {
args('--verbose')
imageName('mn-data-mysql')
}
$ sdk install java 21.2.0.r16-grl 
$ sdk use java 21.2.0.r16-grl
$ gu install native-image
$ ./gradlew nativeImage
    classlist:   3,199.14 ms,  0.94 GB
(cap): 3,199.71 ms, 0.94 GB
setup: 5,181.91 ms, 0.94 GB
(clinit): 4,429.36 ms, 6.76 GB
(typeflow): 62,585.25 ms, 6.76 GB
(objects): 34,938.55 ms, 6.76 GB
(features): 4,693.12 ms, 6.76 GB
analysis: 112,287.61 ms, 6.76 GB
universe: 6,905.92 ms, 6.80 GB
(parse): 4,274.53 ms, 6.80 GB
(inline): 27,546.31 ms, 6.81 GB
(compile): 50,030.18 ms, 7.14 GB
compile: 88,289.22 ms, 7.14 GB
image: 15,035.10 ms, 7.04 GB
write: 3,421.70 ms, 7.04 GB
[total]: 234,588.60 ms, 7.04 GB

BUILD SUCCESSFUL in 3m 59s
$ ./gradlew run
09:34:34.103 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 2044ms. Server Running: http://localhost:8080
$ ./build/native-image/mn-data-mysql
09:33:08.048 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 151ms. Server Running: http://localhost:8080

Conclusions

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store