NHibernate Spring .NET 向事务代理添加建议

发布于 2024-10-17 02:08:05 字数 3680 浏览 3 评论 0 原文

我使用 Spring .NEt 和 NHibernate 以及 ASP .NET MVC。

最近我决定将事务管理与Spring集成。我了解 spring 提供了一种使用服务上的 [Transaction()] 属性轻松集成事务的方法。我让这个工作。

我的旧架构是使用 AOP 通过建议拦截对服务的调用,并检查是否允许调用此方法。

呼叫 -->发送建议 -->权限建议 -->服务-->执行逻辑--> Tx 建议中的提交/回滚 Tx -->结束通话。

现在,由于我的服务方法上有 Transaction() 属性,我不再需要 Tx Advice。

但是,如何才能使事务在发送到 PermissionAdvice 之前启动。

文件:services.xml

<object id="BusinessLogicServiceBase" type="Winito.Server.Services.Impl.BusinessLogicService, Winito.Server">
<property name="ObjectPersistenceManager" ref="ObjectPersistenceManager" />
<property name="StatisticsStandingsManager" ref="StatisticsStandingsManager" />

....
</object>

<tx:attribute-driven/>

文件:Dao.xml

<object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>

<!-- Database and NHibernate Configuration -->
<!-- SqlServer-2.0 -->
<db:provider id="DbProvider" provider="System.Data.SqlClient" connectionString=""                  />

<!-- NHibernate Configuration -->
<object id="NHibernateSessionFactory" type="Winito.Data.Base.NHibernate.CustomLocalSessionFactoryObject, Winito.Data.Base">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Winito.Core</value>
  </list>
</property>
<property name="HibernateProperties">
  <dictionary>
    <entry key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
    <entry key="use_proxy_validator" value="false" />
    <entry key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
    <entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
    <entry key="connection.isolation" value="ReadCommitted" />
    <!--<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider,NHibernate.Caches.SysCache</property>-->
    <!--<property name="cache.use_second_level_cache">true</property>-->
    <entry key="command_timeout" value="340" />
    <entry key="use_outer_join" value="true" />
    <entry key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
  </dictionary>
</property>

<!-- provides integation with Spring's declarative transaction management features -->
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

<!-- Transaction Management Strategy - local database transactions -->
<object id="transactionManager"        type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate30">

<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="NHibernateSessionFactory"/>

</object>

<!-- Exception translation object post processor -->
<object type="Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data"/>

旧代码:

<object id="BusinessLogicService" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="target" ref="BusinessLogicServiceBase" />
<property name="interceptorNames">
  <list>
    <!-- <value>TimingAdvice</value> -->
    <value>TransactionalAdvice</value>
    <value>PermissionAdviceBusinessLogic</value>
  </list>
</property>
</object>

I am using Spring .NEt and NHibernate along with ASP .NET MVC.

Recently I decided to integrate transaction management with Spring. I understand spring give a way to easily integrate transation using the [Transaction()] attribute on your service. I got this to work.

My old architecture was to intercept calls to the service via advice using AOP and check if you were permitted to call this method.

CALL --> Tx Advice --> Permission Advice --> Service -- > execute logic --> Commit/Rollback Tx in Tx Advice --> End CALL.

Now, I no longer need the Tx Advice because of the Transaction() attribute on my service methods.

However, how do I make it so that the transaction gets started before it makes it to the PermissionAdvice.

File : services.xml

<object id="BusinessLogicServiceBase" type="Winito.Server.Services.Impl.BusinessLogicService, Winito.Server">
<property name="ObjectPersistenceManager" ref="ObjectPersistenceManager" />
<property name="StatisticsStandingsManager" ref="StatisticsStandingsManager" />

....
</object>

<tx:attribute-driven/>

File: Dao.xml

<object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>

<!-- Database and NHibernate Configuration -->
<!-- SqlServer-2.0 -->
<db:provider id="DbProvider" provider="System.Data.SqlClient" connectionString=""                  />

<!-- NHibernate Configuration -->
<object id="NHibernateSessionFactory" type="Winito.Data.Base.NHibernate.CustomLocalSessionFactoryObject, Winito.Data.Base">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Winito.Core</value>
  </list>
</property>
<property name="HibernateProperties">
  <dictionary>
    <entry key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
    <entry key="use_proxy_validator" value="false" />
    <entry key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
    <entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
    <entry key="connection.isolation" value="ReadCommitted" />
    <!--<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider,NHibernate.Caches.SysCache</property>-->
    <!--<property name="cache.use_second_level_cache">true</property>-->
    <entry key="command_timeout" value="340" />
    <entry key="use_outer_join" value="true" />
    <entry key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
  </dictionary>
</property>

<!-- provides integation with Spring's declarative transaction management features -->
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

<!-- Transaction Management Strategy - local database transactions -->
<object id="transactionManager"        type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate30">

<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="NHibernateSessionFactory"/>

</object>

<!-- Exception translation object post processor -->
<object type="Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data"/>

Old Code:

<object id="BusinessLogicService" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="target" ref="BusinessLogicServiceBase" />
<property name="interceptorNames">
  <list>
    <!-- <value>TimingAdvice</value> -->
    <value>TransactionalAdvice</value>
    <value>PermissionAdviceBusinessLogic</value>
  </list>
</property>
</object>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光暖心i 2024-10-24 02:08:05

在关于 声明式事务管理的 Spring 文档中,我读到您可以在 上指定 order 元素属性驱动标签

类似于

<tx:attribute-driven transaction-manager="transactionManager" order="1" />

Order 的值与 aop 建议排序

In the Spring documentation on declarative transaction management, I read that you can specify an order element on the attribute-driven tag.

Something like

<tx:attribute-driven transaction-manager="transactionManager" order="1" />

The value for order is used with the normal rules for aop advice ordering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文