4

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 :(

Chris Kolenko
  • 1,020
  • 17
  • 32

3 Answers3

1

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");

Thymine
  • 8,775
  • 2
  • 35
  • 47
0

Perhaps switching the SharedDbConnectionScope and TransactionScope around may help.

using (SharedDbConnectionScope scs = new SharedDbConnectionScope("connstring", "providerName"))
{
    using (TransactionScope ts = new TransactionScope())
    {
    }
}
BlackMael
  • 3,198
  • 5
  • 25
  • 42
0

This will happen when Migration is set - On tablemigration the dbconnection will be closed.

Try the SimpleRepository with SimpleRepositoryOptions.None.

Don't know if this is a bug. I think the transactions don't work with SimpleRepository, I've always half of the data saved when throwing an exception in the transaction... perhaps it's only for ActiveRecord? Anybody knows?

Rumpel
  • 31
  • 5