4

これが私がこれまでに持っているものです。何か間違っているのでしょうか、それとも 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
            {
            }
        }
    }

SubSonic DbDataProvider public DbConnection CurrentSharedConnection { get { return __sharedConnection; でエラーが発生しました。}

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

など.. __sharedConnection == null :( Object Null Reference Exception :(

4

3 に答える 3

1

最後にこれを自分で解決しました。上記のコードはすべて機能しません (SubSonic 3.0.0.3、SQLite を使用) が、追加するBeginTransaction()と期待どおりに機能し、トランザクションが大幅に高速化され、例外が発生した場合は更新がロールバックされます。

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();
    }
}

完全を期すために:Access.Provider私にとってヘルパークラスの静的プロパティであり、それを返しますreturn SubSonic.DataProviders.ProviderFactory.GetProvider(ConnectionString, "System.Data.SQLite");

于 2011-04-16T20:53:14.783 に答える
0

おそらく、SharedDbConnectionScopeとTransactionScopeを切り替えると役立つ場合があります。

using (SharedDbConnectionScope scs = new SharedDbConnectionScope("connstring", "providerName"))
{
    using (TransactionScope ts = new TransactionScope())
    {
    }
}
于 2009-08-22T23:51:31.757 に答える