Hibernate 应用中的二级缓存不起作用

问题描述 投票:0回答:0

我正在尝试连接并测试休眠应用程序中的二级缓存。看来我设法连接了必要的依赖项并设置了配置文件。但是检查时缓存本身不起作用,新会话向数据库进行第二次请求。 依赖项:

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.2.0.CR2</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jcache</artifactId>
            <version>6.2.0.CR2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>3.9.7</version>
        </dependency>

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/online_store?useSSL=false</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.charsetEncoding">utf8</property>
        <property name="show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hbm2ddl.auto">update</property>
        <!--to enable SessionFactory level cache (or second-level cache) -->
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.jcache.internal.JCacheRegionFactory</property>
        <property name="hibernate.javax.cache.missing_cache_strategy">create</property>
        <mapping class="entities.User"/>
        <mapping class="entities.Product"/>
        <mapping class="entities.Order"/>
        <mapping class="entities.TestEntity"/>
    </session-factory>
</hibernate-configuration>

测试方法:

    @Test
    public void secondLevelCacheOn() {
        TestEntity test1;
        TestEntity test2;
        try (Session session = HibernateSessionFactory.getSessionFactory().openSession()) {
            session.setCacheMode(CacheMode.PUT);
            test1 = session.find(TestEntity.class, 1);
        }
        try (Session session = HibernateSessionFactory.getSessionFactory().openSession()) {
            session.setCacheMode(CacheMode.GET);
            test2 = session.find(TestEntity.class, 1);
        }
        System.out.println(test1.equals(test2));
        System.out.println(test1 == test2);
        System.out.println(test1);
        System.out.println(test2);
        assertThat(test1 == test2).isTrue();
    }

结果:

мар. 26, 2023 10:45:32 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.2.0.CR2
мар. 26, 2023 10:45:32 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000406: Using bytecode reflection optimizer
мар. 26, 2023 10:45:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using built-in connection pool (not intended for production use)
мар. 26, 2023 10:45:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: Loaded JDBC driver class: com.mysql.cj.jdbc.Driver
мар. 26, 2023 10:45:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001012: Connecting with JDBC URL [jdbc:mysql://localhost:3306/online_store?useSSL=false]
мар. 26, 2023 10:45:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, charsetEncoding=utf8, user=root}
мар. 26, 2023 10:45:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
мар. 26, 2023 10:45:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH10001115: Connection pool size: 20 (min=1)
мар. 26, 2023 10:45:33 PM org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl logSelectedDialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
мар. 26, 2023 10:45:33 PM org.hibernate.bytecode.internal.BytecodeProviderInitiator buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : bytebuddy
[main] INFO org.ehcache.core.EhcacheManager - Cache 'entities.User.orders' created in EhcacheManager.
[main] INFO org.ehcache.core.EhcacheManager - Cache 'entities.Order' created in EhcacheManager.
[main] INFO org.ehcache.core.EhcacheManager - Cache 'entities.Order.products' created in EhcacheManager.
[main] INFO org.ehcache.core.EhcacheManager - Cache 'entities.TestEntity' created in EhcacheManager.
[main] INFO org.ehcache.core.EhcacheManager - Cache 'entities.User' created in EhcacheManager.
[main] INFO org.ehcache.core.EhcacheManager - Cache 'entities.Product' created in EhcacheManager.
мар. 26, 2023 10:45:34 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@4e3283f6] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: 
    alter table Customer 
       modify column creation_time  datetime(6)
Hibernate: 
    alter table Customer 
       modify column update_time  datetime(6)
Hibernate: 
    alter table Customer_Order 
       modify column creation_time  datetime(6)
Hibernate: 
    alter table Product 
       modify column addition_time  datetime(6)
Hibernate: 
    alter table Product 
       modify column category  enum ('Headphones','Laptop','Phone','Stereo','TV','Tablet','Watches')
Hibernate: 
    alter table Product 
       modify column photo  BLOB
Hibernate: 
    alter table Product 
       modify column update_time  datetime(6)
Hibernate: 
    select
        t1_0.id,
        t1_0.name 
    from
        Test_Entity t1_0 
    where
        t1_0.id=?
true
false
entities.TestEntity@40bf064
entities.TestEntity@40bf064

org.junit.ComparisonFailure: 
Expecting value to be true but was false 
Expected :true
Actual   :false

我尝试为会话设置不同的缓存模式,但这没有帮助。 我将不胜感激任何提示

java hibernate second-level-cache maven-dependency hibernate-session
© www.soinside.com 2019 - 2024. All rights reserved.