zoukankan      html  css  js  c++  java
  • subsonic Transactions

     

    Transactions

    Running transactions is simple - here's a primer for you

    Switching connection strings and performing shared connection transactions about as seamless as possible. It also extends support to Oracle.

    To Change the Active Connection String (Not Necessarily with a Transaction)

    using (SharedDbConnectionScope scope = new SharedDbConnectionScope("new connectionstring "))

    {

        //do stuff using new connection string}

    // returns to default provider connection string

    To Perform a Transaction With a Shared Connection

    using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())

    using (TransactionScope ts = new TransactionScope())

    {

        Product p = new Product(1);

        p.Title = "new title";

        Product p2 = new Product(2);

        p.Title = "another new title";

        // ...

        p.Save();

        p2.Save();

        ts.Close();

    }

    Using System.Transactions

    You can optionally wrap any code you like in a System.Transaction.TransactionScope to take advantage of the DTC. This allows you to do all kinds of operations within the scope of a given transaction. You have to have the DTC service running on your server, however. Note that the DTC is not used in every case and System.Transaction will elevate the transaction to the DTC when it's necessary. If it can run this inside of SQL, it will.

    There are times, though, when the performance tradeoff is needed:

                //use the product object to test the transaction scope

                using (TransactionScopescope = newTransactionScope()) {

                    try {

                        Productp1 = newProduct(1);

                        p1.QuantityPerUnit = "10 boxes x 30 bags";

                        p1.Save("Unit Test");

                        Productp2 = newProduct(2);

                        p2.QuantityPerUnit = "24 - 42 oz bottles";

                        p2.Save("Unit Test");

                        Productp3 = newProduct(3);

                        p3.QuantityPerUnit = "12 - 5500 ml bottles";

                        p3.Save("Unit Test");

                       

                        //other operations...

                       

                        scope.Complete();

                    } catch (System.Data.SqlClient.SqlExceptionx) {

                        //trap/trace/log as needed

                        throwx;

                    }

                }

    Simple Pass-through Transactions

    You can also use our QueryCommandCollection and the DataService to handle some simple pass-through transactions where you don't need to handle any return data.

    To do this, you create multiple QueryCommands (by using straight SQL or pulling them from instanced objects), add them to a QueryCommandCollection, and pass that to the DataService:

    //create a new command collection

    QueryCommandCollectioncoll = newQueryCommandCollection();

    //change some data

    Productp1 = newProduct(1);

    p1.ProductName = "New Name";

    Productp2 = newProduct(2);

    p2.ProductName = "Another new name";

    //add the commands to the collection

    //by pulling them from the objects

    //can also use GetInsertCommand() and GetDeleteCommand()

    coll.Add(p1.GetUpdateCommand(Environment.UserName));

    coll.Add(p2.GetUpdateCommand(Environment.UserName));

    //Hand them to the DataService to execute in the scope

    //of a transaction

    DataService.ExecuteTransaction(coll);

    Last updated by spookytooth on 5/31/2007

  • 相关阅读:
    Backtrace stopped: previous frame identical to this frame (corrupt stack?)
    windows 10 无法启动 windows update 服务 错误 0x80070005 拒绝访问
    error LNK2019: 无法解析的外部符号 __imp_recv,该符号在函数 evthread_notify_drain_default 中被引用
    opencv3.1.0 在控制台程序中报错:winnt.h(6464): error C2872: ACCESS_MASK: 不明确的
    使用OCCI操作Oracle数据库写入中文乱码
    fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h""
    清空资源管理器访问过FTP的账号、密码
    Windows系统查看xxx.dll、xxx.lib文件的导出函数、依赖文件等信息的方法
    ConvertBSTRToString导致的内存泄漏
    mxnet.base.MXNetError: src/imperative/./imperative_utils.h:70: Check failed: inputs[i]->ctx().dev_mask() == ctx.dev_mask() (1 vs. 2)
  • 原文地址:https://www.cnblogs.com/yuanzhengang/p/yuan.html
Copyright © 2011-2022 走看看