cancel
Showing results for 
Search instead for 
Did you mean: 

5.14 missing method call

tcheekva
Champ in-the-making
Champ in-the-making
In previous version we were able to get a handle to the DBSession by way of the following code.  This is done in a custom form type

        Map<String,ProcessEngine> pe = ProcessEngines.getProcessEngines();
            for (Entry<String, ProcessEngine> enumEntry : pe.entrySet()) {
               System.out.println("ProcessEngine name="+enumEntry.getKey());
               ProcessEngineImpl pei = (ProcessEngineImpl)enumEntry.getValue();
               Connection c = ((DbSqlSession)pei.getDbSqlSessionFactory().openSession()).getSqlSession().getConnection();
                                        //… more stuff here.
            }

in 5.14 we get the following error

java.lang.NoSuchMethodError: org.activiti.engine.impl.getDbSqlSessionFactory()Lorg/activiti/engine/impl/db/DbSqlSessionFactory;

The method is no longer there. 

Anyone have any suggestions on how to get access to current database?

Thanks

4 REPLIES 4

frederikherema1
Star Contributor
Star Contributor
That's the risk of using IMPL code, this is subject to change between versions Smiley Wink

I'm not sure where the code you showed is used, but I suspect it's inside form-handling. If that is the case, you can use the org.activiti.engine.impl.context.Context.getCommandContext() method. The CommandContext will expose the sessions that are currently used. This will ensure you get the same connection as activiti is using, without the need to explicitly call "openSession".

tcheekva
Champ in-the-making
Champ in-the-making
What we really need is an function that will return a handle to the current session.  We have created some new FormTypes that need database access.  We need them to use the currently active session.  My code is below.  It fills in a combo box from the database.  Our app takes care of generating the user forms.

I think we had tried the Context before but could not figure out how to get a handle from within the formtype code.

package org.activiti.custom;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.activiti.custom.spring.CustomQuery;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.form.AbstractFormType;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.activiti.engine.impl.db.DbSqlSession;


public class EnumSelectRespEngFormType extends AbstractFormType {

  protected Map<String, String> values;

  public EnumSelectRespEngFormType() {
   }
 
  public EnumSelectRespEngFormType(Map<String, String> values) {
    this.values = values;
  }

  public String getName() {
    return "enumselectrespeng";
  }
 
  @Override
  public Object getInformation(String key) {
   Connection c = null;
   if (key.equals("values")) {
    Map<String,ProcessEngine> pe = ProcessEngines.getProcessEngines();
    for (Entry<String, ProcessEngine> enumEntry : pe.entrySet()) {
     System.out.println("ProcessEngine name="+enumEntry.getKey());
     ProcessEngineImpl pei = (ProcessEngineImpl)enumEntry.getValue();
     c = ((DbSqlSession)pei.getDbSqlSessionFactory().openSession()).getSqlSession().getConnection();
    
    
    }

    String qry = "select cu.username,cu.FIRST_NAME||'  '||cu.LAST_NAME from cm_users cu,cm_group cg where upper(cu.USERNAME)=upper(cg.USERNAME) and upper(cg.GROUPNAME)='STARSCNRESPENG'";
    //Connection connection = Context.getCommandContext().getDbSqlSession().getSqlSession().getConnection();

    String[][] qryResults = CustomQuery.getResultSet(c,qry, 2);
    Map<String, String> v = new HashMap<String,String>();
    if ((qryResults != null) && (qryResults.length > 0)) {
     for (int count = 0; count < qryResults.length; count++) {
      v.put(qryResults[count][0], qryResults[count][1]);
     }

    }      
    return v;
  
   }
 
   return null;
  }

  @Override
  public Object convertFormValueToModelValue(String propertyValue) {
    validateValue(propertyValue);
    return propertyValue;
  }

  @Override
  public String convertModelValueToFormValue(Object modelValue) {
    if(modelValue != null) {
      if(!(modelValue instanceof String)) {
        throw new ActivitiIllegalArgumentException("Model value should be a String");
      }
      validateValue((String) modelValue);
    }
    return (String) modelValue;
  }
 
  protected void validateValue(String value) {
    if(value != null) {
      if(values != null && !values.containsKey(value)) {
        throw new ActivitiIllegalArgumentException("Invalid value for enum form property: " + value);
      }
    }
  }

}

jbarrez
Star Contributor
Star Contributor
Did you try this:

commandContext.getDbSqlSession().getSqlSession()

The commandContext can be fetched using Context.getCommandContext().

sudheerm
Champ in-the-making
Champ in-the-making
Hi jbarrez,

I am not able to get CommandContext from Context. null object is being returned .

CommandContext commandContext = Context.getCommandContext();

can you please help me ?