The JDK 19 release brings a preview of Project Loom, virtual threads for Java. As virtual threads are just Threads, Hibernate already runs on Loom without any changes. However, to utilize Loom with best efficiency, code must deal with some limitations. To avoid virtual threads pinning to carrier threads, critical sections in which the CPU may be yielded e.g. when doing I/O, should be guarded with j.u.c.locks rather than object monitors i.e. ‘synchronized’ keyword. By using a modified version of JUnit (sorry, it’s not public at this time) to run the test suite on Loom threads with {{-Djdk.tracePinnedThreads=full }}enabled, some instances of this undesirable pinning behaviour are revealed in the ORM. Fortunately these occur mainly in startup/shutdown code that is off the critical path. Nevertheless, it may be desirable to make Hibernate more Loom-friendly by refactoring to use ReentrantLock instead. The count of pinning occurrences in the test suite is dominated by the three cases below. Note that whilst test coverage is good, the short-lived nature of test environments and the default configuration used skews the data somewhat compared to production use. https://github.com/hibernate/hibernate-orm/blob/main/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java methods getService (though there is an unsynchronized fast path), stopService and destroy. https://github.com/hibernate/hibernate-orm/blob/main/hibernate-core/src/main/java/org/hibernate/boot/registry/internal/StandardServiceRegistryImpl.java methods initiateService, configureService and destroy. https://github.com/hibernate/hibernate-orm/blob/main/hibernate-core/src/main/java/org/hibernate/id/enhanced/PooledOptimizer.java method generate. The choice of JDBC driver and other components such as the logging library and connection pool are also factors in performance of Hibernate on Loom threads. Broadly speaking, the mariadb driver is well behaved, whilst the mysql and pgsql drivers have some issues. YMMV and testing in your own environment is recommended. |