1

I am fetching a few records from a table in an Oracle database. My program is able to fetch a few records but later it stops abruptly without showing any error.

Please check the code below

void fetch_data()
{
char tran_dt[16];
trace("fetch_data(): Begin");
EXEC SQL declare log3 cursor for select NVL(type_code,0),
                                        NVL(pri_tran_code,' '),
                                        NVL(tran_datetime,to_date('0001','YYYY')),
                                        from log_record 
                                        WHERE sys_seq_nbr=:next_sys_seq_nbr1 and
                                        tran_datetime = :last_tran_datetime1;
dttostr(tran_dt, &last_tran_datetime1);
sprintf(str, "fetch_data(): tran date time of last ssn (%d): (%s)", next_sys_seq_nbr1, tran_dt);
trace(str);
trace("fetch_data(): Before open log3");
EXEC SQL open log3;
if(sqlca.sqlcode) 
{
    printf("Error : sqlcode(%d) for open log3\n", sqlca.sqlcode);
    sprintf(str, "(%d) for open log3\0",sqlca.sqlcode);
    trace(str);
    error(str);
    return;
}
printf("fetch_data(): After open log3");
for(;;) 
{
    trace("fetch_data(): Before fetch log3");
    EXEC SQL fetch log3
        into

        :type_code, :pri_tran_code, :tran_datetime;
        if(sqlca.sqlcode) 
        {
            if (sqlca.sqlcode != DB_NORECORDS) 
            {
                trace("Error : sqlcode(%d) for fetch log3\n", sqlca.sqlcode);
                sprintf(str, "fetch_data() : Error : sqlcode(%d) for fetch log3\0", sqlca.sqlcode);
                trace(str);
                error(str);
                return;
            }
            break;
        }

}
trace("fetch_data(): After fetch log3");
EXEC SQL CLOSE log3;
}

The functions trace() and error() write the string which is passed to it as a parameter in a their respective text files. So the last thing that I can see written in the text file is "fetch_data(): Before open log3". If there is any problem when we open the cursor, shouldn't it have been caught by if(sqlca.sqlcode).

Please help me find out where is the problem. Thanks.

MK Singh
  • 706
  • 1
  • 13
  • 36
  • Does your database have any transaction logs you can look at? – Alex Reynolds Jan 22 '13 at 08:16
  • If it "stops abruptly", it is most likely terminated either by the runtime system or by the operating system. Is this a command line application? Do you see any messages like "segmentation fault"? I see some unsafe function calls in your code, like `sprintf` - how is `str` defined? Also is `dttostr()` your own function? How does it look like? – Andreas Fester Jan 22 '13 at 08:23
  • The table is already filled up with the details of 100s of transactions. After processing a few rows properly the program stops abruptly without any reason. I have also checked that the values in the rows which are processed correctly and those which are not have similar data. So it can't be a problem with the data that is held in these rows. – MK Singh Jan 22 '13 at 08:25
  • @Andreas: Yes this is a command line application and the str is a character string which is declared globally in this file. And I don't get any "segmentation fault" message. – MK Singh Jan 22 '13 at 08:27
  • You should run the application with a debugger. What operating system is this running on? – Andreas Fester Jan 22 '13 at 08:29
  • @Andreas : It is running on Solaris. I don't have a debugger. From where can I get it? And also I forgot to tell that yes 'dttostr' is my own function. It converts date to string. If there was any problem in any of the functions or variables, my program wouldn't have been able to process even a single row. But it does and stops every time at some specific rows (I have checked, all rows have similar data). – MK Singh Jan 22 '13 at 08:40
  • `If there was any problem in any of the functions or variables, my program wouldn't have been able to process even a single row.` Not true. If you write outside array bounds and corrupt your stack or heap, anything can happen at any time. Such applications can run for many years, and then for some reason (let it be an operating system update) suddenly stop working. Get [dbx](http://en.wikipedia.org/wiki/Dbx_%28debugger%29) or [gdb](http://en.wikipedia.org/wiki/GNU_Debugger) and get a backtrace when the application stops. – Andreas Fester Jan 22 '13 at 08:46
  • Can you try adding a global "EXEC SQL WHENEVER SQLERROR CALL my_error_function"? With a suitable my_error_function? – Chris Jan 22 '13 at 09:37

0 Answers0