使用 Spring 和 Hibernate 对两个数据库实例进行声明式事务管理

发布于 2024-12-21 05:03:33 字数 2123 浏览 4 评论 0原文

我正在寻找一种解决方案,使用 Spring (V1.3.2) 和 NHibernate (V3.2.0) 以及 Spring 的声明式事务管理来与两个独立的 SQLite 数据库实例进行通信。

目前,我可以从/向两个数据库实例读取和写入,但事务管理仅适用于一个数据库(DbProvider_DB1)。

“为什么”对我来说很清楚,但如何对两个数据库使用声明式事务管理?我需要两个事务管理器吗?如果是,我如何定义第二个并使用它?

这是我的配置,没什么奇怪的,但为了完整起见:


dao.xml

<tx:attribute-driven />

<!-- Datenbankprovider -->
<db:provider id="DbProvider_DB1" provider="SQLite-1.0.72" connectionString="Data Source=db1.db3;Version=3;New=False;" />
<db:provider id="DbProvider_DB2" provider="SQLite-1.0.72" connectionString="Data Source=db2.db3;Version=3;New=False;" />

<!-- SessionFactories -->
<object id="SessionFactory" abstract="true" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
  <property name="HibernateProperties">
    <dictionary>...</dictionary>
  </property>
  <property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

<object id="SessionFactory_DB1" parent="SessionFactory" >
  <property name="DbProvider" ref="DbProvider_DB1" />
</object>

<object id="SessionFactory_DB2" parent="SessionFactory" >
  <property name="DbProvider" ref="DbProvider_DB2" />
</object>

<!-- Transactionmanager -->  
<object id="transactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32">
  <property name="DbProvider" ref="DbProvider_DB1"/>
  <property name="SessionFactory" ref="SessionFactory_DB1"/>
  <property name="TransactionSynchronization" value="Always"/>
</object>

<!-- Data Access Objects -->
<object id="Dao_DB1" type="Dao1, Dao">
  <property name="SessionFactory" ref="SessionFactory_DB1" />
</object>

<object id="Dao_DB2" type="Dao2, Dao">
  <property name="SessionFactory" ref="SessionFactory_DB2" />
</object>

Dao

[Transaction]
public TEntity Save( TEntity entity )
{
  CurrentSession.Save( entity );
  return entity;
}

Thanx

I am searching for a solution, to use Spring (V1.3.2) and NHibernate (V3.2.0) together with the declarative transactionmanagement of Spring to communicate with two independent SQLite database instances.

Currently I can read and write from/to both database instances but the transaction management only works for one database (DbProvider_DB1).

The „why“ is clear for me, but how can I use the declarative transaction management for both databases? Do I need two transaction manager? If yes, how can I define a second one and use it?

Here is my configuration, nothing strange but for the sake of completeness:


dao.xml

<tx:attribute-driven />

<!-- Datenbankprovider -->
<db:provider id="DbProvider_DB1" provider="SQLite-1.0.72" connectionString="Data Source=db1.db3;Version=3;New=False;" />
<db:provider id="DbProvider_DB2" provider="SQLite-1.0.72" connectionString="Data Source=db2.db3;Version=3;New=False;" />

<!-- SessionFactories -->
<object id="SessionFactory" abstract="true" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
  <property name="HibernateProperties">
    <dictionary>...</dictionary>
  </property>
  <property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

<object id="SessionFactory_DB1" parent="SessionFactory" >
  <property name="DbProvider" ref="DbProvider_DB1" />
</object>

<object id="SessionFactory_DB2" parent="SessionFactory" >
  <property name="DbProvider" ref="DbProvider_DB2" />
</object>

<!-- Transactionmanager -->  
<object id="transactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32">
  <property name="DbProvider" ref="DbProvider_DB1"/>
  <property name="SessionFactory" ref="SessionFactory_DB1"/>
  <property name="TransactionSynchronization" value="Always"/>
</object>

<!-- Data Access Objects -->
<object id="Dao_DB1" type="Dao1, Dao">
  <property name="SessionFactory" ref="SessionFactory_DB1" />
</object>

<object id="Dao_DB2" type="Dao2, Dao">
  <property name="SessionFactory" ref="SessionFactory_DB2" />
</object>

Dao

[Transaction]
public TEntity Save( TEntity entity )
{
  CurrentSession.Save( entity );
  return entity;
}

Thanx

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

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

发布评论

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

评论(1

阳光下的泡沫是彩色的 2024-12-28 05:03:33

解决方案是使用 TxScopeTransactionManager 代替 HibernateTransactionManager。

<object id="transactionManager" type="Spring.Data.Core.TxScopeTransactionManager, Spring.Data">
    <property name="TransactionSynchronization" value="Always"/>
</object>

The solution is to use the TxScopeTransactionManager instaed of HibernateTransactionManager.

<object id="transactionManager" type="Spring.Data.Core.TxScopeTransactionManager, Spring.Data">
    <property name="TransactionSynchronization" value="Always"/>
</object>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文