【问题标题】:EntityManager.flush() not flushing (JPA2 (OpenJPA), EJB3, Spring3, Websphere 7)EntityManager.flush() 不刷新(JPA2 (OpenJPA)、EJB3、Spring3、Websphere 7)
【发布时间】:2013-06-26 18:44:32
【问题描述】:

我在我的项目中遇到了一个问题:entityManager.flush() 没有做任何事情,并且刷新只是在提交之前,退出 EJB 时完成。

我的项目在 WebSphere 7 上运行。 我通过 OpenJPA 使用 JPA2。 我正在使用 Spring 进行自动装配。 我正在使用容器管理事务。

相关代码如下sn-ps

persistence.xml

<persistence-unit name="persistenceUnit" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <properties>
        <property name="openjpa.TransactionMode" value="managed" />
        <property name="openjpa.ConnectionFactoryMode" value="managed" />
        <property name="openjpa.DynamicEnhancementAgent" value="true" />
    <property name="openjpa.jdbc.DBDictionary" value="StoreCharsAsNumbers=false" />
        <property name="openjpa.Log" value="SQL=TRACE"/>
    </properties>
</persistence-unit>

applicationContext.xml

<!-- Configure a JPA vendor adapter -->
<bean id="openJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
    <property name="showSql" value="true" />
    <property name="generateDdl" value="false" />
</bean>

<!-- Entity Manager -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/myappDS"/>
</bean>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter" ref="openJpaVendorAdapter" />
</bean>

EJB3 豆

@Stateless(name="JPABaseEntityServiceBean")
@Configurable
public class JPABaseEntityServiceBean implements JPABaseEntityService {

    Logger logger = LoggerFactory.getLogger(JPABaseEntityServiceBean.class);

    @Autowired
    JPABaseEntityDao jpaBaseEntityDao;

    public JPABaseEntity persist(JPABaseEntity jpaBaseEntity) {
        return jpaBaseEntityDao.persist(jpaBaseEntity);
    }

道:

@Repository
public class JPABaseEntityDao implements BaseEntityDao {

    @PersistenceContext
    transient EntityManager entityManager;

    public JPABaseEntity persist(JPABaseEntity jpaBaseEntity) {
        Date now = new Date();
        jpaBaseEntity.setCreatedBy(TO_DO_ME);
        jpaBaseEntity.setUpdatedBy(TO_DO_ME);
        jpaBaseEntity.setUpdatedOn(now);
        jpaBaseEntity.setCreatedOn(now);

        entityManager.persist(jpaBaseEntity);

        entityManager.flush();

        return jpaBaseEntity;
    }

“INSERT”仅在离开 EJB 时执行,这意味着 DAO 内的 entityManager.flush() 不起作用

【问题讨论】:

  • “离开 EJB”是什么意思?
  • 执行“return jpaBaseEntityDao.persist(jpaBaseEntity);”后来自 JPABaseEntityServiceBean(在返回控制器之前)
  • 当您说“仅在离开 EJB 时才执行 INSERT”时,您究竟是什么意思?当您刷新()时应该执行 INSERT,但在提交 EJB 的事务之前,数据在 DB(或其他事务)中不可见。
  • 你好 bkail;我是说当我 flush() 时根本没有执行 INSERT

标签: spring jpa-2.0 ejb-3.0 openjpa websphere-7


【解决方案1】:

好的,以某种方式解决

似乎问题在于实体管理器没有从 WebSphere 获取事务(可能是因为实体管理器是由 Spring 注入的,我没有深入调查)

所以我所做的是让 Spring 控制 EntityManager 中的事务:

1. added <tx:annotation-driven/> and <tx:jta-transaction-manager/> to applicationContext.xml
2. annotated the DAO methods with @Transactional

整个事务仍然由 EJB 处理,这意味着它仍然使用来自 WebSphere 的 CMT 和 JTA

由于依赖地狱,我遇到了很多问题(最让我感到困惑的是 hibernate-core,包括 JBoss 的 javax.transaction 实现,grr),但除此之外,一切似乎都很顺利

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2011-10-23
    • 2015-08-02
    • 1970-01-01
    相关资源
    最近更新 更多