Aspect # Application - Transaction Processing

xiaoxiao2021-03-06  55

The following is to describe the transaction processing in the AOP application, which uses NHibernate because the database access is used, so the transaction is used in the TransactionInterceptor (transaction interceptor), then perform implementation methods If the exception is rolling, the transaction is returned. Transaction interceptor

The invoke method code in TransactionInterceptor is as follows:

Public class transactioninterceptor: iMethodInterceptor {publicinvocation infocation {iSession session = sessions.opensession ();

// Store the session into the manager, which is the key to the shared session! SessionManager.setSession (session);

ItraSaction trans = null; try {trans = session.begintransAction (); object result = infoccization.proceed (); trans.Commit (); returnrate;} catch (exception e) {if (trans! = Null) Trans. Rollback (); Throw e;} finally {session.close ();}}} // Class TransactionInterceptor

Business objects and control objects

Product and ENQUIRY are the same as general business objects, this is not posted here.

// Product Control Object Interface for Application Aspect.public Interface iproductco {Void Delete (INT ProductID);} // Interface Productco

// Product Control Object Implementation Code: Public Class Productco: iproductco {public void delete (int product) {// Remove the current session from the session manager. ISession session = sessionmanager.getations (); product product = (product) session. LOAD (TypeOf (Product), PRODUCTID; SESSION.DELETE ("from enquiry e where e.product); session.delete (product);}} // Class Productco

Session factory

public class Sessions {private static ISessionFactory factory; public static ISession OpenSession () {if (factory == null) {Configuration cfg = new Configuration (); cfg.AddXmlFile ( "product.hbm.xml"); cfg.AddXmlFile ( " ENQUIRY.HBM.XML "); factory = cfg.buildsessionFactory ();} return factory.opensession ();}} Note, for simplicity, save multithreaded processing code. Session Manager

The session manager is the key to the share of a conversation in the thread, where TLS (thread local storage) is used to store the ISession.

Public class sessionmanager {public static void setsession (iSession session) {thread.setdata (thread.getnameddatalot ("session"), session);}

Public static iSession getsession () {Object obj = thread.getdata ("session"); if (obj == null) Throw new Exception ("No session object!"); return (iSession) Obj;} } // Class SessionManager For TLS and NamedDataslot (Named Diversity), please refer to .NET SDK

Aspect Configuration Delete the intercept point on the delete method of the service control object Productco, by TransactionInterceptor performs load processing.

Main program code

Static void main (string [] args) {iproductco product = aspectsharpengine.wrap (new productco ()) as iproductco; product.delete (1);} The above method has been successfully run.

转载请注明原文地址:https://www.9cbs.com/read-116751.html

New Post(0)