ActiveRecord: Multiple Database Configuration

Perhaps you have a web application which not only has a database in which it stores its content but also has one to hold logging data (Exceptions, Log Entries).
This can be a common practice used to keep unrelated data separate and to off load the load created by logging data for purely health monitoring reasons.
If you are using Castle ActiveRecord as your ORM (Object Relational Mapping) layer and are familiar with setting it up for one database you may not be familiar with configuring it to handle more than one database for your objects.
ActiveRecord decides which database to send an object to based on a combination of a Abstract base class which inherits from ActiveRecordBase and a separate configuration section for each of those base classes. Each of those base classes must also be registered with ActiveRecord at application startup just like any Entity you plan on using within the application.
Below are the steps you need to take to properly setup your ASP.net web application to use 2 databases.
1. Create a ActiveRecordBase class for each of the databases you will be using. Each entity which inherits from that base class will be mapped to the corresponding database.

public abstract class ApplicationActiveRecordBase : ActiveRecordBase{}

public abstract class LoggingActiveRecordBase : ActiveRecordBase{}

2. Initialize each of the base classes so that ActiveRecord is aware of the classes

        static ActiveRecordInitializer()
        {
            IConfigurationSource source = ActiveRecordSectionHandler.Instance;
            ActiveRecordStarter.Initialize(
                source,
                typeof(LoggingActiveRecordBase),
                typeof(ApplicationActiveRecordBase));
        }

3. All entities belonging to a specific database should inherit from the base class associated with that database in the configuration.

    [DataContract]
    [ActiveRecord(Table = "exceptionentry")]
    public class ExceptionEntry : LoggingActiveRecordBase<ExceptionEntry>
    {

4. Add configuration sections to the ActiveRecord configuration for each database specifying the ActiveRecordBase class that will be used to identify the database.

  <activerecord isWeb="true" isDebug="false" threadinfotype="Castle.ActiveRecord.Framework.Scopes.HybridWebThreadScopeInfo, Castle.ActiveRecord">
 
    <config type="ActiveRecord.BaseClasses.ApplicationActiveRecordBase`1, Data.Core, Version=1.0.0.0, Culture=neutral">
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect" />
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver" />
      <add key="hibernate.connection.connection_string" value="Server=localhost; Database=YOURAPPLICATIONDATABASE; User Id=YOURUSER; Password=YOURPASSWORD" />
    </config>
 
    <config type="ActiveRecord.BaseClasses.LoggingActiveRecordBase`1, Data.Core, Version=1.0.0.0, Culture=neutral">
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect" />
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver" />
      <add key="hibernate.connection.connection_string" value="Server=localhost; Database=YOURLOGGINGDATABASE; User Id=YOURUSER; Password=YOURPASSWORD" />
    </config>
 
  </activerecord

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s