ibatis源码分析之---缓存

 public Object executeQueryForObject(RequestScope request, Transaction trans, Object parameterObject, Object resultObject)
        throws SQLException
    {
        CacheKey cacheKey = getCacheKey(request, parameterObject);
        cacheKey.update("executeQueryForObject");
        Object object = cacheModel.getObject(cacheKey);
        if(object == CacheModel.NULL_OBJECT)
            object = null;
        else
        if(object == null)
        {
            object = statement.executeQueryForObject(request, trans, parameterObject, resultObject);
            cacheModel.putObject(cacheKey, object);
        }
        return object;
    }

 ibatis的配置文件里面有一个cache项:

 

sqlMapConfig >  

     < settings 
       cacheModelsEnabled ="true"      
      enhancementEnabled ="true"      
      lazyLoadingEnabled ="true"      
      errorTracingEnabled ="true"      
      maxRequests ="32"           
      maxSessions ="10"           
      maxTransactions ="5"          
      useStatementNamespaces ="false"    
       />  

 

 

     cacheModelsEnabled 设置了ibatis在进行查询时是不是要进行缓存,如果设置了,那么在查询时先到缓存中查询,如果缓存中没有,那么就到数据库查询,并且把查询结果存储到缓存里面。

   ibatis里面的jdbc操作最终是由SqlMapExecutorDelegate类执行的,比如查询

  :

 

 

public Object queryForObject(SessionScope session, String id, Object paramObject, Object resultObject)
        throws SQLException
    {
        Object object;
        MappedStatement ms;
        Transaction trans;
        boolean autoStart;
        object = null;
        ms = getMappedStatement(id);
        trans = getTransaction(session);
        autoStart = trans == null;
        RequestScope request;
        trans = autoStartTransaction(session, autoStart, trans);
        request = popRequest(session, ms);
        object = ms.executeQueryForObject(request, trans, paramObject, resultObject);
        pushRequest(request);
        break MISSING_BLOCK_LABEL_85;
        Exception exception;
        exception;
        pushRequest(request);
        throw exception;
        autoCommitTransaction(session, autoStart);
        autoEndTransaction(session, autoStart);
        break MISSING_BLOCK_LABEL_114;
        Exception exception1;
        exception1;
        autoEndTransaction(session, autoStart);
        throw exception1;
        return object;
    }

 

   如果在配置文嘉里面设置了缓存,那么这时候会使用一个CachingStatement:

   

   

 public Object executeQueryForObject(RequestScope request, Transaction trans, Object parameterObject, Object resultObject)
        throws SQLException
    {
        CacheKey cacheKey = getCacheKey(request, parameterObject);
        cacheKey.update("executeQueryForObject");
        Object object = cacheModel.getObject(cacheKey);
        if(object == CacheModel.NULL_OBJECT)
            object = null;
        else
        if(object == null)
        {
            object = statement.executeQueryForObject(request, trans, parameterObject, resultObject);
            cacheModel.putObject(cacheKey, object);
        }
        return object;
    }

 

    其中的CashModel是一个线程安全的类。用来根据key取得缓存的对象。

 

你可能感兴趣的:(jdbc,cache,ibatis)