DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: cuiqingbo
今日帖子: 20
在线用户: 13
导航: 论坛 -> 数据库专区 斑竹:liumazi,waterstone  
作者:
男 loveunidac (loveunidac) ★☆☆☆☆ -
普通会员
2017/4/8 18:51:28
标题:
morMot 的 TSQLDBServerHttpApi 浏览:2761
加入我的收藏
楼主: //server.
procedure TForm1.Button74Click(Sender: TObject);
var
    fProps: TSQLDBUniDACConnectionProperties;
    cServer,cDatabase,cUserId,cUserPwd:RawUTF8;
    fServer: TSQLDBServerHttpApi;
begin
     cServer:='127.0.0.1';
     cDatabase:='test';
     cUserId:='sa';
     cUserPwd:='sa';

     fProps := TSQLDBUniDACConnectionProperties.Create('SQL Server',
          cDatabase,cUserId,cUserPwd);
     fProps.ThreadingMode := tmThreadPool;
     fProps.SpecificOptions.Values['Server'] :='127.0.0.1';
     fProps.SpecificOptions.Values['Port'] := '1433';
     //打开HTTP服务器
     fServer := TSQLDBServerHttpApi.Create(fProps, cDatabase, '9000', '1',
          '1',false,5,nil,tmMainConnection);
end;

//客户端:
procedure TForm1.Button76Click(Sender: TObject);
var

  cServer,cDatabase,cUserId,cUserPwd:RawUTF8;
  client : TSQLDBWinHTTPConnectionProperties;


begin
       cServer:='127.0.0.1:9000';
       cDatabase:='test';
       cUserId:='1';
       cUserPwd:='1';

      client := TSQLDBWinHTTPConnectionProperties.Create(cServer,
          cDatabase,cUserId,cUserPwd);
      if not client.MainConnection.Connected then
          client.MainConnection.Connect;
       client.MainConnection.StartTransaction;
       try
          client.ExecuteNoResult( 'INSERT INTO syslog(frmName,UserID,EventTim
          e,Notes) '+
          ' VALUES(?,?,?,?)',['test','test',now,'test']);
          client.MainConnection.Commit;
       except
          on E: Exception do
          begin
          client.MainConnection.Rollback;
          end;
       end;

end;



研究了一天,可能功力不够,有几个地方还是没理解或者说搞失败了。 :(

fServer := TSQLDBServerHttpApi.Create(fProps, cDatabase, '9000', '1', '1',false,5,nil,tmMainConnection);

  只要启动超过 1 个线程,数据库启动事务就会失败。提示是:
  if session_ID=0 then
     raise ESQLDBRemote.Create('Remote transaction expects authentication/session');
  if connection.Properties.InheritsFrom(TSQLDBConnectionPropertiesThreadSafe) and
     (TSQLDBConnectionPropertiesThreadSafe(connection.Properties).ThreadingMode=tmThreadPool) then
    raise ESQLDBRemote.CreateUTF8('Remote transaction expects %.ThreadingMode<>tmThreadPool: '+
      'commit/execute/rollback should be in the same thread/connection',[connection.Properties]);

   应该意思是:  sqlconnection 不允许为 tmTheadpool 模式, 但我在想如果是Server端,数据库不允许使用线程模式。如果出现大量事务,系统不就卡死了?


请各位有研究的兄弟,多多指点。 谢谢。
----------------------------------------------
-
作者:
男 744840146 (744840146) ▲▲▲▲▲ -
普通会员
2017/4/9 11:03:40
1楼: 不要使用maincconnection,那个是给服务器用的主连接,要使用的话,自己NewConnection,操作完后释放,这个是池来管理的,代码如下,有些东西自己修改成自己的,

ExecuteNoResult,看看代码

with hcProp.NewConnection do
  try
    Connect;
    StartTransaction;
    try
      with NewStatementPrepared('sql', true, true) do
      begin
        Bind(['test', 'test', Now, 'test']);
        ExecutePrepared;
      end;
      Commit;
    except
      on e: Exception do
        Rollback;
    end;
  finally
    Free;
  end;
----------------------------------------------
-
作者:
男 loveunidac (loveunidac) ★☆☆☆☆ -
普通会员
2017/4/9 11:40:01
2楼: 客户端我改成你这样的方式.可以了. 但有个最致命的问题. 当我有多个客户端操作的时候
     starTransaction ; //因为我的操作一般要10-20秒才能完成. 
//我看了他这个方式,好像只支持一个数据库连接.. 这样就会出现大量超时.

 raise ESQLDBRemote.CreateUTF8('Reached %("%/%").StartTransaction_TimeOut=% ms',
      [self,fProxy.ServerName,fProxy.DatabaseName,fProxy.StartTransactionTimeOut]);


  //打开HTTP服务器
  fServer := TSQLDBServerHttpApi.Create(fProps, cDatabase, '9000', '1',          '1',false,2,nil,tmMainConnection);  
  //这里用tmMainConnection 是可以的.不会报错.但是一但改成:tmpThreadPool . 又提示:
raise ESQLDBRemote.CreateUTF8('Remote transaction expects %.ThreadingMode<>tmThreadPool: '+
      'commit/execute/rollback should be in the same thread/connection',[connection.Properties]);


请再给点提示...这个真的搞得我... 多谢了.
----------------------------------------------
-
作者:
男 744840146 (744840146) ▲▲▲▲▲ -
普通会员
2017/4/10 14:42:16
3楼: 服务器设置300秒超时清除池对象

fProps.ConnectionTimeOutMinutes:=300;
----------------------------------------------
-
作者:
男 loveunidac (loveunidac) ★☆☆☆☆ -
普通会员
2017/4/11 9:18:22
4楼: 好的,我试下。谢谢。
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行66.40625毫秒 RSS