From 30b3813afc978856001260acda996077db36ec3c Mon Sep 17 00:00:00 2001 From: Ben Manes Date: Thu, 3 Aug 2023 22:08:27 -0700 Subject: [PATCH] upgrade error prone and hibernate --- .../caffeine/cache/BoundedLocalCache.java | 1 + .../gradle/wrapper/gradle-wrapper.properties | 2 +- examples/hibernate/README.md | 45 +++++++++++++++++++ examples/hibernate/gradle/libs.versions.toml | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- .../examples/hibernate/Repository.java | 34 +++++--------- gradle/libs.versions.toml | 6 +-- ...errorprone-caffeine-conventions.gradle.kts | 2 + gradle/wrapper/gradle-wrapper.properties | 2 +- 9 files changed, 67 insertions(+), 29 deletions(-) create mode 100644 examples/hibernate/README.md diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java index 40decf17f4..ef4dc49b41 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java @@ -3974,6 +3974,7 @@ private Object writeReplace() { } } + @SuppressWarnings("NullableOptional") static final class BoundedPolicy implements Policy { final BoundedLocalCache cache; final Function transformer; diff --git a/examples/graal-native/gradle/wrapper/gradle-wrapper.properties b/examples/graal-native/gradle/wrapper/gradle-wrapper.properties index a16fbf5ce3..ef8345e026 100644 --- a/examples/graal-native/gradle/wrapper/gradle-wrapper.properties +++ b/examples/graal-native/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip networkTimeout=10000 zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/examples/hibernate/README.md b/examples/hibernate/README.md new file mode 100644 index 0000000000..1d5fcf4b3b --- /dev/null +++ b/examples/hibernate/README.md @@ -0,0 +1,45 @@ +The [Hibernate ORM] can be configured to use a [second-level cache] to reduce the number of accesses +to the database by caching data in memory to be shared between sessions. + +In [hibernate.conf](src/main/resources/hibernate.properties) specify the JCache provider and enable +the second-level cache. + +```ini +hibernate.cache.use_second_level_cache=true +hibernate.cache.region.factory_class=jcache +hibernate.javax.cache.provider=com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider +``` + +The caches are configured in Caffeine's [application.conf](src/main/resources/application.conf) +following the [reference format](../../jcache/src/main/resources/reference.conf). The default file +path may be overridden by setting the `hibernate.javax.cache.uri` in the previous step. + +```hocon +caffeine.jcache { + default { + policy.maximum.size = 500 + } + + # Hibernate framework caches + default-query-results-region {} + default-update-timestamps-region {} + + # Hibernate application caches + com.github.benmanes.caffeine.examples.hibernate.User {} +} +``` + +Hibernate will then manage the cache to transparently avoid database calls. + +```java +// miss on first access +sessionFactory.fromSession(session -> session.get(User.class, id)); +assertThat(sessionFactory.getStatistics().getSecondLevelCacheMissCount()).isEqualTo(1); + +// hit on lookup +sessionFactory.fromSession(session -> session.get(User.class, id)); +assertThat(sessionFactory.getStatistics().getSecondLevelCacheHitCount()).isEqualTo(1); +``` + +[Hibernate ORM]: https://hibernate.org/orm/ +[second-level cache]: https://docs.jboss.org/hibernate/orm/6.3/introduction/html_single/Hibernate_Introduction.html#second-level-cache-configuration diff --git a/examples/hibernate/gradle/libs.versions.toml b/examples/hibernate/gradle/libs.versions.toml index 70a913d9b3..dea1dcfdbf 100644 --- a/examples/hibernate/gradle/libs.versions.toml +++ b/examples/hibernate/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] caffeine = "3.1.7" h2 = "2.2.220" -hibernate = "6.2.7.Final" +hibernate = "6.3.0.CR1" junit = "5.10.0" slf4j = "2.0.7" truth = "1.1.5" diff --git a/examples/hibernate/gradle/wrapper/gradle-wrapper.properties b/examples/hibernate/gradle/wrapper/gradle-wrapper.properties index a16fbf5ce3..ef8345e026 100644 --- a/examples/hibernate/gradle/wrapper/gradle-wrapper.properties +++ b/examples/hibernate/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip networkTimeout=10000 zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/examples/hibernate/src/main/java/com/github/benmanes/caffeine/examples/hibernate/Repository.java b/examples/hibernate/src/main/java/com/github/benmanes/caffeine/examples/hibernate/Repository.java index 629d70ff30..3f7c8fa8be 100644 --- a/examples/hibernate/src/main/java/com/github/benmanes/caffeine/examples/hibernate/Repository.java +++ b/examples/hibernate/src/main/java/com/github/benmanes/caffeine/examples/hibernate/Repository.java @@ -32,49 +32,39 @@ public Repository(SessionFactory sessionFactory) { } public Project getProject(long id) { - try (var session = sessionFactory.openSession()) { - return session.get(Project.class, id); - } + return sessionFactory.fromSession(session -> session.get(Project.class, id)); } public User getUser(long id) { - try (var session = sessionFactory.openSession()) { - return session.get(User.class, id); - } + return sessionFactory.fromSession(session -> session.get(User.class, id)); } public Project findProject(long id) { - try (var session = sessionFactory.openSession()) { - var query = session.createQuery("FROM Project WHERE id = :id", Project.class); - query.setParameter("id", id); - return query.uniqueResult(); - } + return sessionFactory.fromSession(session -> + session.createQuery("FROM Project WHERE id = :id", Project.class) + .setParameter("id", id) + .uniqueResult()); } public List findProjects() { - try (var session = sessionFactory.openSession()) { - return session.createQuery("FROM Project", Project.class).list(); - } + return sessionFactory.fromSession(session -> + session.createQuery("FROM Project", Project.class).list()); } public void updateProject(long id, String name) { - try (var session = sessionFactory.openSession()) { - var txn = session.beginTransaction(); + sessionFactory.inTransaction(session -> { var project = session.get(Project.class, id); project.setName(name); session.merge(project); - txn.commit(); - } + }); } public void persist(User user, Project project, Skill skill) { - try (var session = sessionFactory.openSession()) { - var txn = session.beginTransaction(); + sessionFactory.inTransaction(session -> { session.persist(project); session.persist(skill); session.persist(user); - txn.commit(); - } + }); } public void evictProject(long projectId) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d6bf527e43..2e20a0db4c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -22,7 +22,7 @@ coveralls = "2.12.2" dependency-check = "8.3.1" eclipse-collections = "12.0.0.M2" ehcache3 = "3.10.8" -errorprone-core = "2.20.0" +errorprone-core = "2.21.0" errorprone-plugin = "3.1.0" errorprone-support = "0.12.0" expiring-map = "0.5.10" @@ -50,7 +50,7 @@ jcache = "1.1.1" jcommander = "1.82" jctools = "4.0.1" jfreechart = "1.5.4" -jmh-core = "1.36" +jmh-core = "1.37" jmh-plugin = "0.7.1" jmh-report = "0.9.0" joor = "0.9.14" @@ -81,7 +81,7 @@ snakeyaml = "2.0" sonarqube = "4.3.0.3225" spotbugs-contrib = "7.6.0" spotbugs-core = "4.7.3" -spotbugs-plugin = "5.1.0" +spotbugs-plugin = "5.1.1" stream = "2.9.8" tcache = "2.0.1" testng = "7.8.0" diff --git a/gradle/plugins/src/main/kotlin/quality/errorprone-caffeine-conventions.gradle.kts b/gradle/plugins/src/main/kotlin/quality/errorprone-caffeine-conventions.gradle.kts index 309c06ee15..4260f63b78 100644 --- a/gradle/plugins/src/main/kotlin/quality/errorprone-caffeine-conventions.gradle.kts +++ b/gradle/plugins/src/main/kotlin/quality/errorprone-caffeine-conventions.gradle.kts @@ -110,7 +110,9 @@ val enabledChecks = listOf( "MissingDefault", "MixedArrayDimensions", "MissingDefault", + "MutableGuiceModule", "NoAllocation", + "OverridingMethodInconsistentArgumentNamesChecker", "PackageLocation", "PreferredInterfaceType", "PreferJavaTimeOverload", diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a19ef1eb4f..a42ae1e7a1 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,4 @@ -distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME