Choose Database server on SaveEntity

Posts   
 
    
Posts: 29
Joined: 04-Dec-2008
# Posted on: 05-Dec-2008 18:52:48   

Hi all !

I'm (still) using LLBLGEN V2.6 demo

I'm trying to override DataAccessAdapterBase:: OnSaveEntity method to add a particular logic.

When writing data in the database (for instance when saving an entity), depending on the current schema used (I'm using PostgresSQL 8.3), I want to choose between several different databases, i.e. I want to decide on which database the entity will be actually saved.

This logic is necessary as we have implemented a multi-master replication cluster, and for a given schema we can only write data on one particular database (that is "master" for this schema). So I need to manage as many connection strings as nodes in my cluster.

On other words, I want to read all the data from the local dabase (default connection string) and forward all write statements to the correct databases.

Is it possible to do this ?

First I tried to something easy like :

protected override void OnSaveEntity (IActionQuery saveQuery, IEntity2 entityToSave)
{
   // Logic to determine the connection string to use
   string connectionStringToUse = ....

  this.ConnectionString = connectionStringToUse;
}


But the adapter's connection is already open when the OnSaveEntity method is called... (I saw that OpenConnection is called right before PersitQueue method, which actually calls OnSaveEntity and OnBeforeSaveEntity)

Even if i do the following:


  ...
   this.ConnectionString = connectionStringToUse;
  this.CloseConnection();
  this.OpenConnection();
  ...

It still does no work and keeps using the default connection string (provided in "Main.ConnectionString" section in the app.config file).

(Moreover, I don't really want to close the connection here since I would loose the "_keepConnectionOpen" information.)

Thx a lot for your help !

Michael

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 05-Dec-2008 19:14:56   

Use schema overwriting, which works per call. See this page

Frans Bouma | Lead developer LLBLGen Pro
Posts: 29
Joined: 04-Dec-2008
# Posted on: 05-Dec-2008 22:23:01   

Sorry for the late answer and thanks for yours...

flushed I'm confused because I've already read this page dozens of times, and I do use schema overwriting in my code already (as my application manages multiple projects, one schema per project, all schemas being designed exactly the same way), but what I'm trying to do here is overwrite the server's hostname right before the adapter opens its connection (hence overwrite the connection string), and I can't figure out how to do this with schema name overwriting...

FYI, I'm also managing 3 different servers, using replication to synchronize them...

thx again

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Dec-2008 03:55:26   

What about using the DataAccessAdapter constructor override that receives a connection string?

cnnStr = GetTheAppropiateConnectionString();
DataAccessAdapter = new DataAccessAdapter(cnnStr);

// do something
David Elizondo | LLBLGen Support Team
Posts: 29
Joined: 04-Dec-2008
# Posted on: 07-Dec-2008 14:12:40   

Hi daelmo !

Thx for the answer

You're right, I'll do something like that, or maybe I'll inherit DataAccessAdapter and write my custom construstor that will implement the "GetTheAppropiateConnectionString" logic, so it will be transparent for the others developpers...

I just wanted to make sure that it was not possible to do so on a lower level than the constructor.

thx again simple_smile

michael