解决方法如下:有任何不明白可发信到zqszf@21cn.com
定义Session缓冲池
PSessionPool = ^TSessionPool;
TSessionPool = record
Session : TSession;
InUsed : Boolean;
end;
TSessionPooler=class(TThreadList)
private
public
function GetIdleSession: TSession;
procedure ReleaseSession(SessionName: String);
end;
var
SessionPooler : TSessionPooler;
function TSessionPooler.GetIdleSession: TSession;
var i: Integer;
p: PSessionPool;
begin
Result := nil;
while Result=nildo
begin
Sleep(1);
with LockListdo
try
for i:=0 to Count-1do
begin
p := Items;
if not p^.InUsed then
begin
Result := p^.Session;
p^.InUsed := True;
Break;
end;
end;
finally
UnLockList;
end;
end;
end;
procedure TSessionPooler.ReleaseSession(SessionName: String);
var i: Integer;
p: PSessionPool;
begin
with LockListdo
try
for i:=0 to Count-1do
begin
p := Items;
if p^.Session.SessionName=SessionName then
begin
p^.InUsed := False;
Break;
end;
end;
finally
UnLockList;
end;
end;
procedure AddNewSession(Session: TSession);
var p: PSessionPool;
begin
if Assigned(SessionPooler) then
begin
New(p);
SessionPooler.Add(p);
p^.Session := Session;
p^.InUsed := False;
end else
raise Exception.Create('服务器缓冲池没有定义');
end;
生成足够的数据库连接
var aSen: TSession;
aDB: TDatabase;
for i:=1 to MaxSessiondo
//MaxSession自己定义,小于48
begin
aSen := Sessions.OpenSession(MultiSessionID + IntToStr(i));
AddNewSession(aSen);
aDb := TDatabase.Create(Self);
aDb.DriverName := 'MSSQL';//自己改
aDb.DatabaseName := myDatabaseName + IntToStr(i);
aDb.Params.Values['USER NAME'] := 'xxx';
aDb.Params.Values['PASSWORD'] := 'yyy';
aDb.SessionName := aSen.SessionName;
aDb.LoginPrompt := False;
aDb.KeepConnection := True;
end;
生成Session池实例
SessionPooler := TSessionPooler.Create;
每个TRemoteDataModule都重载Lock 和UnLock方法
TMyRemoteDBModule=class(TRemoteDataModule, IAppRModule)
private
aSession: TSession;
aDatabase: TDatabase;
public
procedure Lock;
override;
procedure Unlock;
override;
end;
procedure TMyRemoteDBModule.Lock;
var i: Integer;
begin
inherited;
aSession := SessionPooler.GetIdleSession;
aDatabase := aSession.Databases[0];
for i:=0 to ComponetCount-1do
if Componets is TDBDataSet then
begin
(Componets as TDBDataSet).SessionName := aSession.SessionName;
(Componets as TDBDataSet).DatabaseName := aDatabase.DatabaseName;
end;
end;
procedure TMyRemoteDBModule.UnLock;
begin
SessionPooler.ReleaseSession(aSession.SessionName);
inherited;
end;