Search code examples
subsonic3

Subsonic 3 Simple Repository And Transactions


So this is what I have so far. Am I doing something wrong or is there a bug in 3.0.0.3?

    var Repository = new SimpleRepository("DBConnectionName");

    using (TransactionScope ts = new TransactionScope())
    {
        using (SharedDbConnectionScope scs = new SharedDbConnectionScope("connstring", "providerName"))
        {
            try
            {
                for (int i = 0; i < 5; i++)
                {
                    Supplier s = new Supplier();
                    s.SupplierCode = i.ToString();
                    s.SupplierName = i.ToString();

                    Repository.Add<Supplier>(s);
                }

                ts.Complete();
            }
            catch
            {
            }
        }
    }

I'm getting an error in SubSonic DbDataProvider public DbConnection CurrentSharedConnection { get { return __sharedConnection; }

        protected set
        {
            if(value == null)
            {
                __sharedConnection.Dispose();

etc.. __sharedConnection == null :( Object Null Reference Exception :(


Solution

  • Finally solved this for myself. All of the above code does not work for me (SubSonic 3.0.0.3, using SQLite) but adding BeginTransaction() caused it to work as expected, greatly speeding up the transaction and rolling back the updates if any exceptions occur.

    using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope(Access.Provider))
    {
        using (IDbTransaction ts = sharedConnectionScope.CurrentConnection.BeginTransaction())
        {
            IRepository repo = new SimpleRepository(Access.Provider);
            //Do your database updates
    
            //throw new ApplicationException("Uncomment this and see if the updates get rolled back");
            ts.Commit();
        }
    }
    

    For completeness: Access.Provider is a static property in a helper class for me that returns return SubSonic.DataProviders.ProviderFactory.GetProvider(ConnectionString, "System.Data.SQLite");