• hibernate二级缓存 。高并发


    sessesionFactory配置

    <property name="hibernateProperties">  
                <props>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>   
                    <!-- 是否开启二级缓存 -->
                    <prop key="hibernate.cache.use_second_level_cache">true</prop>  
                    <!-- 是否启用查询缓存 -->  
                    <prop key="hibernate.cache.use_query_cache">true</prop>  
                    <!-- 配置二级缓存提供商 -->
                    <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>  
                     <!-- 配置 ehcache.xml的路径-->
                    <prop key="hibernate.net.sf.ehcache.configurationResourceName">classpath:ehcache.xml</prop>  
                </props>  
            </property>

    ehcache.xml

    <?xml version="1.0" encoding="UTF-8"?>    
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
        xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"    
        updateCheck="false">  
        <!-- DISKStury路径必须配置,不然报错 -->
        <diskStore path="D:sourceehcache"/>  
        <!-- 数据过期策略  如果开启查询缓存就必须开启 -->  
        <defaultCache  
            maxElementsInMemory="10000"
            eternal="false"  
            timeToIdleSeconds="600"  
            timeToLiveSeconds="600"  
            overflowToDisk="true"  
            />
        <!-- 具体到某个类的数据过期策略 -->  
        <cache name="com.gxuwz.labasswork.model.entity.SysUser"  
            maxElementsInMemory="10000"  
            eternal="false"  
            timeToIdleSeconds="300"  
            timeToLiveSeconds="600"  
            overflowToDisk="true"  
            />  

    </ehcache>  

    在hbm.xml文件中加入<cache usage="read-write" region="com.gxuwz.labasswork.model.entity.SysUser" />

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <!--
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com.gxuwz.labasswork.model.entity.SysUser" table="sys_user" catalog="lab_ass_work">
        <cache usage="read-write" region="com.gxuwz.labasswork.model.entity.SysUser" />
            <id name="id" type="java.lang.Integer">
                <column name="id" />
                <generator class="native" />
            </id>
            <property name="userName" type="java.lang.String">
                <column name="user_name" length="30" not-null="true" unique="true" />
            </property>
            <property name="userPwd" type="java.lang.String">
                <column name="user_pwd" length="30" />
            </property>
            <property name="userType" type="java.lang.String">
                <column name="user_type" length="10" />
            </property>
            <property name="stuNo" type="java.lang.String">
                <column name="stu_no" length="30" unique="true" />
            </property>
            <property name="stuName" type="java.lang.String">
                <column name="stu_name" length="30" />
            </property>
            <property name="major" type="java.lang.String">
                <column name="major" length="30" />
            </property>
            <property name="institute" type="java.lang.String">
                <column name="institute" length="30" />
            </property>
            <property name="classes" type="java.lang.String">
                <column name="classes" length="30" />
            </property>
            <property name="grade" type="java.lang.String">
                <column name="grade" length="4" />
            </property>
            <property name="phone" type="java.lang.String">
                <column name="phone" length="11" />
            </property>
        </class>
    </hibernate-mapping>

    二级缓存当使用load()、get();查询时程序只发送一条sql语句

    查询缓存

    在使用query.list()之前,必须要开启query.setCacheable(true);

  • 相关阅读:
    python-----贴图 和 报错:OSError: image file is truncated (8 bytes not processed)的处理
    springboot集成RabbitMQ
    MySQL数据库设计规范
    腾讯云COS对象存储
    腾讯云OCR图片文字识别
    java基础之 java注释
    centos7下自动备份mysql数据库
    nginx配置ssl证书
    java基础之 控制语句
    js -- 操作sqlite数据库
  • 原文地址:https://www.cnblogs.com/riyueqian/p/12120697.html
Copyright © 2020-2023  润新知