You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Alfresco Process Services中DbSqlSessionFactory重写后自定义方法未执行求助

问题:自定义DbSqlSession方法未被执行(Alfresco Process Services 24.2)

我正在使用Alfresco Process Services 24.2,Spring版本为6.1.5。我通过自定义代码重写了DbSqlSessionFactory和DbSqlSession,编译部署后流程能正常创建执行,但CustomDbSqlSession中重写的方法始终没被触发,调试发现还是调用了默认的DbSqlSession方法。

自定义代码实现

CustomDbSqlSessionFactory

package com.activiti.extension.bean.extension.db;

import lombok.RequiredArgsConstructor;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.impl.db.DbSqlSession;
import org.activiti.engine.impl.db.DbSqlSessionFactory;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.interceptor.Session;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

@Component
@RequiredArgsConstructor
public class CustomDbSqlSessionFactory extends DbSqlSessionFactory {

    private final ObjectFactory<CustomDbSqlSession> dbSqlSession;

    @Override
    public Session openSession(CommandContext commandContext) {
        return super.openSession(commandContext);
    }

}

CustomDbSqlSession

package com.activiti.extension.bean.extension.db;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.impl.db.DbSqlSession;
import org.activiti.engine.impl.persistence.entity.Entity;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

@Slf4j
@Component
@Scope(SCOPE_PROTOTYPE)
public class CustomDbSqlSession extends DbSqlSession {

    public CustomDbSqlSession(CustomDbSqlSessionFactory dbSqlSessionFactory, CustomEntityCache entityCache) {
        super(dbSqlSessionFactory, entityCache);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public List selectListWithRawParameter(String statement, Object parameter, int firstResult, int maxResults) {
        return super.selectListWithRawParameter(statement, parameter, firstResult, maxResults);

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public List selectListWithRawParameterWithoutFilter(String statement, Object parameter, int firstResult, int maxResults) {
       return super.selectListWithRawParameterWithoutFilter(statement, parameter, firstResult, maxResults);

    }

    @Override
    protected void flushRegularInsert(Entity persistentObject, Class<? extends Entity> clazz) {
        super.flushRegularInsert(persistentObject, clazz);
    }

    @Override
    protected void flushBulkInsert(Collection<Entity> persistentObjectList, Class<? extends Entity> clazz) {
        super.flushBulkInsert(persistentObjectList, clazz);
    }

    @Override
    protected void flushUpdates() {
        super.flushUpdates();
    }
}

解决方案

1. 修复CustomDbSqlSessionFactory的openSession方法

问题核心在于你的openSession方法直接调用父类实现,没有返回自定义的CustomDbSqlSession实例,框架自然会使用默认的DbSqlSession。修改如下:

@Component
@RequiredArgsConstructor
public class CustomDbSqlSessionFactory extends DbSqlSessionFactory {

    private final ObjectFactory<CustomDbSqlSession> dbSqlSession;

    @Override
    public Session openSession(CommandContext commandContext) {
        CustomDbSqlSession customSession = dbSqlSession.getObject();
        // 复制父类的初始化逻辑(父类initSession为private,无法直接调用)
        customSession.setSqlSessionFactory(this);
        customSession.setDbSqlSessionFactory(this);
        customSession.setSqlSession(getSqlSession(commandContext));
        customSession.setEntityCache(getEntityCache());
        customSession.setIdGenerator(getIdGenerator());
        customSession.setDatabaseType(getDatabaseType());
        customSession.setTablePrefix(getTablePrefix());
        customSession.setIsDbIdentityUsed(isDbIdentityUsed());
        return customSession;
    }
}

2. 确保自定义Factory被框架优先加载

在Spring配置中,给自定义的DbSqlSessionFactory添加@Primary注解,确保框架使用你的实现而非默认:

@Configuration
public class ActivitiCustomConfig {

    @Bean
    @Primary
    public DbSqlSessionFactory customDbSqlSessionFactory(ObjectFactory<CustomDbSqlSession> dbSqlSession) {
        return new CustomDbSqlSessionFactory(dbSqlSession);
    }
}

3. 验证CustomDbSqlSession的构造函数

确保CustomEntityCache已正确注册为Spring Bean,且构造函数能被Spring正常实例化。同时检查@Slf4j的lombok依赖是否配置正确,避免编译时缺失日志实例。

4. 调试验证

在CustomDbSqlSession的重写方法中添加日志输出,比如:

@Override
public List selectListWithRawParameter(String statement, Object parameter, int firstResult, int maxResults) {
    log.info("进入自定义selectListWithRawParameter方法,SQL语句:{}", statement);
    return super.selectListWithRawParameter(statement, parameter, firstResult, maxResults);
}

重新部署后执行流程,查看日志是否输出,或通过断点确认是否进入自定义方法。


内容的提问来源于stack exchange,提问作者Vikash Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:12:37