Spring.Net学习笔记-事务

Posted Kimisme

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring.Net学习笔记-事务相关的知识,希望对你有一定的参考价值。

一、开发环境

操作系统:Win7

编译器:VS2010

二、涉及程序集

Spring.Core.dll

Spring.Data.dll

Common.Logging.dll

三、开发过程

1.项目结构

image

2.IAccountDao.cs

namespace Dao
{
    public interface IAccountDao
    {
        void Create(string name, string userName);

        void Delete(string userName);
    }
}

3.IUserDao.cs

namespace Dao
{
    public interface IUserDao
    {
        void Create(string name, int age);

        void Delete(string name);

        DataSet Get(string name);
    }
}

4.AccountDao.cs

namespace Dao
{
    public class AccountDao : AdoDaoSupport, IAccountDao
    {
        public void Create(string name, string userName)
        {
            AdoTemplate.ExecuteNonQuery(CommandType.Text,
                String.Format("INSERT INTO T_Account (UserName,    AccountName) VALUES (\'{0}\', \'{1}\')", userName, name));
        }

        public void Delete(string userName)
        {
            AdoTemplate.ExecuteNonQuery(CommandType.Text,
                String.Format("DELETE FROM T_Account WHERE UserName = \'{0}\'", userName));
        }
    }
}

5.UserDao.cs

namespace Dao
{
    public class UserDao : AdoDaoSupport, IUserDao
    {
        public void Create(string name, int age)
        {
            AdoTemplate.ExecuteNonQuery(CommandType.Text,
                string.Format("INSERT INTO T_User (UserName,    UserAge) VALUES (\'{0}\', {1})", name, age));
        }

        public void Delete(string name)
        {
            AdoTemplate.ExecuteNonQuery(CommandType.Text,
                string.Format("DELETE FROM T_User WHERE UserName = \'{0}\'", name));
        }

        public DataSet Get(string name)
        {
            return AdoTemplate.DataSetCreate(CommandType.Text,
                string.Format("SELECT * FROM T_User WHERE UserName = \'{0}\'", name));
        }
    }
}

6.IUserService.cs

namespace Service
{
    public interface IUserService
    {
        void SaveData(string name, int age, string accountName);

        void DeleteData(string name);

        DataSet Get(string name);
    }
}

7.UserService.cs

namespace Service
{
    public class UserService : IUserService
    {
        public IUserDao UserDao { get; set; }

        public IAccountDao AccountDao { get; set; }

        [Transaction]
        public void SaveData(string name, int age, string accountName)
        {
            UserDao.Create(name, age);
            AccountDao.Create(accountName, name);
        }


        [Transaction]
        public void DeleteData(string name)
        {
            UserDao.Delete(name);
            throw new Exception("测试数据是否回滚");
            AccountDao.Delete(name);
        }

        [Transaction(ReadOnly = true)]
        public DataSet Get(string name)
        {
            return UserDao.Get(name);
        }
    }
}

8.App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
      <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
    </sectionGroup>
  </configSections>

  <spring>
    <parsers>
      <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data"/>
      <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/>
    </parsers>
    
    <context>
      <!--<resource uri="assembly://Dao/Dao/Dao.xml"/>-->
      <resource uri="config://spring/objects"></resource>
    </context>
    
    <objects xmlns="http://www.springframework.net"
             xmlns:db="http://www.springframework.net/database"
             xmlns:tx="http://www.springframework.net/tx">

      <db:provider id="DbProvider"
                    provider="SqlServer-1.1"
                    connectionString="Server=(local);Database=CIS;Uid=sa;Pwd=woaini;Trusted_Connection=False"/>

      <object id="userDao" type="Dao.UserDao, Dao">
        <property name="AdoTemplate" ref="adoTemplate"/>
      </object>

      <object id="accountDao" type="Dao.AccountDao, Dao">
        <property name="AdoTemplate" ref="adoTemplate"/>
      </object>

      <object id="userService" type="Service.UserService, Service">
        <property name="UserDao" ref="userDao"/>
        <property name="AccountDao" ref="accountDao"/>
      </object>

      <object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
        <property name="DbProvider" ref="DbProvider"/>
        <property name="DataReaderWrapperType" value="Spring.Data.Support.NullMappingDataReader, Spring.Data"/>
      </object>

      <!--事务管理器-->
      <object id="transactionManager"
             type="Spring.Data.Core.AdoPlatformTransactionManager, Spring.Data">
        <property name="DbProvider" ref="DbProvider"/>
      </object>


      <!--事务切面-->
      <tx:attribute-driven/>

    </objects>
  </spring>

</configuration>
View Code

9.控制台文件

namespace SpringNetTrans
{
    class Program
    {
        static void Main(string[] args)
        {
            AdoTransaction();
            Console.ReadKey();
        }

        public static void AdoTransaction()
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            IUserService service = (IUserService)ctx.GetObject("userService");
            service.SaveData("Kimisme", 27, "1233456");
        }


        public static void AdoDeleteTransaction()
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            IUserService service = (IUserService)ctx.GetObject("userService");
            service.DeleteData("Kimisme");
        }
    }
}
View Code

以上是关于Spring.Net学习笔记-事务的主要内容,如果未能解决你的问题,请参考以下文章

Spring.Net学习笔记-依赖注入

Spring.Net学习笔记-属性及构造器注入

Spring.Net学习笔记-设置配置文件参数

Spring.Net学习笔记-容器的使用

Spring.Net 入门学习笔记

Spring.Net学习笔记-数据访问器