【发布时间】:2020-08-17 17:59:32
【问题描述】:
在我的应用程序中,我使用 Eclipselink 作为 OracleDB 的 ORM,但遇到了性能问题。
我正在执行这样的代码:
entityManager
.createNamedQuery(RoleToPermissionEntity.FIND_BY_APPLICATION_ROLE, RoleToPermissionEntity.class)
.setParameter(RoleToPermissionEntity.APPLICATION_ROLES_QUERY_PARAM, applicationRoles)
.getResultList();
使用命名查询:
SELECT mapping
FROM RoleToPermissionEntity mapping
WHERE mapping.applicationRole IN :applicationRoles
ORDER BY mapping.id
实体管理器由@PersistenceContext设置。
对于 3 个给定的应用程序角色,应用程序获得 123 行(从 393 行),每列 9 列(2 个带时区的时间戳,3 个数字,4 个短 varchars)。
我检查了执行时间作为给定代码执行前后System.nanoTime() 之间的差异。大约 550 毫秒,无论是第 1 次还是连续第 10 次执行。我的假设是它应该更快。
我的第一个猜测是查询有问题,所以我检查了 Eclipselink 日志。执行的查询是:
SELECT *all_columns*
FROM *table_name*
WHERE (APPLICATION_ROLE IN (?,?,?)) ORDER BY ID
bind => [3_application_roles]
对我来说看起来不错。我尝试将其作为本机查询执行,但结果是相同的。我还尝试了其他查询,例如SELECT * FROM table_name,但时间仍然约为 500-600 毫秒。
这次我想进行一些比较,所以我手动创建了数据库连接并执行如下查询:
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(database_args);
Statement statement = connection.createStatement();
statement.executeQuery(query);
我执行了几次,第一次(建立连接时)花了相当长的时间,但接下来花了大约 50-60 毫秒。
我的第二个猜测是连接池的问题。我试图在 Eclipselink 文档中找到一些东西,但我只注意到参数:
<property name="eclipselink.connection-pool.default.initial" value="1"/>
<property name="eclipselink.connection-pool.default.min" value="16"/>
<property name="eclipselink.connection-pool.default.max" value="16"/>
应该设置。他们是,但问题仍然存在。
我的 persistence.xml 的内容:
<persistence>
<persistence-unit name=unit transaction-type="JTA">
<jta-data-source>datasource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<!-- cache needs to be deactivated for multiple pods -->
<!-- https://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching -->
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<!--<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>-->
<property name="eclipselink.weaving" value="false"/>
<property name="eclipselink.target-database"
value="org.eclipse.persistence.platform.database.oracle.Oracle12Platform"/>
<property name="eclipselink.connection-pool.default.initial" value="1"/>
<property name="eclipselink.connection-pool.default.min" value="16"/>
<property name="eclipselink.connection-pool.default.max" value="16"/>
</properties>
</persistence-unit>
</persistence>
我能做些什么来解决这个问题?
【问题讨论】:
-
您与 JDBC 的比较:您在重用什么以及每次重新创建什么?您是否查看过 EclipseLink 的语句缓存 (eclipse.org/eclipselink/documentation/2.4/jpa/extensions/…)?这是每个连接,因此在 EclipseLink 连接池中使用 16 个不同的连接时比较不公平。
标签: java oracle eclipselink