Ejemplo n.º 1
0
 private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) {
   try {
     if (parameter != null && keyStatement != null && keyStatement.getKeyProperties() != null) {
       String keyProperty =
           keyStatement.getKeyProperties()[0]; // just one key property is supported
       final Configuration configuration = ms.getConfiguration();
       final MetaObject metaParam = configuration.newMetaObject(parameter);
       if (keyProperty != null && metaParam.hasSetter(keyProperty)) {
         // Do not close keyExecutor.
         // The transaction will be closed by parent executor.
         Executor keyExecutor =
             configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);
         List<Object> values =
             keyExecutor.query(
                 keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
         if (values.size() == 0) {
           throw new ExecutorException("SelectKey returned no data.");
         } else if (values.size() > 1) {
           throw new ExecutorException("SelectKey returned more than one value.");
         } else {
           metaParam.setValue(keyProperty, values.get(0));
         }
       }
     }
   } catch (ExecutorException e) {
     throw e;
   } catch (Exception e) {
     throw new ExecutorException(
         "Error selecting key or setting result to parameter object. Cause: " + e, e);
   }
 }
  /**
   * when executing a query operation 1. record this statement's id and it's corresponding Cache
   * Object into Global Caching Manager; 2. record this statement's id and
   *
   * @param invocation
   * @return
   * @throws Throwable
   */
  private Object processQuery(Invocation invocation) throws Throwable {
    Object result = invocation.proceed();
    if (cachingManager.isCacheEnabled()) {
      Object[] args = invocation.getArgs();
      MappedStatement mappedStatement = (MappedStatement) args[0];

      // 如果本条statementId表示的查询语句配置了 flushCache=true,则清空querCacheOnCommit缓存
      if (mappedStatement.isFlushCacheRequired()) {
        queryCacheOnCommit.clear();
      }
      // 如果本条statementId表示的查询语句配置了使用缓存,并且二级缓存不为空,则将StatementId
      // 和对应的二级缓存对象映射关系添加到全局缓存映射管理器中
      if (mappedStatement.isUseCache() && mappedStatement.getCache() != null) {
        cachingManager.appendStatementCacheMap(mappedStatement.getId(), mappedStatement.getCache());
      }

      Object parameter = args[1];
      RowBounds rowBounds = (RowBounds) args[2];
      Executor executor = (Executor) invocation.getTarget();
      BoundSql boundSql = mappedStatement.getBoundSql(parameter);

      // 记录本次查询所产生的CacheKey
      CacheKey cacheKey = executor.createCacheKey(mappedStatement, parameter, rowBounds, boundSql);
      queryCacheOnCommit.putElement(mappedStatement.getId(), cacheKey);
    }

    return result;
  }
Ejemplo n.º 3
0
 public void close() throws SQLException, TransactionException {
   if (executor != null) {
     try {
       isolationLevel.restoreIsolationLevel(executor.getTransaction().getConnection());
     } finally {
       executor.close(false);
       executor = null;
     }
   }
 }
 private Object getNestedQueryConstructorValue(
     ResultSet rs, ResultMapping constructorMapping, String columnPrefix) throws SQLException {
   final String nestedQueryId = constructorMapping.getNestedQueryId();
   final MappedStatement nestedQuery = configuration.getMappedStatement(nestedQueryId);
   final Class<?> nestedQueryParameterType = nestedQuery.getParameterMap().getType();
   final Object nestedQueryParameterObject =
       prepareParameterForNestedQuery(
           rs, constructorMapping, nestedQueryParameterType, columnPrefix);
   Object value = null;
   if (nestedQueryParameterObject != null) {
     final BoundSql nestedBoundSql = nestedQuery.getBoundSql(nestedQueryParameterObject);
     final CacheKey key =
         executor.createCacheKey(
             nestedQuery, nestedQueryParameterObject, RowBounds.DEFAULT, nestedBoundSql);
     final Class<?> targetType = constructorMapping.getJavaType();
     final ResultLoader resultLoader =
         new ResultLoader(
             configuration,
             executor,
             nestedQuery,
             nestedQueryParameterObject,
             targetType,
             key,
             nestedBoundSql);
     value = resultLoader.loadResult();
   }
   return value;
 }
