Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28232
  • 博文数量: 4
  • 博客积分: 45
  • 博客等级: 民兵
  • 技术积分: 97
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-25 12:04
文章分类
文章存档

2013年(3)

2012年(1)

我的朋友

分类: C/C++

2013-03-29 16:56:15

TLR.ec:

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include"atmi.h"
  4. #include"fml.h"
  5. #include"account.fml.h"
  6. #include"math.h"
  7. #include"userlog.h"


  8. //int account_id;
  9. //float amts,amt,acct_bal;

  10. //add by myself
  11. EXEC SQL BEGIN DECLARE SECTION;
  12. char userid[20]="owner/abc1234";
  13. char remoteDB[20]="mydb";
  14. char data[2];
  15. int account_id;
  16. int amt,acct_bal;
  17. char amts[10];
  18. EXEC SQL END DECLARE SECTION;

  19. EXEC SQL INCLUDE sqlca;

  20. void sqlerror();

  21. void DEPOSIT(TPSVCINFO *transb){
  22.     FBFR *transf=(FBFR*)transb->data;
  23.     account_id=Fvall(transf,ACCOUNT_ID,0);//函数的具体作用


  24.     Fget(transf,SAMOUNT,0,amts,0);//此函数的作用
  25.     sscanf(amts,"%f",&amt);

  26.     //add by myself
  27.     EXEC SQL WHENEVER SQLERROR DO sqlerror();
  28.     //此处不需要指定连接,由tuxedo通过GROUP的OPENINFO进行连接
  29.     //EXEC SQL CONNECT :userid USING :remoteDB;

  30.     EXEC SQL DECLARE dacur CURSOR FOR
  31.         SELECT BALANCE
  32.         FROM ACCOUNT
  33.         WHERE ACCOUNT_ID=:account_id;
  34.     EXEC SQL OPEN dacur;
  35.     EXEC SQL FETCH dacur INTO :acct_bal;

  36.     acct_bal+=amt;
  37.     EXEC SQL update ACCOUNT set BALANCE = :acct_bal
  38.         where ACCOUNT_ID = :account_id;
  39.     EXEC SQL close dacur;
  40.     //add by myself
  41.     //EXEC SQL COMMIT WORK RELEASE;

  42.     sprintf(amts,"$%d",acct_bal);
  43.     Fchg(transf,SBALANCE,0,amts,(FLDLEN)0);//

  44.     tpreturn(TPSUCCESS,0,transb->data,0L,0);
  45. }


  46. void WITHDRAWAL(TPSVCINFO *transb){
  47.     FBFR *transf=(FBFR*)transb->data;
  48.     account_id=Fvall(transf,ACCOUNT_ID,0);
  49.     Fget(transf,SAMOUNT,0,amts,0);
  50.     sscanf(amts,"%f",&amt);

  51.     //add by myself
  52.     EXEC SQL WHENEVER SQLERROR DO sqlerror();
  53.     //EXEC SQL CONNECT :userid USING :remoteDB;

  54.     EXEC SQL declare wacur cursor for
  55.         select BALANCE from ACCOUNT where ACCOUNT_ID= :account_id;
  56.     EXEC SQL open wacur;
  57.     EXEC SQL fetch wacur into :acct_bal;
  58.     //EXEC SQL close wacur;
  59.     if(amt>acct_bal){
  60.         if(fabs(acct_bal-amt)>0.01){
  61.             Fchg(transf,STATLIN,0,"Account overdraft",(FLDLEN)0);
  62.             EXEC SQL close wacur;
  63.             tpreturn(TPFAIL,0,transb->data,0L,0);
  64.         }
  65.         amt=acct_bal;
  66.     }
  67.     acct_bal-=amt;
  68.     EXEC SQL update ACCOUNT set BALANCE = :acct_bal
  69.         where ACCOUNT_ID = :account_id;
  70.     EXEC SQL close wacur;
  71.     //add by myself
  72.     //EXEC SQL COMMIT WORK RELEASE;

  73.     sprintf(amts,"$%d",acct_bal);
  74.     Fchg(transf,SBALANCE,0,amts,(FLDLEN)0);

  75.     tpreturn(TPSUCCESS,0,transb->data,0L,0);
  76. }

  77. void INQUERY(TPSVCINFO *transb){
  78.     userlog("INQUERY receive request from client!\n");

  79.     FBFR *transf=(FBFR*)transb->data;
  80.     account_id=Fvall(transf,ACCOUNT_ID,0);



  81.     //add by myself
  82.     EXEC SQL WHENEVER SQLERROR DO sqlerror();
  83.     //EXEC SQL CONNECT :userid USING :remoteDB;

  84.     //EXEC SQL declare icur cursor for
  85.     //    select BALANCE from ACCOUNT where ACCOUNT_ID = :account_id;

  86.     //EXEC SQL open icur;
  87.     //EXEC SQL fetch icur into :acct_bal;
  88.     //EXEC SQL close icur;

  89.     EXEC SQL select BALANCE into :acct_bal from ACCOUNT where ACCOUNT_ID=:account_id;
  90.     if(sqlca.sqlcode!=0){
  91.         userlog("select from EMP failure,sqlcode=%ld, sqlerr=%s\n",sqlca.sqlcode,(char *)sqlca.sqlerrm.sqlerrmc);
  92.         tpreturn(TPFAIL,0,NULL,0L,0);
  93.     }
  94.     userlog("BALANCE=%d",acct_bal);

  95.     //add by myself
  96.     //EXEC SQL COMMIT WORK RELEASE;

  97.     sprintf(amts,"$%d",acct_bal);//%.2f不能进行有效转换,所以amts为0.0
  98.     //userlog("amts=%s",amts);
  99.     Fchg(transf,SBALANCE,0,amts,(FLDLEN)0);
  100.     Fchg(transf,FORMNAM,0,"CBLANCE",(FLDLEN)0);

  101.     tpreturn(TPSUCCESS,0,transb->data,0L,0);
  102. }


  103. void sqlerror(){
  104.     EXEC SQL WHENEVER SQLERROR CONTINUE;
  105.     EXEC SQL ROLLBACK WORK RELEASE;
  106.     printf("-----pro*c connect error!----n");
  107.     exit(-1);
  108. }

