Z
zhqingguang
Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序是一个收费系统,银行代收费,使用indy9中Tidtcpserver,我采用临界保护方式,现在正常情况下运行挺好,但当访问量大的时候,有时出现无法查询,只能服务器接收,我猜测是数据库死锁,不知道如何解决才好,下面是我的例码(略有删减),请高手提供解决问题思路,十分感谢!<br>//-------接收数据事件,客户端向服务器传送数据时触发----------------------------//<br>procedure TForm1.serverExecute(AThread: TIdPeerThread);<br> var<br> r_data:string; //接收的字符串<br> sl:tstringlist; //分隔接收数据为各个字段<br> lenstr:string; //pklen:数据长度字段<br> package_len:integer; //数据包长度 = 去掉pklen字段以后的长度<br> begin<br> with athread.Connection do<br> begin<br> //athread.Connection.ReadTimeout := 5000; //设置读数据超时时间为5秒<br> sl:=tstringlist.Create; //创建tstringlist<br> lenstr:= readstring(4); //读取数据长度<br> try<br> package_len := strtoint(lenstr);<br> except<br> begin<br> exit ;<br> end;<br> end;<br> //try<br> //begin<br> r_data := readstring(package_len); //读取数据包<br> sl:= split(r_data,'|'); //将各个字段分解出来<br> //-----------------判断是何种操作-------------------------------------//<br> if sl.Strings[1] = '1111' then //查询操作<br> begin<br> cscx.Enter; //进入临界区,进行保护<br> query(sl.Strings[2],athread);<br> r_data := '';<br> cscx.Leave; //操作完成,离开临界区<br> end;<br><br> if sl.Strings[1]='2222' then //收费功能<br> begin<br> athread.Connection.InputBuffer.Clear; //清接收缓冲区<br> cssf.Enter; //进入临界区,进行保护<br> sf(sl.strings[2],sl,athread); //用户编号<br> r_data:='';<br> cssf.Leave; //离开线程保护区<br> end;<br> end;<br> end;<br><br>//-----按卡号查询用户应收费用信息 交易代码:1111------------------------------------//<br>procedure tform1.query(cardid:String;athread:tidpeerthread);<br> var<br> begin<br> //----进行查询------------------------------------------------------------//<br> try<br> begin<br><br> sql:= 'SELECT a.bh, a.fh, a.xm, a.postaddr, a.cardid, b.nd, b.dj,'+<br> 'b.mj, b.yjk, b.yshk, b.qk, b.shfybh, b.shfbzhid '+<br> 'FROM a,b WHERE b.qk <> 0 and a.bh = b.bh and a.cardid=:cardid;';<br> query_cx.Close;<br> query_cx.SQL.Clear;<br> query_cx.sql.Add(sql);<br> query_cx.Parameters.ParamByName('cardid').Value := trim(cardid);<br> if query_cx.Prepared=false then query_cx.Prepared:=true;<br><br> query_cx.Open;<br> end;<br> except<br> begin<br> send('','0','1111','100',athread);<br> console.Lines.Add(' 查询时出错');<br> exit;<br> end;<br> end;<br><br> athread.Connection.InputBuffer.Clear; //清接收缓冲区<br> exit;<br> end;<br>//------------------------收费功能函数-交易代码:2222---------------------------------------//<br>procedure tform1.sf(yhbh:String;sl:tstringlist;athread:tidpeerthread);<br> var<br><br> send_cmd:string;<br> sql:string; //sql语句<br> q_sql:string; //<br> dm:string; //流水表的代码<br> lsh:string; //银行流水<br> xm:string; //姓名<br> addr:string;<br> nd:string; //年度<br> dj:double;<br> mj:double;<br> gnqshrq:string; //起始日期<br> gnjsrq:string; //结束日期<br> pjh:string; //票据号<br> shfzhlid:string;<br><br> yf:string; //月份<br> shfje:double; //收费金额<br> shm:string; //说明<br> k,i:integer;<br> ibok:boolean;<br><br> lsreturn:string; //收费存储过程执行完标志<br> begin<br> ibok:=true;<br> zjesum:=0;<br> if sl.count=11 then<br> begin<br> xm:=sl.strings[3];<br> addr:=sl.strings[4];<br> dj:=strtofloat(sl.strings[5]);<br> mj:=strtofloat(sl.strings[6]);<br> lsh:= sl.Strings[7];<br> nd:= sl.Strings[8];<br> //pjh:=sl.Strings[i*4 + 3 ];<br> shfje:= strtofloat(sl.Strings[9]);<br> //zjesum:=zjesum+shfje;<br> shm:= sl.Strings[10];<br> yf:=floattostr(monthof(date()));<br> if length(yf)=1 then yf:='0'+yf;<br> dm:=rightstr(FormatDateTime('YYMMDDHHMMSSzz',now()),14)+rightstr(inttostr(athread.ThreadID)+IntToStr(i),6);<br> if nd=gsshknd then shfzhlid:='71' else shfzhlid:='70';<br><br> try //对执行sql语和存储过程进行异常保护<br> begin<br><br> //执行收费的存储过程并返回的票据号等参数<br> query_pjh.Close;<br> query_pjh.SQL.Clear;<br> sql:='execute sp_shf :yhbh,:xm,:lsh,:nd,:shfje,:shm,:yf,:dm,'''+'2222'+ ''',:shfzhlid,:dj,:mj,:addr,jh output,:lsgnqshrq output,:lsgnjsrq output,:lsreturn output;';<br> query_pjh.sql.Add(sql);<br> query_pjh.Parameters.ParamByName('yhbh').Value := yhbh;<br> query_pjh.Parameters.ParamByName('xm').Value := xm;<br> query_pjh.Parameters.ParamByName('lsh').Value := lsh;<br> query_pjh.Parameters.ParamByName('nd').Value := nd;<br> query_pjh.Parameters.ParamByName('shfje').Value := shfje;<br> query_pjh.Parameters.ParamByName('shm').Value := shm;<br> query_pjh.Parameters.ParamByName('yf').Value := yf;<br> query_pjh.Parameters.ParamByName('dm').Value := dm;<br> query_pjh.Parameters.ParamByName('shfzhlid').Value := shfzhlid;<br> query_pjh.Parameters.ParamByName('dj').Value := dj;<br> query_pjh.Parameters.ParamByName('mj').Value := mj;<br> query_pjh.Parameters.ParamByName('addr').Value := addr;<br> query_pjh.Parameters.ParamByName('pjh').Value := '@pjh';<br> query_pjh.Parameters.ParamByName('lsgnqshrq').Value := '@lsgnqshrq';<br> query_pjh.Parameters.ParamByName('lsgnjsrq').Value := '@lsgnjsrq';<br> query_pjh.Parameters.ParamByName('lsreturn').Value := '@lsreturn';<br><br> if query_pjh.Prepared=false then query_pjh.Prepared:=true;<br><br> adocon.BeginTrans;<br> query_pjh.ExecSQL;<br> lsreturn:=query_pjh.Parameters.parambyname('lsreturn').value;<br> if lsreturn='0' then //存储过程未执行完退出<br> begin<br> send('','0','2222','200',athread); //交费交易失败,返回响应码200<br> ADOCon.RollbackTrans; //回滚事务<br> exit;<br> end;<br> pjh:=query_pjh.Parameters.parambyname('pjh').value;<br> gnqshrq:= query_pjh.Parameters.parambyname('lsgnqshrq').value;<br> gnjsrq:= query_pjh.Parameters.parambyname('lsgnjsrq').value;<br> adocon.CommitTrans;<br><br> send(send_cmd,'0','2222','000',athread); //交费交易成功,返回响应码000<br> end;<br> except //如果出现异常<br> begin<br> adocon.RollbackTrans;<br> send('','0','2222','200',athread); //交费交易失败,返回响应码200<br> end;<br> end;<br><br><br> end<br> else<br> begin<br><br> send('','0','2222','200',athread); //数据长度不对数据<br> exit;<br> end;<br> end;<br> //-----收费功能函数结束-------------------------------------------------------//