【问题标题】:Tansactional context of Spring and Spring-data-jpaSpring 和 Spring-data-jpa 的事务上下文
【发布时间】:2014-02-17 14:32:22
【问题描述】:

我正在开发一个导入模块,我正在从一个 excel 文件中读取数据,填充实体并保存它们。

现在我的问题是我正在处理许多实体,然后我用三种不同的方法拆分数据库上的保存。

这是我的导入方法:

@Transactional(propagation = Propagation.REQUIRES_NEW)
private void readRowFromExcel(Row row){

    for(Row row : sheet){

        Customer customer = readCustomerFromExcel(row)
        CustomerDeatails customerDetails = readCustomerDetailsFromExcel(row)    

        customerService.save(customer,customerDetails);

        MyEntity myEntity = readMyEntityFromExcel(row);

        myEntityService.save(myEntity)

    }   


}

这是客服中的保存方法

@Transactional(propagation = Propagation.REQUIRED)
public void save(Customer customer, CustomerDetails customerDetails){

            customerRepository.save(customer);
            customerDatailsRepository.save(customer);

}

这是 myEntity 服务中的保存方法:

@Transactional(propagation = Propagation.REQUIRED)
public void save(MyEntity myEntity){

    myEntityRepository.save(myEntity)

}

现在我的问题是,如果我在以下代码行中遇到异常

MyEntity myEntity = readMyEntityFromExcel(row);

实体 customer 和 customerDetails 已经是持久的,而只有在一切正常时我才应该保存

我的 spring-context.xml

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${db.driverClass}" />
        <property name="url" value="${db.connectionURL}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" 
        p:entityManagerFactory-ref="entityManagerFactory">
        <property name="persistenceUnitName" value="myUnit" />
    </bean>

    <!-- 
    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <description>Allow spring to configure hibernate specific settings</description>
    </bean>
     -->
    <bean id="eclipseLinkJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:dataSource-ref="dataSource" 
        p:persistenceUnitName="myUnit"
        p:jpaVendorAdapter-ref="eclipseLinkJpaVendorAdapter">
        <property name="jpaPropertyMap">
            <map>
                <entry key="eclipselink.cache.shared.default" value="false" />
                <entry key="eclipselink.weaving" value="false" />
            </map>
        </property>
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

我该如何解决?

【问题讨论】:

    标签: java spring jpa spring-data-jpa


    【解决方案1】:

    解决方案很简单:在进行任何数据库持久化之前移动从 excel 中读取的行:

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    private void readRowFromExcel(Row row){
    
        for(Row row : sheet){
            //Reading from Excel
            Customer customer = readCustomerFromExcel(row)
            CustomerDeatails customerDetails = readCustomerDetailsFromExcel(row);
            MyEntity myEntity = readMyEntityFromExcel(row);
    
            //DB persisting
            customerService.save(customer,customerDetails);
            myEntityService.save(myEntity)
        }   
    
    }
    

    【讨论】:

    • 如果我尝试保存时发生异常(myEntity)?
    • 因为你在一个事务中,所以一切都应该回滚。如果不是这种情况,那么您的数据源可能不是 JTA,或者您的数据库表不支持事务。
    猜你喜欢
    • 2020-09-06
    • 2017-12-23
    • 2013-05-30
    • 2013-09-13
    • 1970-01-01
    • 2015-05-04
    • 2011-08-06
    • 2018-08-07
    • 2011-07-22
    相关资源
    最近更新 更多