java - 如何使用 Spring JPA 在同一事务中的不同数据库上维护多个 sql 查询

标签 java mysql database hibernate spring-data-jpa

我为 2 个数据库设置了实体和存储库

persistence.xml

<persistence-unit name="user_per_unit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.example.User</class>
    <class>com.example.Order</class>
    <class>com.example.Package</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="false" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
        <property name="hibernate.jdbc.batch_size" value="100" />
        <property name="hibernate.order_inserts" value="true" />
        <property name="hibernate.order_updates" value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    </properties>
</persistence-unit>

<persistence-unit name="stock_per_unit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.example.Stock</class>
    <class>com.example.Graph</class>
    <class>com.example.Change</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="false" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
        <property name="hibernate.jdbc.batch_size" value="100" />
        <property name="hibernate.order_inserts" value="true" />
        <property name="hibernate.order_updates" value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    </properties>
</persistence-unit>

以及applicationContext.xml

<jpa:repositories base-package="com.example.user.repository" entity-manager-factory-ref="userEntityManagerFactory" transaction-manager-ref="userTransactionManager"/>

<bean id="userEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName"    value="user_per_unit" />
    <property name="dataSource"             ref="userDataSource" />
    <property name="jpaVendorAdapter"       ref="jpaVendorAdapter" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">false</prop>
        </props>
    </property>
</bean>

<bean id="userTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="userEntityManagerFactory" />
</bean>

<bean id="userDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="driverClassName" value="${driver}" />
    <property name="url" value="${url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
    <property name="initialSize" value="${size}" />
    <property name="maxTotal" value="${maxtotal}" />
    <property name="maxIdle" value="${maxidle}" />
    <property name="minIdle" value="${minidle}" />
</bean>

<jpa:repositories base-package="com.example.stock.repository" entity-manager-factory-ref="stockEntityManagerFactory" transaction-manager-ref="stockTransactionManager"/>

<bean id="stockEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName"    value="stock_per_unit" />
    <property name="dataSource"             ref="stockDataSource" />
    <property name="jpaVendorAdapter"       ref="jpaVendorAdapter" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">false</prop>
        </props>
    </property>
</bean>

<bean id="stockTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="stockEntityManagerFactory" />
</bean>

<bean id="stockDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="driverClassName" value="${driver}" />
    <property name="url" value="${url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
    <property name="initialSize" value="${size}" />
    <property name="maxTotal" value="${maxtotal}" />
    <property name="maxIdle" value="${maxidle}" />
    <property name="minIdle" value="${minidle}" />
</bean>

现在在我的类里面,我正在使用类似的方法执行多个 CURD 操作

userRepository.updateUser(...);

userRepository.addUser(...);

userRepository.deleteUser(...);

stockRepository.updateUser(...);

stockRepository.addUser(...);

stockRepository.deleteUser(...);

如何在顶部启动事务,如果在此过程中出现问题,请恢复到启动方法之前的原始状态。

最佳答案

我使用 ChainesTransactionManager 来实现此功能

applicationContext.xml

<bean id="chainedTxManager" class="org.springframework.data.transaction.ChainedTransactionManager">
    <constructor-arg>
        <list>
            <ref bean="userTransactionManager"/>
            <ref bean="stockTransactionManager"/>
        </list>
    </constructor-arg>
</bean>

并在我的类上使用注释作为

@Transactional(value = "chainedTxManager")

当任何数据库上的任何查询失败时,我都会抛出异常并自动回滚,如果没有异常,则会提交。

关于java - 如何使用 Spring JPA 在同一事务中的不同数据库上维护多个 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55640457/

相关文章:

java - 夏令时和java

java - 在java中返回对象的最佳实践

mysql - 如何组合三个不同的查询

php - 从慢速查询日志提高 MySQL 查询性能

java - JPA 与 JavaBeans 验证 (JSR 303) 与您的解决方案 : Automatically truncate field before persisting

mysql - 计算机重新启动后数据库值是否应设置回默认值

java - BlockingQueue 对于实现生产者/消费者系统有用吗?

java - Action 监听器,执行 Gui

mysql - 使用 MAX() 从检索到的列中获取相关数据

database - 防止错误数据输入