cancel
Showing results for 
Search instead for 
Did you mean: 

"Trying to use non-attached object TableValue"

Former Member
0 Kudos

Hello,

I wrote a script which should create one job per each row in a table. The table has two columns (key and parameter).

The script works only for the first value. Afterwards the job goes to error with the error message "Trying to use non-attached object TableValue".

May you help me to find the failure?

Source code:

import java.util.Iterator;
import com.redwood.scheduler.api.model.*;
import com.redwood.scheduler.api.model.enumeration.*;
import com.redwood.scheduler.api.scripting.variables.ScriptSessionFactory;

{
String     vTableName  = "TABLE1";
SchedulerSession session = ScriptSessionFactory.getSession();

Table importTable = session.getTableByName(vTableName);
  if(importTable == null)
  {
    jcsOut.println("Table " + vTableName + " does not exist");
    return;
  }

// Iterator for looping through the table
  Iterator iterTV = importTable.getTableValues();

  while(iterTV.hasNext())
  {
    // Get the single table value
    TableValue tv = (TableValue)iterTV.next();

    // Check, if the table value belongs to a specific column
    if (tv.getColumnName().equals("PARAMETER"))
    {
      // Get the key to identify the table row
      String vkey     = tv.getKey();

      String strParameter =   importTable.getTableValueBySearchKeySearchColumnName(vkey,"PARAMETER").getColumnValue();

      // Get the job definition      
      JobDefinition jd = jcsSession.getJobDefinitionByName("TEST_JOBDEFINITION");
      // Prepare a job based on the job definition
      Job job = jd.prepare();
      // Provide the other parameter values to the job
      jcsOut.println("JOBNAME: " + strParameter);
      job.getJobParameterByName("p_JobName").setInValueString(strParameter);
      job.getJobParameterByName("jdpName").setInValueString("PRINT_PDEST");
      job.getJobParameterByName("replaceValue").setInValueString("");
      job.getJobParameterByName("p_ChangeJobDef").setInValueString("Y");
      job.getJobParameterByName("p_TestRun").setInValueString("Y");
      jcsOut.println("");
      jcsSession.persist();
      jcsSession.waitForJob(job);
      jcsSession.refreshObjects(new SchedulerEntity[]{jd});
       }
     }
jcsOut.println("All requests from Table " + vTableName + " processed.");

}

Best regards

Dana

Accepted Solutions (1)

Accepted Solutions (1)

gmblom
Active Contributor
0 Kudos

Hello Dana,

This has to do with the refreshing of the session, this removes all retrieved objects from the session, also the entries that are still in your Iterator.

It is better to setup two SchedulerSessions, one for the query and one for the submit. Or do the query, store the items in a list and do a loop on that.

Regards Gerben

PS: a small improvement:

String strParameter =   importTable.getTableValueBySearchKeySearchColumnName(vkey,"PARAMETER").getColumnValue();

can be replaced with:

String strParameter = tv.getColumnValue();

Because you already have the correct TableValue selected here.

Former Member
0 Kudos

Hello Gerben,

I thought session and jcsSession are already 2 sessions!?

I created a second session but got the same error message. Do I use the sessions in a wrong way?

import java.util.Iterator;
import com.redwood.scheduler.api.model.*;
import com.redwood.scheduler.api.model.enumeration.*;
import com.redwood.scheduler.api.scripting.variables.ScriptSessionFactory;

{
String     vTableName  = "TABLE1";
SchedulerSession session1 = ScriptSessionFactory.getSession();

SchedulerSession session2 = ScriptSessionFactory.getSession();

Table importTable = session1.getTableByName(vTableName);
  if(importTable == null)
  {
    jcsOut.println("Table " + vTableName + " does not exist");
    return;
  }

// Iterator for looping through the table
  Iterator iterTV = importTable.getTableValues();

  while(iterTV.hasNext())
  {
    // Get the single table value
    TableValue tv = (TableValue)iterTV.next();

    // Check, if the table value belongs to a specific column
    if (tv.getColumnName().equals("PARAMETER"))
    {
      // Get the key to identify the table row
      String vkey     = tv.getKey();

      String strParameter =   tv.getColumnValue();

      // Get the job definition      
      JobDefinition jd = session2.getJobDefinitionByName("TEST_JOBDEFINITION");
      // Prepare a job based on the job definition
      Job job = jd.prepare();
      // Provide the other parameter values to the job
      jcsOut.println("JOBNAME: " + strParameter);
      job.getJobParameterByName("p_JobName").setInValueString(strParameter);
      job.getJobParameterByName("jdpName").setInValueString("PRINT_PDEST");
      job.getJobParameterByName("replaceValue").setInValueString("");
      job.getJobParameterByName("p_ChangeJobDef").setInValueString("Y");
      job.getJobParameterByName("p_TestRun").setInValueString("Y");
      jcsOut.println("");
      session2.persist();
      session2.waitForJob(job);
      session2.refreshObjects(new SchedulerEntity[]{jd});
       }
     }
jcsOut.println("All requests from Table " + vTableName + " processed.");

}

Best regards

Dana

gmblom
Active Contributor
0 Kudos

Hi Dana,

Yes you are probably right. It is possible that the ScriptSessionFactory.getSession() does not actually provide you with a new session.

Can you try SchedulerSession session = jcsJobContext.createSchedulerSession(); This is the preferred way in a RWScript.

Regards Gerben

Former Member
0 Kudos

Many thanks, Gerben!

Now it works.

Best regards

Dana

Answers (0)