提问者:小点点

Spring-JPA-m@TransactionalDB提交数据


我看到了很多问题,但他们所有的解决方案仍然不清楚。这是我设置的Spring Context文件

 <?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:context="http://www.springframework.org/schema/context"
    ..
    ...


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

    ...
    ...

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

    <bean id="entityManagerFactory1"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        autowire="byName">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="packagesToScan" value="com.my.dom.domain" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle10gDialect
                </prop>             
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.fetch_size">50</prop>
                <prop key="hibernate.jdbc.batch_size">10</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="persistenceUnitName" value="PersistenceUnit1"></property>
    </bean>

    <bean id="EntityManagerFactory2"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        autowire="byName">
        <property name="dataSource" ref="myDataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="packagesToScan" value="com.my.dom.domain" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.H2Dialect
                </prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.fetch_size">50</prop>
                <prop key="hibernate.jdbc.batch_size">10</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
     ..
     ...     


    <bean id="entityManager1" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory1" />
    </bean>
    <tx:annotation-driven transaction-manager="entityManager1" />

    <bean id="EntityManager2" factory-bean="EntityManagerFactory2" factory-method="createEntityManager" autowire="byName" scope="prototype"/>
    <tx:annotation-driven transaction-manager="EntityManager2" />


    <context:component-scan base-package="com.my.dom" />

    <jpa:repositories base-package="com.my.dom.repository"
        entity-manager-factory-ref="entityManagerFactory"
        transaction-manager-ref="transactionManager" />

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>


</beans>

所有数据源和其他连接bean都已就位并正常工作。

我的DAO代码如下包com.my. dom.道;

@Component
public class MyDAOImpl implements MyDAO {

@PersistenceContext(unitName="PersistenceUnit1")
EntityManager entityManager; // this is IAR EntityManager.

@Override
@Transactional
public boolean saveMyGroups(
        List<MyGroups> theGroups) {

    logger.info("Entering method -  saveFuturetheGroups ");
    //entityManager.getTransaction().begin();
    for(MyGroups pg : theGroups){
        MyGroups attachedENtity = entityManager.merge(pg);
        entityManager.persist(pg);
    }
    //entityManager.getTransaction().commit();
    entityManager.flush();
    logger.info("Exiting method -  saveFuturetheGroups ");
    //List<MyGroups> savedEntities = futureProcGrpRepository.save(theGroups);
    return true;
}

}

正如Spring JPA@Transactional未提交中建议的那样,我在类路径中包含了所有cglib和aopalliance1.0. jars

我的代码仍然没有将更改提交给DB。任何帮助指针都会很有帮助。

代码显示选择和插入sql语句,没有异常抛出,但代码没有提交。@Transactional注释中是否有任何问题?

这里可以定义多个实体管理器吗?

好的,我发现如果我使用@Transactional(传播=传播。REQUIRES_NEW)

在我的方法之上,它提交了更改!!但是根据我的理解,传播的默认值是“必需的”,任何方式都会开始新的传输,并且应该在结束时提交?任何指针我现在真的很困惑!!

谢谢HK

只是一个注意事项,我正在从一个Junit测试用例中运行这个,有没有可能它正在明确地回滚更改?我在日志中看到了类似的内容

INFO : org.springframework.test.context.transaction.TransactionalTestExecutionListener - Rolled back transaction after test execution for test context [[TestContext@a5503a testClass = ProcessingGroupsTestCase, testInstance = null(com.wellmanage.dos.processingGroups.ProcessingGroupsTestCase), testMethod = testSave@ProcessingGroupsTestCase, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1363f5a testClass = ProcessingGroupsTestCase, locations = '{classpath:applicationContext.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]]

谢谢HK


共3个答案

匿名用户

好吧,伙计们!!这都是我的愚蠢…我相信我应该承认这一点。一直以来,我都在通过我的测试用例运行整个DAO和服务,我只是忽略了我的测试用例中的以下内容。

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional

defaultRollback=true是罪魁祸首。

我学到了什么教训。永远不要忽视任何代码,并始终尝试从代码开始发现问题。

因此,要使Spring与JPA一起工作-拥有适当的上下文条目(如我的示例)-使用@Transactional,如我的DAO。

你应该都准备好了。

谢谢HK

匿名用户

默认情况下,defaultRollback为junit测试设置为true,这意味着即使您没有指定@TransactionConfiguration,通过junit测试更新的数据也会回滚。

显式设置defaultRollback=false将解决此问题。

@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)

匿名用户

您要在代码上指定您的事务管理器

@Transactional("entityManager1")
public boolean saveMyGroups(