简体   繁体   English

Spring的具有不同transactionManagers的嵌套事务

[英]Spring's nested transactions with different transactionManagers

I'm new to Spring's transaction management having troubles to tackle the following scenario of nested transactions while integrating Spring (3.2) and Hibernate (3.6). 我是Spring事务管理的新手,在集成Spring(3.2)和Hibernate(3.6)时难以解决以下嵌套事务的情况。

I've declared two appContext files as following. 我已经声明了两个appContext文件,如下所示。

File1) applicationContext-student.xml File1)applicationContext-student.xml

    <bean id="studentProjSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
        ...**dataSource1_on_Machine1**...
    </bean>
    <bean id="studentProjTransactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="studentProjSessionFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="studentProjTransactionManager" />

    <bean id="studentDao" class="com.my.univ.employee.dao.studentHibDao" scope="singleton" />

    <bean id="studentService" class="com.my.univ.student.service.studentServiceImpl" scope="singleton">
            <property name="studentDao" ref="studentDao" />
    </bean>

File2) applicationContext-employee.xml File2)applicationContext-employee.xml

<bean id="employeeProjSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
            ........**dataSource2_on_Machine2**...
</bean>
<bean id="employeeProjTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="employeeProjSessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="employeeProjTransactionManager" />

<bean id="employeeDao" class="com.my.univ.employee.dao.EmployeeHibDao" scope="singleton" />

<bean id="employeeService" class="com.my.univ.employee.service.EmployeeServiceImpl" scope="singleton">
        <property name="employeeDao" ref="employeeDao" />
</bean>

Imported above two files in the following file. 在上面的两个文件中导入以下文件。

File3) applicationContext-university.xml File3)applicationContext-university.xml

<import resource="applicationContext-student.xml" />
<import resource="applicationContext-employee.xml" />

<bean id="personService" class="com.my.univ.person.service.PersonServiceImpl" scope="singleton">
        <property name="studentService" ref="studentService" />
        <property name="employeeService" ref="employeeService" />
</bean>

Questions 问题

Let's assume that method level @Transactional annotations are provided with the right txManager names in studentService and employeeService but not in personService. 假设在方法级别@Transactional注释中,在StudentService和employeeService中提供了正确的txManager名称,而在personService中没有提供。

Q1) If I declare a method in personService as @Transactional, which txManager gets picked? Q1)如果我在personService中将方法声明为@Transactional,则选择哪个txManager?

Q2) How does the nested txManager scenario work if the txManagers in the hierarchy are different from each other? Q2)如果层次结构中的txManager彼此不同,则嵌套的txManager方案如何工作?

Ex: If a @Transactional method in personService invokes a @Transactional method in studentService and then another @Transactional method in employeeService (with in the same method of personService). 例如:如果personService中的@Transactional方法调用了StudentService中的@Transactional方法,然后employeeService中的了另一个@Transactional方法(具有与personService相同的方法)。

Q3) How does the commit, rollback elements work in above scenario. Q3)在上述情况下,提交,回滚元素如何工作。

Q4) Readonly operations vs Read/Write operations in above scenario. Q4)在上述情况下,只读操作与读/写操作比较。

It'd be great if anybody could clarify the above. 如果有人可以澄清以上内容,那就太好了。

Thanks. 谢谢。

The @Transactional annotation takes as a value the bean name of the transaction manager. @Transactional批注将事务管理器的bean名称作为值。 From the documentation : 文档中

public abstract String value
A qualifier value for the specified transaction.
May be used to determine the target transaction manager, matching the qualifier value (or the bean name) of a specific PlatformTransactionManager bean definition.

Default:
""

So in your case, explicitly defining: 因此,在您的情况下,显式定义:

@Transactional("studentProjTransactionManager")
@Transactional("employeeProjTransactionManager")

should wrap transactionally using the right transaction manager. 应该使用正确的交易管理器进行交易包装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM