0 Replies Latest reply: Jan 17, 2007 2:51 AM by Knam-Oracle RSS

    SAMPLE: XA PROGRAM

    Knam-Oracle
      제품 : PRECOMPILERS

      작성날짜 : 1996-11-12


      ============
      myfuncb.sql
      ============
      create or replace function myfuncb ( aaa number )
           return number
           is
           bbb number;
      begin
                for c1 in (
      select nvl(comm,0) as bbb
                from emp
                where empno=7499
                ) loop
           return c1.bbb;
           end loop;
           return 0;
      end;
      /

      ===========
      xa_prob.pc
      ===========
      /*
      */

      EXEC ORACLE OPTION (RELEASE_CURSOR=NO);
      EXEC ORACLE OPTION (HOLD_CURSOR=YES);
      /* EXEC ORACLE OPTION (SQLCHECK=SEMANTICS);
      */

      /*#include <stdio.h>*/
      #include <string.h>

      EXEC SQL INCLUDE SQLCA.H;
      EXEC SQL INCLUDE ORACA.H;

      #include <xa.h>

      EXEC SQL BEGIN DECLARE SECTION;
           int emp_number;
           char emp_name[15];
      char *username = "SCOTT";
      char *password = "TIGER";
      VARCHAR sqlstmt[80];
           int outval;
      EXEC SQL END DECLARE SECTION;

      int xarslt = 0;          /* xa result code */
      int usexa = 1;          /* 1 -> use xa, otherwise normal sql */
      int debug = 1;          /* print ALL result codes */
      int rmid = 1;
      XID xidx = { 0x1e0a0a1e, 12, 8, "KeesGTID01Branch0001" };
      XID *xid = &xidx;
      char xaoinfo[] =
      "oracle_xa+ACC=P/SCOTT/TIGER+SESTM=600";
      int serialno = 1;      /* next transaction this pid */
      #define LOOPMAX 1

      void
      xidgen(xid)
      XID* xid;     /* output parameter */
      {
      char seq [11];

      sprintf(seq, "%d", serialno);
      strncpy((&xid->data[6]), seq, 6);
      serialno++;
      }

      int dbopen()
      {
      int anyerror=0;
      /* open database */
      if ( usexa )
      {
      xarslt = xaoopen(xaoinfo, rmid, TMNOFLAGS);
      if ( xarslt || debug )
      printf("xaoopen = %d\n", xarslt);
      if ( xarslt )
      anyerror++;
      }
      else
      {

      /* enable oraca */
      oraca.oradbgf=1;
      oraca.oracchf=1;
      oraca.orastxtf=3;

      EXEC SQL CONNECT :username IDENTIFIED BY :password;
      if (sqlca.sqlcode || debug )
      printf("connect sqlcode = %d\n", sqlca.sqlcode);
      if ( sqlca.sqlcode )
      anyerror++;
      }
      return(anyerror);
      }

      int starttr()
      {
      int anyerror=0;
      int xarslt;

      /* start transaction */
      if ( usexa )
      {
      xidgen(xid);
      xarslt = xaostart(xid, rmid, TMNOFLAGS);
      if ( xarslt || debug )
      printf("xaostart = %d\n", xarslt);
      if ( xarslt )
      anyerror++;
      }
      else
      {
      /* normal sql does by default */
      }
      return(anyerror);
      }

      int jointr()
      {
      int anyerror=0;
      int xarslt;

      /* start transaction */
      if ( usexa )
      {
      xidgen(xid);
      xarslt = xaostart(xid, rmid, TMJOIN);
      if ( xarslt || debug )
      printf("xaostart = %d\n", xarslt);
      if ( xarslt )
      anyerror++;
      }
      else
      {
      /* can't join a session w/o xa */
      printf("can't join if not xa\n");
      anyerror++;
      }
      return(anyerror);
      }

      int dosql()
      {
      int anyerror=0;
      int xarslt;

      /* do database operation */
      EXEC SQL EXECUTE
      begin :outval := myfuncb(1); end;
      END-EXEC;
      if (sqlca.sqlcode || debug )
      printf("execute sqlcode = %d, result=%d\n", sqlca.sqlcode,outval);
      if ( sqlca.sqlcode )
      anyerror++;

      return(anyerror);

      }

      int commit()
      {
      int anyerror=0;
      int xarslt;

      /* commit transaction */
      if ( usexa )
      {
      xarslt = xaocommit(xid, rmid, TMONEPHASE);
      if ( xarslt || debug )
      printf("xaocommit = %d\n", xarslt);
      if ( xarslt )
      anyerror++;
      }
      else
      {
      EXEC SQL commit work;
      if (sqlca.sqlcode || debug )
      printf("commit sqlcode = %d\n", sqlca.sqlcode);
      if ( sqlca.sqlcode )
      anyerror++;
      }
      return(anyerror);
      }

      int rollback()
      {
      int anyerror=0;
      int xarslt;

      if ( usexa )
      {
      xarslt = xaorollback(xid, rmid, TMNOFLAGS);
      if ( xarslt || debug )
      printf("xaocommit = %d\n", xarslt);
      if ( xarslt )
      anyerror++;
      }
      else
      {
      EXEC SQL rollback work;
      if (sqlca.sqlcode || debug )
      printf("commit sqlcode = %d\n", sqlca.sqlcode);
      if ( sqlca.sqlcode )
      anyerror++;
      }
      return(anyerror);
      }

      int dbclose()
      {
      int anyerror=0;
      int xarslt;

      /* close database */
      if ( usexa )
      {
      xarslt = xaoclose(xaoinfo, rmid, TMNOFLAGS);
      if ( xarslt || debug )
      printf("xaoclose = %d\n", xarslt);
      if ( xarslt )
      anyerror++;
      }
      else
      {
      EXEC SQL rollback work release;
      if (sqlca.sqlcode || debug )
      printf("rollback sqlcode = %d\n", sqlca.sqlcode);
      }
      return(anyerror);
      }

      int detach()
      {
      int anyerror=0;
      int xarslt;

      if ( usexa )
      {
      xarslt = xaoend(xid, rmid, TMSUCCESS);
      if ( xarslt || debug )
      printf("xaoend = %d\n", xarslt);
      if ( xarslt )
      anyerror++;
      }
      else
      {
      }
      return(anyerror);
      }



      void main()
      {
      int i;

      char temp[11];

      /* sprintf(temp,"%d", getpid()); */
      sprintf(temp,"%d", 100000);
      strncpy(xid->data, temp, 6);
      if (dbopen()) exit(1);
      for ( i=0; i < LOOPMAX; i++ )
      {
      if (starttr()) exit(1);
      if (dosql()) exit(1);
      if (detach()) exit(1);
      if (commit()) exit(1);
      }
      if (dbclose()) exit(1);

      exit(0);
      }


      ========
      proc.mk
      ========
      include $(ORACLE_HOME)/precomp/env_precomp.mk

      .SUFFIXES: .pc .c .o

      LDSTRING=
      PRODUCT_LIBHOME=
      MAKEFILE=proc.mk
      PROCPLSFLAGS= sqlcheck=full userid=$(USERID) dbms=v6_char
      PROCPPFLAGS= code=cpp include=/usr/include include=/opt/SUNWspro/SC3.0.1/include/CC
      USERID=scott/tiger
      INCLUDE=$(I_SYM). $(PRECOMPPUBLIC)

      SAMPLES=sample1 sample4 sample6 sample7 sample8 \
           sample9 sample10 xa_prob sample12 oraca sqlvcp cv_demo
      CPPSAMPLES=cppdemo1 cppdemo2 cppdemo3

      # Rule to compile any program (specify EXE= and OBJS= on command line)

      build: $(OBJS)
           $(CC) -o $(EXE) $(OBJS) -L$(LIBHOME) $(PROLDLIBS)

      cppbuild:
           $(PROC) $(PROCPPFLAGS) iname=$(EXE)
           CC -c $(INCLUDE) $(EXE).c
           CC -o $(EXE) $(OBJS) -L$(LIBHOME) $(PROLDLIBS)

      samples: $(SAMPLES)
      cppsamples: $(CPPSAMPLES)

      $(SAMPLES):
           $(MAKE) -f $(MAKEFILE) build OBJS=$@.o EXE=$@

      $(CPPSAMPLES):
           $(MAKE) -f $(MAKEFILE) cppbuild OBJS=$@.o EXE=$@

      sample5:
           @echo 'sample5 is a user-exit demo; use a forms makefile to build it.'

      .pc.c:
           $(PROC) $(PROCFLAGS) iname=$*.pc

      .pc.o:
           $(PROC) $(PROCFLAGS) iname=$*.pc
           $(CC) $(CFLAGS) -c $*.c

      .c.o:
           $(CC) $(CFLAGS) -c $*.c

      sample6.o: sample6.pc
           $(PROC) dbms=v6_char iname=$*.pc
           $(CC) $(CFLAGS) $(PRECOMPPUBLIC) -c $*.c

      sample9.o: sample9.pc calldemo-sql
           $(PROC) $(PROCPLSFLAGS) iname=$*.pc
           $(CC) $(CFLAGS) $(PRECOMPPUBLIC) -c $*.c

      cv_demo.o: cv_demo.pc cv_demo-sql
           $(PROC) $(PROCPLSFLAGS) iname=$*.pc
           $(CC) $(CFLAGS) $(PRECOMPPUBLIC) -c $*.c

      xa_prob.o: xa_prob.pc
           $(PROC) $(PROCPLSFLAGS) iname=$*.pc dbms=v6
           $(CC) $(CFLAGS) $(PRECOMPPUBLIC) -c $*.c

      calldemo-sql:
           sqlplus scott/tiger @../sql/calldemo </dev/null

      sample11-sql:
           sqlplus scott/tiger @../sql/sample11 </dev/null

      cv_demo-sql:
           sqlplus scott/tiger @../sql/cv_demo </dev/null