Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce java21 artifact with initial test showing Virtual Threads at least basically work #2597

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package org.jdbi.v3.core.internal;

import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand All @@ -32,9 +33,11 @@ public class MemoizingSupplier<T> implements Supplier<T> {
private Supplier<T> delegate = this::init;
private volatile boolean initialized;
private T value;
private final ReentrantLock initializationLock;

private MemoizingSupplier(Supplier<T> create) {
this.create = create;
this.initializationLock = new ReentrantLock();
}

public static <T> MemoizingSupplier<T> of(Supplier<T> supplier) {
Expand Down Expand Up @@ -63,13 +66,16 @@ public void ifInitialized(Consumer<T> consumer) {
}

private T init() {
synchronized (this) {
initializationLock.lock();
try {
if (!initialized) {
value = create.get();
initialized = true;
delegate = this::internalGet;
}
return delegate.get();
} finally {
initializationLock.unlock();
}
}
}
5 changes: 5 additions & 0 deletions docs/src/adoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ Jdbi runs on all Java versions 11 or later. All releases are built with the late
Jdbi ended support for Java 8 with version 3.39. There are occasional backports of security relevant things or major bugs but as of version 3.40.0, JDK 11+ is required.


==== Virtual Threads

Jdbi has experimental support for virtual threads. Basic use cases are verified to work, and we welcome reports of any issues.
As virtual thread support is still evolving, please make sure to verify against the latest JDK before reporting any issues.

=== Getting started

Jdbi has a flexible plugin architecture, which makes it easy to fold in support for your favorite libraries (Guava, JodaTime, Spring, Vavr) or database vendors (Oracle, Postgres, H2).
Expand Down
75 changes: 75 additions & 0 deletions java21/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jdbi.internal</groupId>
<artifactId>jdbi3-parent</artifactId>
<version>3.45.4-SNAPSHOT</version>
</parent>

<groupId>org.jdbi</groupId>
<artifactId>jdbi3-java21</artifactId>

<name>jdbi3 - internal - java21</name>
<description>jdbi3 for Java 21 or better</description>

<properties>
<basepom.deploy.skip>true</basepom.deploy.skip>
<basepom.install.skip>true</basepom.install.skip>
<jdbi.check.skip-japicmp>true</jdbi.check.skip-japicmp>
<moduleName>org.jdbi.v3.java21</moduleName>
<project.build.targetJdk>21</project.build.targetJdk>
</properties>

<dependencies>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-testing</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-sqlobject</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
105 changes: 105 additions & 0 deletions java21/src/test/java/org/jdbi/v3/java21/TestVirtualThreads.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.
*/
package org.jdbi.v3.java21;

import java.util.concurrent.Executors;
import java.util.stream.IntStream;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
import org.jdbi.v3.sqlobject.customizer.BindMethods;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.jdbi.v3.sqlobject.transaction.Transactional;
import org.jdbi.v3.testing.junit5.JdbiExtension;
import org.jdbi.v3.testing.junit5.internal.TestingInitializers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.Assertions.assertThat;

class TestVirtualThreads {

@RegisterExtension
JdbiExtension h2Extension = JdbiExtension.h2()
.withPlugin(new SqlObjectPlugin())
.withInitializer(TestingInitializers.usersWithData());

@Test
void virtualThreads() {
final var dao = h2Extension.getJdbi().onDemand(UserDao.class);
final var inserts = IntStream.range(100, 200)
.mapToObj(id -> new User(id, "User " + id))
.toList();
try (var exec = Executors.newVirtualThreadPerTaskExecutor()) {
inserts.forEach(insert ->
exec.submit(() -> {
dao.useTransaction(txn -> {
txn.insertUser(insert);
try {
Thread.sleep((long) (Math.random() * 100));
} catch (final InterruptedException e) {
throw new AssertionError(e);
}
});
}));
}
assertThat(dao.countUsers()).isEqualTo(102);
}

@Test
void virtualThreadsAndLimitedPool() {
final var hikariCfg = new HikariConfig();
hikariCfg.setMaximumPoolSize(10);
hikariCfg.setJdbcUrl(h2Extension.getUrl());
try (var pool = new HikariDataSource(hikariCfg)) {
final var dao = Jdbi.create(pool).installPlugin(new SqlObjectPlugin()).onDemand(UserDao.class);
final var inserts = IntStream.range(100, 200)
.mapToObj(id -> new User(id, "User " + id))
.toList();
try (var exec = Executors.newVirtualThreadPerTaskExecutor()) {
inserts.forEach(insert ->
exec.submit(() -> {
dao.useTransaction(txn -> {
txn.insertUser(insert);
try {
Thread.sleep((long) (Math.random() * 100));
} catch (final InterruptedException e) {
throw new AssertionError(e);
}
});
}));
}
assertThat(dao.countUsers()).isEqualTo(102);
}
}

public record User(int id, String name) {}

interface UserDao extends Transactional<UserDao> {

@SqlQuery("""
SELECT count(1) FROM users
""")
int countUsers();

@SqlUpdate("""
INSERT INTO users (id, name)
VALUES (:id, :name)
""")
void insertUser(@BindMethods User user);
}
}
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,16 @@
</dependency>
</dependencies>
</dependencyManagement>

<profiles>
<profile>
<id>java21</id>
<activation>
<jdk>[21,)</jdk>
</activation>
<modules>
<module>java21</module>
</modules>
</profile>
</profiles>
</project>
Loading