1--Case

比如t_Order订单表1,t_OrderDetail订单明细表2

下一张订单会往表1插入一条数据,表2会插入多行数据

使用Spring.net事务管理

例如

---Order---订单主信息-------

public class OrderBIL

{

      private void InsertOrderHeader(Order order)

  {

          OrderDAL.InsertOrderHeader(Order order);

  }

}

public class OrderDAL

{

      private void InsertOrderHeader(Order order)

  {

           string insertSQL="insert into Order(orderNo,CreateTime,Creator,CreatName,SupplierCode,SupplierName,ClientCode,ClientName)"

    .........................as so on

  }

}

--订单明细表----OrderDetail-----

public class OrderDetailBIL

{

      private void InsertOrderDetail(Order order)

  {

           OrderDetailDAL.InsertOrderDetail(Order order);

  }

}

public class OrderDetailDAL

{

      private void InsertOrderDetail(Order order)

  {

           string insertSQL="insert into Order(orderNo,CreateTime,Creator,CreatName,SupplierCode,SupplierName,ClientCode,ClientName)"

    .........................as so on

  }

}

public InterFace IOrderService

{

    void CreateOrder(Order OrderHeader,OrderDetail orderDetail);

}

--Service 实现类

public class OrderManageSerivceIMPL:IOrderService

{

      private void CreateOrder(Order OrderHeader,OrderDetail orderDetail)

  {

            OrderBIL.InsertOrderHeader(OrderHeader);

            OrderDetailBIL.InsertOrderDetail(orderDetail);

  }

}

public class OrderManageSerivceTransaction

{

      public OrderManageSerivce OrderManageSerivce{

      private void CreateOrder(Order OrderHeader,OrderDetail orderDetail)

  {

            OrderManageSerivce.CreateOrder(OrderHeader,orderDetail);

  }

}

事务调用----

IOrderService orderService=(IOrderService)BeanFactory.getObject("OrderManageSerivceTransaction");

orderService.CreateOrder(OrderHeader,orderDetail);

---这个是Spring.net事务封装整个过程

如果插入表1成功,表2不成功整个事务回滚--呵呵

 

如果在某一个类中使用了异常处理

比如:

public class OrderDetailBIL

{

      private void InsertOrderDetail(Order order)

  {

         try{

           OrderDetailDAL.InsertOrderDetail(Order order);  

    }

          catch(exception ex)

          {

               case 1: throw new Exception(ex.Message);

               case 2: Log.WriteInfo(ex.Message);

          }

  }

}

 

case 1: Spring事务运行到这的时候,将回滚

case 2: Spring事务不回滚--将回出现这样情况---表1插入数据成功,表2插入数据失败

 

-----------好的东东下回分解--------------------------------------------------------

 

 

  

posted on 2013-12-13 12:10  W晴空  阅读(203)  评论(0编辑  收藏  举报