Changing databases at runtime

Hello.


Is it possible with RemoteDB for the client to change databases at runtime, i.e. after the server has started?

Thanks.
Thom

Yes and no. There is no direct way to do that. But there are some workarounds that can be used for that. But I wonder what is your exact need? Note that you can have multiple TRemoteDBModule in your server, for example, http://server/db1 points to one database, http://server/db2 points to another database. So the most common approach our users are doing is to pre-create all possible databases in different addresses, then the client just chooses the URL they want to connect to. Being more flexible than that, i.e., choosing any arbitrary database is still possible, as I said, with some workarounds, but I wonder why it would be needed and if it doesn't decrease security.

Our customers are accountant offices where each client they create in the system database get a physical database created for it. So 20 clients = 20 records in the Clients system database = 20 client databases.

What you are proposing is that you could loop the Client table in the system database and create an address for each record:

http://server/Client1
http://server/Client2
http://server/Client3
etc.

So something like:

FServer := THttpSysServer.Create;
try
  while not ClientDS.Eof do
  begin
    Url := Format('http:/server/%s', [ClientDS.FieldByName('DbName').AsString]);
    FServer.AddModule(TRemoteDBModule.Create(Url, TDBConnectionFactory.Create(
     function: IDBConnection
     begin
       Result := CreateIDBConnection;
     end
    )));
    ClientDS.Next;
  end;
.......

What would you do when a new client database is created? Can you use FServer.AddModule when the server is running? Or does the server need to be stopped?

// Thom

Yes, that's how you should do it. I know one of our customers is using that exactly approach. Of course, you should pass the database name to your CreateIDBConnection function so it creates the connection properly (pointing to the correct database). 

You have to stop the server to add modules to the server.