我已经看到了很多问题,但他们的所有解决方案仍然不清楚。这是我设置的 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.dao;
@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;
}
}
我在类路径中有所有 cglib 和 aopalliance1.0.jar,正如Spring+JPA @Transactional 中所建议的那样,没有提交
我的代码仍然没有将更改提交到数据库。任何帮助指针都会非常有帮助。
该代码显示了 select 和 insert sql 语句,没有抛出异常,但代码没有提交。@Transactional 注释有什么问题吗?
在这里定义多个实体管理器有什么问题吗?
好的,我发现如果我使用 @Transactional(propagation=Propagation.REQUIRES_NEW)
在我的方法之上,它提交了更改!但根据我的理解,Propagation 的默认值是“REQUIRED”,任何方式都会开始一个新的交易并应该在最后提交?任何指针我现在真的很困惑!
谢谢香港
请注意,我是从 A 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']]]
谢谢香港