Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified
Accepted Solution

synchronization of a database object

 

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.getNextSurveyQuestionResponseId())
            {
                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_RESPONSEID);
            GenerateNextId.SURVEY_QUESTION_RESPONSEID = Integer.toString(++temp);
            return GenerateNextId.SURVEY_QUESTION_RESPONSEID;
        }
    }

 

 

Thanks and Regards,
PraveenGoparaju.
Please use plain text.
Developer
simon_hain
Posts: 15,805
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: synchronization of a database object

just to doublecheck. do you call the code from a single application instance/entry point? or do you use an autostart part?

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: synchronization of a database object

no it is not an auto start up. the app is starting from a single point of instance public static void main(String []args)

Thanks and Regards,
PraveenGoparaju.
Please use plain text.
Developer
simon_hain
Posts: 15,805
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: synchronization of a database object

don't know why your synchronized doesn't work.

you could implement a queue with a callback maybe.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: synchronization of a database object

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!!!!!!

 

 

 

 

Thanks and Regards,
PraveenGoparaju.
Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: synchronization of a database object

making the method getNextSurveyResponseId as synchronized method works for me.

 

thanks all

Thanks and Regards,
PraveenGoparaju.
Please use plain text.