12-24-2024 12:03 AM - edited 12-24-2024 12:06 AM
I am using Alfresco Process Services 24.2 Spring version - 6.1.5
I have overridden DbSqlSessionFactory in my custom code using below code 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;
public Session openSession(CommandContext commandContext) {
return super.openSession(commandContext);
}
}
And CustomDbSqlSession
package com.activiti.extension.bean.extension.db;
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 java.util.stream.Collectors.toList;
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"})
public List selectListWithRawParameter(String statement, Object parameter, int firstResult, int maxResults) {
return super.selectListWithRawParameter(statement, parameter, firstResult, maxResults);
}
@SuppressWarnings({"rawtypes", "unchecked"})
public List selectListWithRawParameterWithoutFilter(String statement, Object parameter, int firstResult, int maxResults) {
return super.selectListWithRawParameterWithoutFilter(statement, parameter, firstResult, maxResults);
}
protected void flushRegularInsert(Entity persistentObject, Class<? extends Entity> clazz) {
super.flushRegularInsert(persistentObject, clazz);
}
protected void flushBulkInsert(Collection<Entity> persistentObjectList, Class<? extends Entity> clazz) {
super.flushBulkInsert(persistentObjectList, clazz);
}
protected void flushUpdates() {
super.flushUpdates();
}
}
I am able to compile and create build, After deploying changes to APS I am able to create and execute process . But in CustomDbSqlSession class, I have overridden few DbSqlSession's methods and that are not getting executed. I tried by adding logs and debuggers in Intellij Idea it seems flow is not executing in custom overridden method, it's calling the default DbSqlSession's method.
I wanted to execute the CustomDbSqlSession's overridden methods.
if I changes openSession method to below code then the overridden method of CustomDbSqlSession is getting executed, but due to that I am getting CommandContext error and not able to start the process,
@Override
public Session openSession() {
return dbSqlSession.getObject();
}
Can anyone please help here?
12-26-2024 05:39 AM
package com.activiti.extension.bean.extension.db;
import com.activiti.extension.bean.service.encryption.VariableEncryptionService;
import lombok.RequiredArgsConstructor;
import org.activiti.engine.impl.db.DbSqlSessionFactory;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.interceptor.Session;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class CustomDbSqlSessionFactory extends DbSqlSessionFactory {
private final VariableEncryptionService variableEncryptionService;
@override
public Session openSession(CommandContext commandContext) {
return new CustomDbSqlSession(this, commandContext, variableEncryptionService);
}
}
Custom CustomDbSqlSession class
package com.activiti.extension.bean.extension.db;
import com.activiti.extension.bean.service.encryption.VariableEncryptionService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.ActivitiOptimisticLockingException;
import org.activiti.engine.impl.db.DbSqlSession;
import org.activiti.engine.impl.db.HasRevision;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.Entity;
import org.springframework.context.annotation.Scope;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import static java.util.stream.Collectors.toList;
import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;
@Slf4j
//@Component
@Scope(SCOPE_PROTOTYPE)
public class CustomDbSqlSession extends DbSqlSession {
private final VariableEncryptionService variableEncryptionService;
public CustomDbSqlSession(CustomDbSqlSessionFactory dbSqlSessionFactory, CommandContext commandContext,
VariableEncryptionService variableEncryptionService) {
super(dbSqlSessionFactory, commandContext.getEntityCache());
this.variableEncryptionService = variableEncryptionService;
}
private <T> void encrypt(T value) {
variableEncryptionService.encrypt(value);
}
private <T> T decrypt(T value) {
return variableEncryptionService.decrypt(value);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@override
public List selectListWithRawParameter(String statement, Object parameter, int firstResult, int maxResults) {
List<Object> result = super.selectListWithRawParameter(statement, parameter, firstResult, maxResults);
return super.cacheLoadOrStore(result.stream()
.map(this::decrypt)
.collect(toList()));
}
@SuppressWarnings({"rawtypes", "unchecked"})
@override
public List selectListWithRawParameterWithoutFilter(String statement, Object parameter, int firstResult, int maxResults) {
List<Object> result = super.selectListWithRawParameterWithoutFilter(statement, parameter, firstResult, maxResults);
return result.stream()
.map(this::decrypt)
.collect(toList());
}
@SuppressWarnings({"rawtypes", "unchecked"})
@override
public List selectListWithRawParameter(String statement, Object parameter, int firstResult, int maxResults, boolean useCache) {
List<Object> result = super.selectListWithRawParameter(statement, parameter, firstResult, maxResults, useCache);
return super.cacheLoadOrStore(result.stream()
.map(this::decrypt)
.collect(toList()));
}
@override
protected void flushRegularInsert(Entity persistentObject, Class<? extends Entity> clazz) {
encrypt(persistentObject);
super.flushRegularInsert(persistentObject, clazz);
}
@override
protected void flushBulkInsert(Collection<Entity> persistentObjectList, Class<? extends Entity> clazz) {
persistentObjectList.forEach(this::encrypt);
super.flushBulkInsert(persistentObjectList, clazz);
}
@override
protected void flushUpdates() {
super.updatedObjects.forEach(this::encrypt);
super.flushUpdates();
}
}
3 weeks ago
Great topic! Properly overriding DbSqlSessionFactory can significantly enhance performance. It’s essential to ensure that your custom implementation efficiently manages connections and transactions. Additionally, I’d recommend checking out Infinite Craft for some innovative ideas on optimizing resource management in game environments—there's a lot we can learn from game development practices that can be applied here as well!
a week ago
@vikash_patel as an option for encryption/decryption variables I could propose to use wrapper or custom implementation of VariableType interface and register it in SpringProcessEngineConfiguration.
Explore our Alfresco products with the links below. Use labels to filter content by product module.