In summary...
int main() {
pid_t childPid;
EXEC SQL CONNECT :username IDENTIFIED BY :password;
for (;;)
{
childPid = fork();
if(childPid > 0)
{ // parent process
...
...
...
}
else if(childPid == 0)
{ // chiled process
EXEC SQL DECLARE c1 CURSOR FOR
SELECT empno, ename, sal FROM emp;
for (;;)
{
EXEC SQL OPEN c1;
EXEC SQL FETCH c1 INTO :emp_rec;
if(sqlca.sqlcode == 1403)
{
EXEC SQL CLOSE c1;
EXEC SQL COMMIT WORK RELEASE;
break;
}
...
...
//A function that sends results to the Client program.
SendPacketTOClient(fd,size);
}
EXEC SQL COMMIT WORK;
exit(0);
}
else { // fork Fail
perror("fork Fail! \n");
return -1;
}
sleep(60);
}
retun 0;
}
As above, DB CONNECT is performed in the parent process and forks is performed every minute, and the child process ends with exit (0) after inquiry (DECLARE->OPEN->FETCH->CLOSE). There was no problem because it was processed until FETCH. But if CLOSE is not working properly, there was an ORA-01000: maximum open response error later. When I actually checked, the child process was created and it didn't close when it was terminated after finishing work.
After Serch, I found out that it was a problem because the Servie Process was one, so I solved it by branching the parent process and the child process DB connection.
I solved it, but I want to know the principle, so I leave a question. May I know why DECLARE, OPEN, and FETCH are executed, but COLSE is not executed?
Thanks.
Add "EXEC SQL FREE :c1;" Statement. This will free the open cursors. Also add "EXEC SQL ALLOCATE :c1;" Statement after declaring the cursor.