当我在java spring上重新连接页面时,ibatis缓存未命中

时间:2016-07-19 06:45:27

标签: java spring caching mybatis ibatis

只要缓存的数据没有被清除,我想在表被截断并为空时使用缓存的数据库数据。

我在使用iBatis的spring应用程序中设置了cachemodel 我想知道为什么我会错过' cache miss'我重新连接控制器时记录 当我重复相同查询的请求时,缓存在同一会话中工作。
但是当我重新连接页面时,
我得到了' cache miss'存储日志和缓存。

请检查我的代码并告诉我这个问题的解决方案是什么。或者结果是否正确?

的SqlMap-bnkmst.xml

<sqlMap namespace="bnkMst"> 
<cacheModel id="bnkMstCache" type="LRU" readOnly="true" serialize="false">
    <flushInterval hours="24"/>
    <property name="reference-type" value="STRONG" />        
    <property name="cache-size" value="1000" /> 
</cacheModel>

<resultMap id="bnkMstResMap" class="com.howtodoinjava.ibatis.demo.dto.bnkMstTEO">
    <result property="id" column="bnk_no" />
    <result property="knNm" column="bnk_nm_kn" />
    <result property="enNm" column="bnk_nm_en" />
    <result property="cd" column="bnk_cd" />
</resultMap>

<select id="selectAllBnkMst" resultMap="bnkMstResMap" cacheModel="bnkMstCache">
      SELECT * FROM BNK_MST
</select>
<select id="getBnkMstById" parameterClass="java.lang.Integer" resultMap="bnkMstResMap" cacheModel="bnkMstCache">
      SELECT * FROM BNK_MST WHERE BNK_NO = #value#
</select>
......omitted
......omitted
</sqlMap>

ibatisController.java

package com.howtodoinjava.ibatis.demo.controller;

......omitted

@Controller
public class ibatisController {

    @RequestMapping("/ibatis")
    public ModelAndView showMessage(
            @RequestParam(value = "id", required = false) int id) throws SQLException, IOException {
        bnkMstDAO manager = new bnkMstDAOImpl();

        Reader reader = Resources.getResourceAsReader("sql-maps-config.xml");
        SqlMapClient sqlmapClient = SqlMapClientBuilder.buildSqlMapClient (reader);     

        bnkMstTEO bean = manager.getBnkMstById(id, sqlmapClient); //1st

        ModelAndView mv = new ModelAndView("ibatisTest");
        mv.addObject("knNm", bean.getKnNm());
        mv.addObject("enNm", bean.getEnNm());

        bean = manager.getBnkMstById(id, sqlmapClient); //2nd
//......omitted
        bean = manager.getBnkMstById(id, sqlmapClient); //3rd
//......omitted


        return mv;
    }
}

bnkMstDAOImpl.java

package com.howtodoinjava.ibatis.demo.dao;

import java.sql.SQLException;
import java.util.List;

import com.howtodoinjava.ibatis.demo.dto.bnkMstTEO;
import com.ibatis.sqlmap.client.SqlMapClient;

public class bnkMstDAOImpl implements bnkMstDAO
{

    public bnkMstTEO getBnkMstById(Integer id, SqlMapClient sqlmapClient) {
        try
        {
            bnkMstTEO teo = (bnkMstTEO)sqlmapClient.queryForObject("bnkMst.getBnkMstById", id);
            return teo;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
        return null;
    }
    public List selectAllBnkMst(SqlMapClient sqlmapClient) throws SQLException{
        List list = sqlmapClient.queryForList("bnkMst.selectAllBnkMst");
        return list;
    }

    public void delBnkMstById(Integer id, SqlMapClient sqlmapClient) {
        try
        {
            sqlmapClient.delete("bnkMst.deleteBnkMstById", id);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
    }

}

这是我启动时的日志

DEBUG [http-bio-8080-exec-8] - Cache 'bnkMst.bnkMstCache': cache miss
DEBUG [http-bio-8080-exec-8] - Created connection 411106513.
DEBUG [http-bio-8080-exec-8] - {conn-100012} Connection
DEBUG [http-bio-8080-exec-8] - {conn-100012} Preparing Statement:      SELECT * FROM BNK_MST WHERE BNK_NO = ?  
DEBUG [http-bio-8080-exec-8] - {pstm-100013} Executing Statement:      SELECT * FROM BNK_MST WHERE BNK_NO = ?  
DEBUG [http-bio-8080-exec-8] - {pstm-100013} Parameters: [1]
DEBUG [http-bio-8080-exec-8] - {pstm-100013} Types: [java.lang.Integer]
DEBUG [http-bio-8080-exec-8] - {rset-100014} ResultSet
DEBUG [http-bio-8080-exec-8] - {rset-100014} Header: [bnk_no, bnk_nm_kn, bnk_nm_en, bnk_cd]
DEBUG [http-bio-8080-exec-8] - {rset-100014} Result: [1, bank1, bank1, 0001]
DEBUG [http-bio-8080-exec-8] - Cache 'bnkMst.bnkMstCache': stored object 'com.howtodoinjava.ibatis.demo.dto.bnkMstTEO@484f7153'
DEBUG [http-bio-8080-exec-8] - Returned connection 411106513 to pool.
DEBUG [http-bio-8080-exec-8] - Cache 'bnkMst.bnkMstCache': retrieved object 'com.howtodoinjava.ibatis.demo.dto.bnkMstTEO@484f7153'
DEBUG [http-bio-8080-exec-8] - Cache 'bnkMst.bnkMstCache': retrieved object 'com.howtodoinjava.ibatis.demo.dto.bnkMstTEO@484f7153'

1 个答案:

答案 0 :(得分:0)

问题是您在每个请求上都创建了SqlMapClient。缓存在SqlMapClient中进行管理,因此当它被销毁时(在您的情况下在HTTP请求结束时)缓存内容将丢失。

使用SqlMapClient的推荐方法是将其配置为应用程序中的单例。