在spring.net中實現事物管理,可以有兩種方式:
1.編程式事物
編程式事物有如下幾步,
a.
2.Ioc容器注入事物
在通過aop注入事物時有多種配置方式
A.在Ioc容器中聲明事物,然後在應用事物的方法上通過.net的attribute機制來應用事物。
示例代碼:
application.xml代碼:
<object id="transactionManager"
type="Spring.Data.Core.TxScopeTransactionManager, Spring.Data">
</object>
<!-- Transaction aspect -->
<tx:attribute-driven/>
應用事物的方法:
[Transaction(TransactionPropagation.Required)]
public bool Save(Models.Student student)
{
object obj = HibernateTemplate.Save(student);
return obj != null;
}
但是必須注意的一點是,如果在nunit下測試時必須通過容器來提交
示例代碼如下:
[Test]
public void TestStudentDAL()
{
bool bo = StudentDAL().Save(new Student("李四", "男", 22, "[email protected]"));
transactionManager.Commit(transactionStatus);
Assert.IsNotNull(bo);
}
B:採用Aop的另外是用方式
如果Ioc容器中的配置是
<object id="transactionManager"
type="Spring.Data.Core.ServiceDomainPlatformTransactionManager, Spring.Data">
</object>
<!-- Transaction aspect -->
<tx:attribute-driven/>
這是針對於web服務的一種配置方式
C:這種配置方式是針對於使用Data Access數據訪問的一種方式
<object id="transactionManager"
type="Spring.Data.Core.AdoPlatformTransactionManager, Spring.Data">
<property name="DbProvider" ref="DebitDbProvider"/>
</object>
<!-- Transaction aspect -->
<tx:attribute-driven/>
D:切面方式的配置
<object id="txAttributePointcut" type="Spring.Aop.Support.AttributeMatchMethodPointcut, Spring.Aop">
<property name="Attribute" value="Spring.Transaction.Interceptor.TransactionAttribute, Spring.Data"/>
</object>
<aop:config>
<aop:advisor id="exceptionProcessAdvisor" order="1"
advice-ref="exceptionAdvice"
pointcut-ref="txAttributePointcut"/>
</aop:config>
E:最後一種配置方式