Directory
Spring. NET + nhib.pdf + ASP. MVC + easyui practices (1)
Spring. NET + nhib.pdf + ASP. MVC + easyui practices (2)
Spring. NET + nhib.pdf + ASP. MVC + easyui Practices (3)
Spring. NET + nhib.pdf + ASP. MVC + easyui Practices (4)
Test spring's support for Transaction Management
Propagation_required |
Supports the current transaction. If no transaction exists, a new transaction is created. This is the most common choice. |
Propagation_supports |
Supports the current transaction. If no transaction exists, it is executed in non-transaction mode. |
Propagation_mandatory |
Supports the current transaction. If no transaction exists, an exception is thrown. |
Propagation_requires_new |
Create a new transaction. If a transaction exists, the current transaction is suspended. |
Propagation_not_supported |
The operation is performed in non-transaction mode. If a transaction exists, the current transaction is suspended. |
Propagation_never |
It is executed in non-transaction mode. If a transaction exists, an exception is thrown. |
Propagation_nested |
If a transaction exists, it is executed in the nested transaction. If no transaction exists, perform a similar operation as propagation_required. |
The configuration of the Transaction Manager in manager. XML is as follows:
<! -- Transaction Manager --> <Object ID = "basetransactionmanager" type = "Spring. transaction. interceptor. transactionproxyfactoryobject, spring. data "abstract =" true "> <property name =" platformtransactionmanager "ref =" transactionmanager "/> <property name =" transactionattributes "> <name-values> <! -- Add --> <add key = "Save *" value = "propagation_required"/> <! -- Modify --> <add key = "Update *" value = "propagation_required"/> <! -- Delete --> <add key = "delete *" value = "propagation_required"/> <! -- Get --> <add key = "get *" value = "propagation_required"/> <! -- Browse --> <add key = "find *" value = "propagation_supports, readonly"/> <! -- Search --> <add key = "Search *" value = "propagation_supports, readonly"/> <! -- Report --> <add key = "query *" value = "propagation_supports, readonly"/> <! -- Load --> <add key = "load *" value = "propagation_supports, readonly"/> <! -- Report --> <add key = "report *" value = "propagation_supports, readonly"/> <! -- Others --> <add key = "*" value = "propagation_supports"/> <! -- <Add key = "*" value = "propagation_required"/> --> </name-values> </property> </Object>
If you use <add key = "*" value = "propagation_required"/>, the methods injected into the target Bll are not saved. All Methods Starting with deletee are executed in transaction mode.
If you want to add a transaction to a method separately, select <add key = "*" value = "propagation_supports"/>
Add the [transaction] Mark before the corresponding function, and add spring. Data. nhibernate32.dll to the project.
For example, to add a transaction-driven method to userdepinfomanager. CS in Bll, the interface in ibll must be modified accordingly.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using FinanceWebSys.Model;using FinanceWebSys.IBLL;using Spring.Transaction.Interceptor;namespace FinanceWebSys.BLL{ public class UserDepInfoManager:GenericManager<UserDepInfo>,IUserDepInfoManager { public IList<UserDepInfo> LoadAllByPage(out long total, int page, int rows, string order, string sort) { return ((FinanceWebSys.IDao.IUserDepInfoRepository)(this.CurrentRepository)).LoadAllByPage(out total, page, rows, order, sort); } [Transaction] public void CreateUser(UserDepInfo user) { ((FinanceWebSys.IDao.IUserDepInfoRepository)(this.CurrentRepository)).Save(user); if (user.DepID < 2) { throw new Exception("wrong"); } } }}
Homecontroller tests
public class HomeController : Controller { static log4net.ILog logger = log4net.LogManager.GetLogger("Logger"); public string Message { get; set; } public IUserDepInfoManager UserDepInfoManager { get; set; } public ActionResult Index() { var a = UserDepInfoManager.Get(1); ViewBag.Message = a.DepName + a.Id; try { log4net.Config.XmlConfigurator.Configure(); UserDepInfo user = new UserDepInfo { DepID = 1, DepName = "liuhuan" }; UserDepInfoManager.CreateUser(user); } catch (Exception ex) { logger.Error(ex); throw ex; } return View(); }
Add web. config Configuration
<Configsections> ...... <Sectiongroup name = "Spring"> ...... <! -- <Section name = "objects" type = "Spring. context. support. defaultsectionhandler, spring. core "/> --> <section name =" parsers "type =" Spring. context. support. namespaceparserssectionhandler, spring. core "/> </sectiongroup> </configsections> <! -- Spring configuration file --> <spring> <parsers> <! -- <Parser type = "Spring. data. config. databasenamespaceparser, spring. data "/> --> <parser type =" Spring. transaction. config. txnamespaceparser, spring. data "/> </parsers> ...... </Spring>
Modify manager. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Objects xmlns = "http://www.springframework.net" xmlns: DB = "http://www.springframework.net/database" xmlns: Tx = "http://www.springframework.net/tx"> <Object ID = "transactionmanager" type = "Spring. data. nhib.pdf. hibernatetransactionmanager, spring. data. nhibernate32 "> <property name =" dbprovider "ref =" dbprovider "/> <property name =" sessionfactory "ref =" nhibernatesessionfactory "/> </Object> <! -- Transaction aspect --> <TX: Attribute-driven/>
Full web. config
<? XML version = "1.0" encoding = "UTF-8"?> <! -- For more information about how to configure ASP. NET applications, visit the http://go.microsoft.com/fwlink? Linkid = 169433 --> <configuration> <configsections> <! -- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink? Linkid = 237468 --> <section name = "entityframework" type = "system. data. entity. internal. configfile. entityframeworksection, entityframework, version = 5.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "requirepermission =" false "/> <! -- Spring injection to MVC --> <sectiongroup name = "Spring"> <section name = "context" type = "Spring. context. support. mvccontexthandler, spring. web. mvc3 "/> <! -- <Section name = "objects" type = "Spring. context. support. defaultsectionhandler, spring. core "/> --> <section name =" parsers "type =" Spring. context. support. namespaceparserssectionhandler, spring. core "/> </sectiongroup> <! -- Log4net node --> <section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/> <! -- Database settings --> <section name = "databasesettings" type = "system. configuration. namevaluesectionhandler"/> </configsections> <! -- Spring configuration file --> <spring> <parsers> <! -- <Parser type = "Spring. data. config. databasenamespaceparser, spring. data "/> --> <parser type =" Spring. transaction. config. txnamespaceparser, spring. data "/> </parsers> <context> <! -- Web --> <resource uri = "file ://~ /Configs/controllers. xml "/> <! -- BLL --> <resource uri = "file ://~ /Configs/manager. xml "/> <! -- Dao --> <resource uri = "file ://~ /Configs/Repository. xml "/> </context> </spring> <! -- Database connection string --> <databasesettings> <add key = "provider" value = "Npgsql-2.0"/> <add key = "connectionstring" value = "Server = 127.0.0.1; port = 5432; user id = Postgres; Password = zllzh; database = financewebdb; "/> <add key =" dialect "value =" nhib.pdf. dialect. postgresqldialect "/> <add key =" driver_class "value =" nhib.pdf. driver. npgsqldriver "/> <! -- Automatically create a table --> <add key = "hbm2ddl. Auto" value = "Update"/> </databasesettings> <! -- Log4net --> <log4net DEBUG = "true"> <appender name = "logfileappender" type = "log4net. appender. fileappender "> <Param name =" file "value =" logs \ log. log "/> <Param name =" datepattern "value =" mm-dd hh: MM "/> <Param name =" appendtofile "value =" true "/> <layout type =" log4net. layout. patternlayout "> <Param name =" conversionpattern "value =" % d [% T] %-5 p % C [% x]-% m % N "/> </Layout> </appender> <appender name = "httpt Raceappender "type =" log4net. appender. aspnettraceappender "> <layout type =" log4net. layout. patternlayout "> <Param name =" conversionpattern "value =" % d [% T] %-5 p % C [% x]-% m % N "/> </Layout> </appender> <appender name = "eventlogappender" type = "log4net. appender. eventlogappender "> <layout type =" log4net. layout. patternlayout "> <Param name =" conversionpattern "value =" % d [% T] %-5 p % C [% x]-% m % N "/> </Layout> </Appender> <appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender "> <Param name =" file "value =" logs/log. log "/> <Param name =" appendtofile "value =" true "/> <Param name =" maxsizerollbackups "value =" 10 "/> <Param name =" maximumfilesize "Value = "100 K"/> <Param name = "rollingstyle" value = "size"/> <Param name = "staticlogfilename" value = "true"/> <layout type =" log4net. layout. patte Rnlayout "> <Param name =" conversionpattern "value =" % d [% T] %-5 p % C [% x]-% m % N "/> </Layout> </appender> <root> <level value = "error"/> <appender-ref = "rollinglogfileappender"/> </root> </log4net> <connectionstrings> <add name = "defaultconnection" connectionstring = "Data Source = (localdb) \ v11.0; initial catalog = aspnet-FinanceWebSys-20121030155901; Integrated Security = sspi; attachdbfilename = | datadirec Supplied | \ aspnet-FinanceWebSys-20121030155901.mdf "providername =" system. data. sqlclient "/> </connectionstrings> <appsettings> <add key =" webpages: version "value =" 2.0.0.0 "/> <add key =" webpages: enabled "value =" false "/> <add key =" preserveloginurl "value =" true "/> <add key =" clientvalidationenabled "value =" true "/> <add key = "unobtrusivejavascriptenabled" value = "true"/> <! -- Spring added --> <! -- <Add key = "Spring. data. nhib.pdf. support. opensessioninviewmodule. sessionfactoryobjectname "value =" nhibernatesessionfactory "/> --> </appsettings> <system. web> <compilation DEBUG = "true" targetframework = "4.5"/>