transactionManager 应该使用哪个 SessionFactory?

发布于 2024-08-16 06:11:09 字数 1485 浏览 7 评论 0原文

 <bean id="projectService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="target">
        <bean class="com.company.project.company.services.ServiceImpl" init-method="init">

             <property name="HRappsdao" ref="HRappsdao"/>
               <property name="projectdao" ref="projectdao"/>

        </bean>
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="store*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED</prop>
            <prop key="bulkUpdate*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
        </props>
    </property>
</bean>

我有2个数据源HRappsdao和projectdao,两者都使用不同的sessionFactory。在这种情况下,我的transactionmanager应该使用哪个sessionfactory?(hrappsdao或projectdao)?

编辑过的

<bean id="transactionManager" 

class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" >  //my HRappsdao using same 
            <ref local="sessionFactory"/>
        </property>
    </bean>
 <bean id="projectService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="target">
        <bean class="com.company.project.company.services.ServiceImpl" init-method="init">

             <property name="HRappsdao" ref="HRappsdao"/>
               <property name="projectdao" ref="projectdao"/>

        </bean>
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="store*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED</prop>
            <prop key="bulkUpdate*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
        </props>
    </property>
</bean>

i have 2 datasource HRappsdao and projectdao, both are using different sessionFactory. in this case, my transactionmanager should be using which sessionfactory? (hrappsdao or projectdao) ?

editted

<bean id="transactionManager" 

class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" >  //my HRappsdao using same 
            <ref local="sessionFactory"/>
        </property>
    </bean>

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

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

发布评论

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

评论(1

ぃ双果 2024-08-23 06:11:09

实际上,您没有显示事务管理器的配置,因此我不太确定您当前使用的是什么,但是引用文档:

JTA(通常通过 JtaTransactionManager)对于访问同一事务中的多个事务资源是必需的。

对于 Spring 2.5,请考虑使用“new< /a>" 配置元素,用于自动检测基于 JTA 的底层事务平台(适用于大多数应用服务器)。请参阅 9.8 章。特定于应用程序服务器的集成,了解更多详细信息。

如果您使用的是旧版本的 Spring,则需要手动配置 JtaTransactionManager。这需要您对应用程序服务器有一定的了解,因为 JTA TransactionManager 的 JNDI 位置特定于每个 J2EE 服务器。

请提供更多详细信息(例如,如果您需要更多指导,请提供 Spring 的版本和您正在使用的应用程序服务器)。


更新:正如我所说,当使用多个数据源时,您需要使用 JtaTransactionManager 而不是 HibernateTransactionManager (请参阅 javadoc)。如果您使用的是 Spring 2.5,请按如下方式更新 Spring 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"       
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <tx:jta-transaction-manager />

    <!-- 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            //my HRappsdao using same
            <ref local="sessionFactory" />
        </property>
    </bean>
    -->

    ...

</beans>

请注意,您将需要类似 JOTM 的内容与 Tomcat 或 Jetty 一起使用。您也许应该考虑迁移到 J2EE 应用程序服务器,例如 JBoss 或 Glassfish。

Actually, you're not showing the configuration of your transaction manager so I'm not really sure of what your are currently using but, quoting the documentation:

JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction.

With Spring 2.5, consider using the "new" <tx:jta-transaction-manager/> configuration element for automatic detection of the underlying JTA-based transaction platform (works with most app servers). See the chapter 9.8. Application server-specific integration for more details on this.

If you are using an older version of Spring, you'll need to configure your JtaTransactionManager manually. This will require some knowledge of your application server as the JNDI location of the JTA TransactionManager is specific to each J2EE server.

Please provide more details (like the version of Spring and the application server you are using if you need more guidance).


UPDATE: As I said, when using multiple datasources, you need to use the JtaTransactionManager and not the HibernateTransactionManager (see the javadoc). If you are using Spring 2.5, update your Spring configuration as below:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"       
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <tx:jta-transaction-manager />

    <!-- 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            //my HRappsdao using same
            <ref local="sessionFactory" />
        </property>
    </bean>
    -->

    ...

</beans>

Note that you'll need something like JOTM with Tomcat or Jetty. You should maybe consider moving to a J2EE app server like JBoss or Glassfish.

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