Ejemplo n.º 5
0
 private <E> List<E> selectList() throws SQLException {
   Executor localExecutor = executor;
   if (localExecutor.isClosed()) {
     localExecutor = newExecutor();
   }
   try {
     return localExecutor.<E>query(
         mappedStatement,
         parameterObject,
         RowBounds.DEFAULT,
         Executor.NO_RESULT_HANDLER,
         cacheKey,
         boundSql);
   } finally {
     if (localExecutor != executor) {
       localExecutor.close(false);
     }
   }
 }
Ejemplo n.º 6
0
 /**
  * @description TODO
  * @createTime 2016-4-27 下午12:33:11
  * @fileName CacheInterceptor.java
  * @author yaojiamin
  */
 private <E> List<E> executeQuery(
     CacheHandlerBean cacheHandlerBean, BoundSql boundSql, CacheKey cacheKey, Executor executor)
     throws SQLException {
   return executor.query(
       cacheHandlerBean.getMappedStatement(),
       cacheHandlerBean.getParameterObject(),
       cacheHandlerBean.getRowBounds(),
       cacheHandlerBean.getResultHandler(),
       cacheKey,
       boundSql);
 }
 private Object getNestedQueryMappingValue(
     ResultSet rs,
     MetaObject metaResultObject,
     ResultMapping propertyMapping,
     ResultLoaderMap lazyLoader,
     String columnPrefix)
     throws SQLException {
   final String nestedQueryId = propertyMapping.getNestedQueryId();
   final String property = propertyMapping.getProperty();
   final MappedStatement nestedQuery = configuration.getMappedStatement(nestedQueryId);
   final Class<?> nestedQueryParameterType = nestedQuery.getParameterMap().getType();
   final Object nestedQueryParameterObject =
       prepareParameterForNestedQuery(rs, propertyMapping, nestedQueryParameterType, columnPrefix);
   Object value = NO_VALUE;
   if (nestedQueryParameterObject != null) {
     final BoundSql nestedBoundSql = nestedQuery.getBoundSql(nestedQueryParameterObject);
     final CacheKey key =
         executor.createCacheKey(
             nestedQuery, nestedQueryParameterObject, RowBounds.DEFAULT, nestedBoundSql);
     final Class<?> targetType = propertyMapping.getJavaType();
     if (executor.isCached(nestedQuery, key)) {
       executor.deferLoad(nestedQuery, metaResultObject, property, key, targetType);
     } else {
       final ResultLoader resultLoader =
           new ResultLoader(
               configuration,
               executor,
               nestedQuery,
               nestedQueryParameterObject,
               targetType,
               key,
               nestedBoundSql);
       if (propertyMapping.isLazy()) {
         lazyLoader.addLoader(property, metaResultObject, resultLoader);
       } else {
         value = resultLoader.loadResult();
       }
     }
   }
   return value;
 }
Ejemplo n.º 8
0
 /**
  * @description TODO
  * @createTime 2016-5-31 下午4:53:32
  * @fileName CacheInterceptor.java
  * @author yaojiamin
  * @param boundSql
  * @param cacheKey
  */
 @SuppressWarnings({})
 public <E> List<E> query(Executor executor, CacheHandlerBean cacheHandlerBean) throws Exception {
   BoundSql boundSql =
       cacheHandlerBean.getMappedStatement().getBoundSql(cacheHandlerBean.getParameterObject());
   CacheKey cacheKey =
       executor.createCacheKey(
           cacheHandlerBean.getMappedStatement(),
           cacheHandlerBean.getParameterObject(),
           cacheHandlerBean.getRowBounds(),
           boundSql);
   MetaObject metaPage = forObject(cacheHandlerBean.getParameterObject());
   // 当需要分页查询时,将page参数里的当前页和每页数加到cachekey里
   if (isNeedPagination(metaPage)) {
     executeQuery(
         cacheHandlerBean,
         createPageBoundSql(cacheHandlerBean, boundSql),
         cloneCacheKey(cacheKey),
         executor);
     Assert.isTrue(metaPage.getValue(Constants.TOTAL) != null, "Fail to count result!!!");
     cacheKey.update(metaPage.getValue(Constants.PAGE_NO));
     cacheKey.update(metaPage.getValue(Constants.PAGE_SIZE));
   }
   return executeQuery(cacheHandlerBean, boundSql, cacheKey, executor);
 }
Ejemplo n.º 9
0
 public void rollback(boolean required) throws SQLException, TransactionException {
   if (executor != null) {
     executor.rollback(required);
   }
 }
Ejemplo n.º 10
0
 public void commit(boolean required) throws SQLException, TransactionException {
   if (executor != null) {
     executor.commit(required);
   }
 }