哪个SessionFactory应该用于transactionManager? [英] which SessionFactory should be use for transactionManager?

查看:93
本文介绍了哪个SessionFactory应该用于transactionManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < bean id =projectServiceclass =org.springframework.transaction.interceptor.TransactionProxyFactoryBean> 
< property name =transactionManagerref =transactionManager/>
< property name =target>
< bean class =com.company.project.company.services.ServiceImplinit-method =init>

< property name =projectdaoref =projectdao/>

< / bean>
< / property>
< property name =transactionAttributes>
<道具>
< 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>
< /道具>
< / property>
< / bean>

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

editted



 < bean id =transactionManager

class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactory> //我的HRappsdao使用相同的
< ref local =sessionFactory/>
< / property>
< / bean>


解决方案

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

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

在Spring 2.5中,请考虑使用 new < tx:jta-transaction-manager /> 9.8。应用服务器专用集成以获取更多详细信息。



如果您使用的是旧版本的Spring,则需要配置<$ c手动添加$ c> JtaTransactionManager 。这将需要您的应用程序服务器的一些知识,因为JTA TransactionManager的JNDI位置是特定于每个J2EE服务器的。



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




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

 <?xml version =1.0encoding = 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/弹簧-TX-2.5.xsd>

< tx:jta-transaction-manager />

<! -
< bean id =transactionManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactory>
//我的HRappsdao使用相同的
< ref local =sessionFactory/>
< / property>
< / bean>
- >

...

< / beans>

请注意,您需要类似 JOTM 与Tomcat或Jetty。您应该考虑转移到J2EE应用服务器,如JBoss或Glassfish。


 <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>

解决方案

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.

这篇关于哪个SessionFactory应该用于transactionManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