zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    JQuery批量图片上传插件—Uploadify使用记录
    SQL2008 附加数据库提示 5120错误
    enter the full pathname for java.exe
    共享文件夹设置
    曾经运行该线程的应用程序域已卸载
    无法打开物理文件 。操作系统错误 5:"5(拒绝访问。)"。 消息 5120,级别 16,状态 101
    开篇一个码农,一个苦逼的码农生活经历
    POI 3.17 读取Excel(模板)、导出Excel
    Java微信支付开发之关闭订单
    Linux下安装 MySQL5.7.28
  • 原文地址:https://www.cnblogs.com/riyueqian/p/12120697.html
Copyright © 2011-2022 走看看