aspose file tools*
The moose likes JDBC and the fly likes Is this a connection problem??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Is this a connection problem???" Watch "Is this a connection problem???" New topic
Author

Is this a connection problem???

sam guo
Greenhorn

Joined: Nov 05, 2007
Posts: 5
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

Joined: Nov 05, 2007
Posts: 5
getActiveWorkflows function is under which in WorkflowP class.

Online waitting for someone can fix it?!
Les Hayden
Greenhorn

Joined: Jun 19, 2002
Posts: 28
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


Les Hayden
sam guo
Greenhorn

Joined: Nov 05, 2007
Posts: 5
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

Joined: Jun 19, 2002
Posts: 28
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

Joined: Dec 06, 2005
Posts: 1321

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,


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this a connection problem???