Activiti持久层是基于MyBatis框架定制了一套自己的使用方式, 处理逻辑的几个核心组件有:
- Entity: 数据对象需实现的接口
- EntityManager: 获取DataManager 和 处理事件的发布
- DataManager: 操作数据库的入口
- Session: 获取sqlSteatment 缓存或执行sql的接口
在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);
Object getPersistentState();
}
2 事件分发管理
持久层对象统一交给org.activiti.engine.impl.persistence.entity.EntityManager实现类 来管理,比如 一个重要的抽象类org.activiti.engine.impl.persistence.entity.AbstractEntityManager
public abstract class AbstractEntityManager3 数据操作封装extends AbstractManager implements EntityManager { 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)); } } }
org.activiti.engine.impl.persistence.entity.data.DataManager接口的实现类是操作数据库的入口
public abstract class AbstractDataManagerextends AbstractManager implements DataManager { @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, Map> insertedObjects
= new HashMap, Map>();//换存插入对象
protected Map, Map> deletedObjects
= new HashMap, Map>();//缓存删除对像
protected Map, List> bulkDeleteOperations
= new HashMap, List>();
protected List updatedObjects = new ArrayList();//缓存更新对象
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()); // 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方法。



