Search code examples
delphiiisiis-7.5adoisapi

How can I make ADO database connections in 'TISAPIApplication` before processing incoming requests?


TADOConnection is failing to connect in the application initialization section of Delphi ISAPI App (TISAPIApplication):

Application is built with Delphi XE SPI, running Win 7 64/IIS 7.5 and WinServer 2008 RS2 - it cannot connect with ADO in the global ISAPI application context. (Example code is using MS-SQLServer OLEDB - but we also fail using Sybase ASE provider.)

The following code fails when conn.Open is called - TADOConnection.open never returns - ISAPI app hangs in la-la land, no exception raised:

library ISAPIBareBones;

uses
  ActiveX,
  ADODB,

    (...)

var
  conn: TADOConnection;

begin

  CoInitFlags := COINIT_MULTITHREADED;
  Application.Initialize;

  coinitialize(nil);
  conn := TADOConnection.Create(Application);
  conn.ConnectionString := 'Provider=SQLOLEDB.1;xxx';

//Fails here:

  try
    conn.Open;
  except on e:exception do
    logException(e)
  end;


  Application.WebModuleClass := WebModuleClass;
  Application.Run;

end.

The same code within a specific request handler (Delphi webAction) runs fine.

We suspect a problem with execution privileges in IIS at the ISAPI application level. But as far as we can tell, the entire IIS application stack from the webServer itself down to the specific virtual directory and the ISAPI dll itself are all running under the same credentials with same execution privileges.

Meanwhile, my workaround has been to initialize the database infrastructure from within an http response call (an ISAPI thread), and then simply check that it's initialized on each subsequent call. This works, but encumbers me with some constraints that I'd prefer not to deal with.

How can I make ADO database connections in a TISAPIApplication instance, before handling incoming requests.


Solution

  • I think the problem is that the code you run runs in the dll's main procedure. This part of the initialization is very restrictive e.g. you may not load any dll nor you are allowed to call CoInitialize (see http://msdn.microsoft.com/en-us/library/ms678543%28v=vs.85%29.aspx) . Your ActiveX alls there will cause some troubles which are most likely the reason for your exception.