cancel
Showing results for 
Search instead for 
Did you mean: 

Concern about JavaDelegate implementations

fwachs
Champ in-the-making
Champ in-the-making
Hi all,

I haven't found any documentation or posts about what's the life cycle for the implementations of JavaDelegate.
I am worried about this because on my JavaDelegate implementations I create a database connection and use it normally, but i'm not sure what happens next with this instance, does it get crashed by the GC at some point? Does Activiti kill it somehow once the JavaDelegate has fulfilled it's purpose?

Talking about my java delegates itself, I am not closing those connections, I'm betting I should close them at the end of the execute method is done, am I betting correctly? Smiley Happy

Regards and sorry for the newbie questions, I'm new to all this.

Federico


PD: Here are my classes, keep in mind they are just for testing purposes Smiley Happy


public class UpdateSQLProcessTask extends SQLProcessTask {

    @Override
    protected void executeSQL(DelegateExecution execution) {
        Properties props = new Properties();
        int rows = this.getTemplate().update(this.getSQL(execution));
        props.setProperty("rows", String.valueOf(rows));
    }
}

public abstract class SQLProcessTask extends DataSourceTask implements JavaDelegate {
    protected FixedValue fixedSQL;
    protected JuelExpression expressionSQL;

    public void execute(DelegateExecution execution) {
        this.executeSQL(execution);
        this.closeDatasource();
    }

    protected abstract void executeSQL(DelegateExecution execution);

    protected void closeDatasource() {
        try {
            DataSourceUtils.releaseConnection(this.datasource.getConnection(), this.datasource);
            this.datasource = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void setFixedSQL(FixedValue fixedSQL) {
        this.fixedSQL = fixedSQL;
    }

    public void setExpressionSQL(JuelExpression expressionSQL) {
        this.expressionSQL = expressionSQL;
    }

    public String getSQL(DelegateExecution execution) {
        if (expressionSQL != null) {
            return expressionSQL.getValue(execution).toString();
        } else if (fixedSQL != null) {
            return fixedSQL.getValue(execution).toString();
        }
        return null;
    }
}


public class DataSourceTask {

    protected DataSource datasource = createDatasource();
    protected JdbcTemplate template = new JdbcTemplate(datasource);

    protected JdbcTemplate getTemplate() {
        return template;
    }

    private DataSource createDatasource() {
        return new DriverManagerDataSource(
                "jdbc:mysql://someHost:3306/someDB", "user",
                "hardPassword");
    }
}
6 REPLIES 6

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
Afaik the documentation states that java delegates should Stateless…

trademak
Star Contributor
Star Contributor
Right, the Java delegates are stateless, so you definitely close every connection you have opened in the Java logic.

Best regards,

fwachs
Champ in-the-making
Champ in-the-making
Uhm ok then, do you think this is the recommended way to hit an external db ( not the activiti one ) ?

jbarrez
Star Contributor
Star Contributor
Yes, looks ok at a first glance: opening and closing the connection in the same call.

fwachs
Champ in-the-making
Champ in-the-making
Actually I created a dbcp datasource bean on activiti application context and I pull that bean on the delegate class by calling a static class from activiti.

That's better right?


public abstract class SQLProcessTask extends DataSourceTask implements JavaDelegate {
    protected FixedValue fixedSQL;
    protected JuelExpression expressionSQL;

    public void execute(DelegateExecution execution) {
        this.openDatasource();
        this.executeSQL(execution);
    }

    private void openDatasource() {
        this.datasource = (DataSource) Context.getProcessEngineConfiguration().getBeans().get("dataSource.single");
        this.template = new JdbcTemplate(datasource);
    }

    protected abstract void executeSQL(DelegateExecution execution);

    public void setFixedSQL(FixedValue fixedSQL) {
        this.fixedSQL = fixedSQL;
    }

    public void setExpressionSQL(JuelExpression expressionSQL) {
        this.expressionSQL = expressionSQL;
    }

    public String getSQL(DelegateExecution execution) {
        if (expressionSQL != null) {
            return expressionSQL.getValue(execution).toString();
        } else if (fixedSQL != null) {
            return fixedSQL.getValue(execution).toString();
        }
        return null;
    }
}

jbarrez
Star Contributor
Star Contributor
Using a connection pool is *always* better 😉
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.