【发布时间】:2014-01-29 15:13:11
【问题描述】:
有一个 OpenJPA 1.2.3 应用程序在 WebSphere 7 上运行,由于某些原因,我们不能对那里的实体管理器使用依赖注入。因此,我们管理它们在事务提交/回滚后手动关闭它们。最近我发现调用EntityManager.close() 需要相当长的时间(大约10% 的整个操作涉及加载~500 个实体)。它所做的是分离在事务期间加载的所有实体。
所以实际上有2个问题:
- 为什么我们需要在实体管理器关闭时分离所有实体?是不是因为 JPA 必须检查所有实体是否脏?
- 由于在某些情况下实体被加载为只读访问 - 是否有可能以某种方式告诉 OpenJPA 这些实体不应该被更新并且不需要将它们标记为脏、更新到 DB 等?
按要求添加我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="DocumentUnit" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>ru.mypackage.Document</class>
<class>ru.mypackage.DocParam</class>
<class>ru.mypackage.DocParamPK</class>
<!-- Some more classes -->
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
</properties>
</persistence-unit>
</persistence>
【问题讨论】: