Activiti源码——持久化模型

一 概述     

        Activiti持久层是基于MyBatis框架定制了一套自己的使用方式, 处理逻辑的几个核心组件有:

  •  Entity:  数据对象需实现的接口
  •  EntityManager:  获取DataManager 和 处理事件的发布
  •  DataManager:  操作数据库的入口
  • Session:  获取sqlSteatment 缓存或执行sql的接口

二 源码详解

     1  持久层对象的封装

        在activiti中持久层所有的操作模型都需要实现org.activiti.engine.impl.persistence.entity.Entity接口

public interface Entity {

  String getId();

  void setId(String id);
  
  boolean isInserted();
  
  void setInserted(boolean inserted);
  
  boolean isUpdated();
  
  void setUpdated(boolean updated);
  
  boolean isDeleted();
  
  void setDeleted(boolean deleted);
  
  /**
   * Returns a representation of the object, as would be stored in the database. 
   * Used when deciding if updates have occurred to the object or not since it was last loaded.
   */
  Object getPersistentState();
}

      2   事件分发管理

  持久层对象统一交给org.activiti.engine.impl.persistence.entity.EntityManager实现类 来管理,比如 一个重要的抽象类org.activiti.engine.impl.persistence.entity.AbstractEntityManager

public abstract class AbstractEntityManager<EntityImpl extends Entity> extends AbstractManager implements EntityManager<EntityImpl> {
    public AbstractEntityManager(ProcessEngineConfigurationImpl processEngineConfiguration)     {
        super(processEngineConfiguration);
  }

 @Override
  public void insert(EntityImpl entity) {
    insert(entity, true);
  }

      @Override
  public void insert(EntityImpl entity, boolean fireCreateEvent) {
    getDataManager().insert(entity);

    ActivitiEventDispatcher eventDispatcher = getEventDispatcher();
    if (fireCreateEvent && eventDispatcher.isEnabled()) {
      eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_CREATED, entity));
      eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_INITIALIZED, entity));
    }
  }
}

      3  数据操作封装            

org.activiti.engine.impl.persistence.entity.data.DataManager接口的实现类是操作数据库的入口

public abstract class AbstractDataManager<EntityImpl extends Entity> extends AbstractManager implements DataManager<EntityImpl> {
    @Override
  public void insert(EntityImpl entity) {
    getDbSqlSession().insert(entity);
  }
  
  public EntityImpl update(EntityImpl entity) {
    getDbSqlSession().update(entity);
    return entity;
  }
  
  @Override
  public void delete(String id) {
    EntityImpl entity = findById(id);
    delete(entity);
  }
  
  @Override
  public void delete(EntityImpl entity) {
    getDbSqlSession().delete(entity);
  }

//...
}

        activiti的sqlSession是由SessionFactory创建的, 引擎内部持有各类SessionFactory实现,用户也能够自定义注册本身的SessionFactory实现。

package org.activiti.engine.impl.interceptor;


public interface SessionFactory {

  Class<?> getSessionType();

  Session openSession(CommandContext commandContext);

}

       4  集中提交

                dbSqlSession的实现片段

public class DbSqlSession implements Session {
  protected SqlSession sqlSession;
  protected DbSqlSessionFactory dbSqlSessionFactory;
  protected EntityCache entityCache;
  
  protected Map<Class<? extends Entity>, Map<String, Entity>> insertedObjects 
    = new HashMap<Class<? extends Entity>, Map<String, Entity>>();//换存插入对象
  protected Map<Class<? extends Entity>, Map<String, Entity>> deletedObjects 
    = new HashMap<Class<? extends Entity>, Map<String, Entity>>();//缓存删除对像
  protected Map<Class<? extends Entity>, List<BulkDeleteOperation>> bulkDeleteOperations
    = new HashMap<Class<? extends Entity>, List<BulkDeleteOperation>>();
  protected List<Entity> updatedObjects = new ArrayList<Entity>();//缓存更新对象

public void insert(Entity entity) {
    if (entity.getId() == null) {//实体是否有id, 没有则生成一个
      String id = dbSqlSessionFactory.getIdGenerator().getNextId();
      entity.setId(id);
    }
    
    Class<? extends Entity> clazz = entity.getClass();
    if (!insertedObjects.containsKey(clazz)) {//判断该类型对象是否存在,不存在则缓存新增
    	insertedObjects.put(clazz, new LinkedHashMap<String, Entity>()); // order of insert is important, hence LinkedHashMap
    }
    //将对像放入缓存map,
    insertedObjects.get(clazz).put(entity.getId(), entity);
    entityCache.put(entity, false); // False -> entity is inserted, so always changed
    entity.setInserted(true);
  }

  public void flush() {
    determineUpdatedObjects(); // Needs to be done before the removeUnnecessaryOperations, as removeUnnecessaryOperations will remove stuff from the cache
    removeUnnecessaryOperations();

    if (log.isDebugEnabled()) {
      debugFlush();
    }

    flushInserts();
    flushUpdates();
    flushDeletes();
  }

}

上面的代码涉及到了集中提交, 增删改查并没有直接操作数据库, 而是将对象缓存在sqlSession中, sqlSession又是从当前线程的CommandContext中获取的,当一个命令执行完毕后,最终命令上下文CommandContext的close方法会被调用。当执行CommandContext.close()方法时,其内部会按照顺序执行flushSessions,closeSessions方法。

        

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值