XFER.c

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include"atmi.h"
  4. #include"tx.h"
  5. #include"account.fml.h"
  6. #include"fml.h"

  7. long db_id,cr_id;
  8. //char t_amts[10];

  9. #ifdef TRANSACTION_MODE
  10. void TRANSFER_XA(TPSVCINFO *transb){
  11. #else
  12. void TRANSFER_NOXA(TPSVCINFO *transb){
  13. #endif
  14.     long olen;
  15.     FBFR *transf=(FBFR*)transb->data;

  16.     db_id=Fvall(transf,ACCOUNT_ID,0);
  17.     cr_id=Fvall(transf,ACCOUNT_ID,1);
  18.     //Fget(transf,SAMOUNT,0,t_amts,0);
  19.     //sscanf(amts,"%f",&amt);

  20.     //构造参数
  21.     //FBFR *inbuf=(FBFR*)tpalloc("FML",NULL,1024);
  22.     //Fchg(inbuf,SAMOUNT,0,(char*)t_amts,(FLDLEN)0);

  23. #ifdef TRANSACTION_MODE
  24.     tpbegin(60,0);
  25. #endif
  26.     Fchg(transf,ACCOUNT_ID,0,(char*)db_id,(FLDLEN)0);
  27.     //取款
  28.     int ret1=tpcall("WITHDRAWAL",(char*)transf,0,(char**)&transf,(long*)&olen,0);
  29.     //system("do shutdown");
  30.     //存款
  31.     Fchg(transf,ACCOUNT_ID,0,(char*)cr_id,(FLDLEN)0);
  32.     int ret2=tpcall("DEPOST",(char*)transf,0,(char**)&transf,(long*)&olen,0);
  33.     //system("do boot");
  34.     if((ret1==0)&&(ret2==0)){
  35. #ifdef TRANSACTION_MODE
  36.         tpcommit(0);
  37. #endif
  38.         tpreturn(TPSUCCESS,0,transb->data,0L,0);
  39.     }else{
  40. #ifdef TRANSACTION_MODE
  41.         tpabort(0);
  42. #endif
  43.         tpreturn(TPFAIL,0,transb->data,0L,0);
  44.     }
  45. }

bankclt.c

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include"atmi.h"
  4. #include"fml.h"
  5. #include"account.fml.h"

  6. int Balance(long accountid);

  7. int main(int argc,char *argv[]){
  8.     long aid1,aid2,samount,olen;
  9.     aid1=10000;
  10.     aid2=20000;
  11.     samount=10;//转账金额


  12.     if(tpinit((TPINIT*)NULL)==-1){
  13.         fprintf(stderr,"tpinit failed!\n");
  14.         exit(-1);
  15.     }
  16.     //申请缓冲区
  17.     FBFR *fbfr=(FBFR*)tpalloc("FML",NULL,1024);
  18.     if(fbfr==NULL){
  19.         fprintf(stderr,"tpalloc failed!\n");
  20.         tpterm();
  21.         exit(-1);
  22.     }

  23. #ifdef TRANSACTION_MODE
  24.     fprintf(stdout,"\nRun under Transaction Mode...\n");
  25. #else
  26.     fprintf(stdout,"\nRun under NON-Transaction Mode...\n");
  27. #endif

  28.     fprintf(stdout,"\nBalance of account before transfer\n");
  29.     fprintf(stdout,"--------------------------------------\n");
  30.     Balance(10000);
  31.     Balance(20000);

  32.     fprintf(stdout,"\nTransfer $10 from account 10000 to 20000...\n");
  33.     fprintf(stdout,"-----------------------------------------------\n");

  34.     //构造入参
  35.     Fchg(fbfr,ACCOUNT_ID,0,(char*)&aid1,(FLDLEN)0);
  36.     Fchg(fbfr,ACCOUNT_ID,1,(char*)&aid2,(FLDLEN)0);
  37.     Fchg(fbfr,SAMOUNT,0,(char*)&samount,(FLDLEN)0);

  38. #ifdef TRANSACTION_MODE
  39.     int ret1=tpcall("TRANSFER_XA",(char*)fbfr,0,(char**)&fbfr,(long*)&olen,0);
  40. #else
  41.     int ret1=tpcall("TRANSFER_NOXA",(char*)fbfr,0,(char**)&fbfr,(long*)&olen,0);
  42. #endif

  43.     if(ret1==-1)
  44.         fprintf(stdout,"\nTransfer Failed!\n");
  45.     else
  46.         fprintf(stdout,"\nTransfer Successful!\n");

  47.     
  48.     fprintf(stdout,"\nBalance of account after transfer\n");
  49.     fprintf(stdout,"--------------------------------------\n");
  50.     Balance(aid1);
  51.     Balance(aid2);

  52.     return 0;
  53. }

  54. int Balance(long account_id){
  55.     long olen;
  56.     FBFR *fbfr=(FBFR*)tpalloc("FML",NULL,1024);
  57.     if(fbfr==NULL){
  58.         fprintf(stderr,"tpalloc failed!\n");
  59.         tpterm();
  60.         exit(-1);
  61.     }
  62.     Fchg(fbfr,ACCOUNT_ID,0,(char*)&account_id,0);
  63.     if(tpcall("INQUERY",(char*)fbfr,0L,(char**)&fbfr,(long*)&olen,0)==-1){
  64.         fprintf(stderr,"Can not send request to service INQUERY\n");
  65.         tpfree((char*)fbfr);
  66.         tpterm();
  67.         exit(1);
  68.     }
  69.     fprintf(stdout,"Account_id=%d,Account_Balance=%s\n",account_id,(char*)Ffind(fbfr,SBALANCE,0,0));
  70.     return 0;
  71. }

编译服务器端程序用到的Makefile:

点击(此处)折叠或打开

  1. ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1
  2. INCLDIR= -I. -I$ORACLE_HOME/precomp/public -I$ORACLE_HOME/rdbms/public -I$ORACLE_HOME/rdbms/demo -I$ORACLE_HOME/plsql/public -I$ORACLE_HOME/network/public
  3. DFLAGS= -DPRECOMP -DLINUX -D_GNU_SOURCE -DSLTS_ENABLE -DSLMXMX_ENABLE -D_REENTRANT -DNS_THREADS
  4. CFLAGS= -O3
  5. LIBPATH= -L/u01/oracle/product/8.1.7/lib/
  6. LIBS=-lclntsh -ldl -lm

  7. conn:TLR_ORA.pc
  8.     proc parse=no iname=TLR_ORA.pc include=$(ORACLE_HOME)/precomp/public

  9.     buildserver -r Oracle_XA -o depositserver -f TLR_ORA.c -s DEPOSIT:DEPOSIT
  10.     buildserver -r Oracle_XA -o withdrawalserver -f TLR_ORA.c -s WITHDRAWAL:WITHDRAWAL
  11.     buildserver -r Oracle_XA -o inqueryserver -f TLR_ORA.c -s INQUERY:INQUERY
  12. clean:    
  13.     rm *.lis tp*

编译XFER和客户端命令:

点击(此处)折叠或打开

  1. buildclient -o bankclient -f bankclt.c -w
  2. buildserver -s TRANSFER_XA -o XFERserver -f XFER.c

运行结果:

点击(此处)折叠或打开

  1. [tuxapp@localhost TLR]$ ./bankclient

  2. Run under Transaction Mode...

  3. Balance of account before transfer
  4. --------------------------------------
  5. Account_id=10000,Account_Balance=$610
  6. Account_id=20000,Account_Balance=$990

  7. Transfer $10 from account 10000 to 20000...
  8. -----------------------------------------------

  9. Transfer

  10. Balance of account after transfer
  11. --------------------------------------
  12. Account_id=10000,Account_Balance=$610
  13. Account_id=20000,Account_Balance=$990



点击(此处)折叠或打开

注:以上内容属于个人学习总结,转载请注明出处
阅读(2371) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~