06-29-2010 08:34 AM
Hi all,
Am trying to insert data in a thread to a single table from different classes..
In the run method of the thread class am preparing the insertquery.
in generation of the insertquery am calling a static method 'getNextSurveyResponseId' of a class to generate a unique id.
as all the insert queries use 'getNextSurveyResponseId' for unique id, am using synchronized block and getting lock on 'getNextSurveyResponseId' but the lock is holding no longer and am getting same values in the generation of unique id.
please help !!
the following is the code snippet....
String sqrid = null;
synchronized(GenerateNextId.getNextSurveyQuestionR esponseId())
{
sqrid = GenerateNextId.getNextSurveyQuestionResponseId();
}
public static String getNextSurveyQuestionResponseId()
{
synchronized(DatabaseFunctions._database) {
if(GenerateNextId.SURVEY_QUESTION_RESPONSEID == null)
{
Tools.print("XCO: GenerateNextId: getNextSurveyQuestionResponseId: Called for the first time");
Dictionary [][]sqrid = DatabaseFunctions.get("Select count(SurveyQuestionResponseID), (max(SurveyQuestionResponseID) +1) from OA_SURVEYQUESTIONRESPONSE");
if(sqrid != null)
{
int count = Integer.parseInt(sqrid[0][0].value.toString());
if(count == 0)
{
GenerateNextId.SURVEY_QUESTION_RESPONSEID = "15001";
}
else
{
GenerateNextId.SURVEY_QUESTION_RESPONSEID = sqrid[0][1].value.toString();
}
return GenerateNextId.SURVEY_QUESTION_RESPONSEID;
}
}
int temp = Integer.parseInt(GenerateNextId.SURVEY_QUESTION_RE SPONSEID);
GenerateNextId.SURVEY_QUESTION_RESPONSEID = Integer.toString(++temp);
return GenerateNextId.SURVEY_QUESTION_RESPONSEID;
}
}
Solved! Go to Solution.
06-29-2010 08:36 AM
just to doublecheck. do you call the code from a single application instance/entry point? or do you use an autostart part?
06-29-2010 08:42 AM
no it is not an auto start up. the app is starting from a single point of instance public static void main(String []args)
06-29-2010 08:48 AM
don't know why your synchronized doesn't work.
you could implement a queue with a callback maybe.
06-29-2010 08:58 AM
now i make the getNextSurveyRespone() synchronized method and i hold a lock on the database object here even.
now am able to generate the unique ids... but everytime while inserting to database programatically it is throwing constraint failed error.
most surprising is when i copy the insert statement from debug messages and executes teh same query in the sqlite manager, the query is executing without any error...
why it is happening...?/
recap after modificcations...
getNextSurveyResponse() is a synchronized method.
am holding lock on teh datbase obj. in this mehtod.
am holding lock on the synchronized method while calling.
unique id is generating now.
but throwing constraint failed error, while inserting data.
hlp!!!!!!
06-30-2010 03:29 AM
making the method getNextSurveyResponseId as synchronized method works for me.
thanks all