Is there a way to hook into LLBLGen db connection creation?

Posts   
 
    
AlexP
User
Posts: 16
Joined: 24-Jan-2020
# Posted on: 29-Jun-2020 16:20:32   

Hi,

We're currently using self servicing LLBLGen 4.2 in legacy code (and 5.7 in our new code).

For our legacy code we're looking for a way to execute something (changing the language/culture ... changing it on machine level is not an option) right after a db connection has been created and right before it's being used for the first time.

We're currently thinking about wrapping OracleClientFactory, but I personally don't think that's the way to go. Neither is modifying generated code.

NOTE: We need to execute OracleConnection which doesn't need the LLBLGen framework perse.

Do you have an idea how we can / should approach this?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 29-Jun-2020 23:39:28   

I believe the following thread has the answer to your question. https://llblgen.com/TinyForum/Messages.aspx?ThreadID=26707

Mainly, you'll need to override CreateConnection in a partial class of CommonDaoBase. Then you can get the connection returned by calling the base method of CreateConnection.

AlexP
User
Posts: 16
Joined: 24-Jan-2020
# Posted on: 30-Jun-2020 09:50:25   

public partial class CommonDaoBase
{
    public override DbConnection CreateConnection(string connectionString)
    {
        var connection = base.CreateConnection(connectionString);
        
        if (connection is OracleConnection oracle)
        {
            oracle.ConnectionOpen += e => ConnectionOpen?.Invoke(e);
        }

        return connection;
    }

    public static event OracleConnectionOpenEventHandler ConnectionOpen;
}


CommonDaoBase.ConnectionOpen += e => 
{
    var sessionInfo = e.Connection.GetSessionInfo();

    if (sessionInfo.Language != "AMERICAN")
    {
        sessionInfo.Language = "AMERICAN";
        ...

        e.Connection.SetSessionInfo(sessionInfo);
    }
}

It seems be working, thanks simple_smile