• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this a connection problem???

 
sam guo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class WorkflowProcessor {
/..some codes../
private static HashMap activeWorkflows;
/..some codes../
static {
try {
// Load all active workflows
Persistence.init(); //build connection from databse
activeWorkflows = WorkflowP.getActiveWorkflows(); //get the activeworkflows from databse
} catch (XflowException e) {
log.error("Unable to load active workflows: " + e.getMessage());
}
}
/.other codes../


//get the activeworkflows from databse!
public static HashMap getActiveWorkflows () throws XflowException {

HashMap activeWorkflows = new HashMap();
Connection con = null;
Statement s = null;
try {
con = Persistence.getConnection();
s = con.createStatement();

String sql = "select workflowId from workflow where isActive = 1";
log.info (sql);
ResultSet rs = s.executeQuery (sql);
while (rs.next()) {
int wfId = rs.getInt("workflowId");
WorkflowId workflowId = new WorkflowId(wfId);
Integer key = new Integer(workflowId.getValue());
activeWorkflows.put (key, workflowId);
}
} catch (Exception e) {
e.printStackTrace();
throw new XflowException ("Failed to save workflow in database");
} finally {
if (s!=null) {
try {
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (Exception e) {
}
}
}
return activeWorkflows;
}


Why sometimes, just a few times... I get activeWorkflows=null, but according to the database this activeworkflow can never be null.

Can someone fix out any problems in this code?
 
sam guo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getActiveWorkflows function is under which in WorkflowP class.

Online waitting for someone can fix it?!
 
Les Hayden
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get activeWorkflows=null, but according to the database this activeworkflow can never be null


Actually if your method getActiveWorkflows() throws any exception, activeWorkflows will be null. Are you seeing any stack traces, if so, they would be useful to resolving the problem.

Also, I can't speak for anyone else, but formatting your code would make it much easier to read and assist you.

Regards,
Les
 
sam guo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Les

Thanks for your message!
But it seems that getActiveWorkflows() will not possible to throws any exception here if connection is OK.
And from the work process, I never got exception from getActiveWorkflows().

Actually I got null for activeWorkflows below:
public static void setVariable(WorkflowId workflowId, String name, Object value) throws XflowException {
int wid = workflowId.getId();
Integer key = new Integer(wid);
System.out.println("As we can get the wid for the workflow is : " + wid);
System.out.println("As we can get the active work flow key for this work flow ID is: " + activeWorkflows.get(key));
if (activeWorkflows.get(key) == null) {
throw new XflowException("Workflow ID is not active or valid");
}
WorkflowP.setVariable(workflowId.getValue(), name, value);
}
//The wid is not null, but activeWorkflows sometimes is null, then it will throws exception: "Workflow ID is not active or valid".

 
Les Hayden
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my logic...

First you set active workflows to new HashMap when you enter getActiveWorkflows().


You have only one return statement and it returns activeWorkflows. Therefore, if your method ever returns, activeWorkflows cannot be null.

While your getActiveWorkflows() method handles the SQLExceptions with a catch( Exception e), it then re-throws the exception as a XFlowException.



By throwing the exception to the calling method, you would cause the activeWorkflows in the calling method to be null because no value is returned from the getActiveWorkflows() method. You just log the exception and continue.

From the code you gave us, that is the only way that the value could ever be null. In the code below activeWorkflows is undefined


I'm afraid that your description of the problem and your code snipits are not lining up for me.

Regards,
Les
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sam guo,

please use the UBB Code facility to format your post. it makes your post much easier to read for other ranchers.

Thanks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic