文章
网格计算
作者:金辉 (Oracle)
2016 年 7 月 12 日发布
第 1 章 — 漫说内存网格
第 2&3 章 — 安装和配置开发环境
第 4 章 — 开始第一个 Coherence 程序
第 5 章 — 在 Coherence 中使用 POF 对象
第 6 章 — 分布式内存集群的数据灌注、查询和统计
第 7 章 — 监听器处理内存集群中变化的对象
第 8 章 — 使用 ZFS 加密
第 9 章 — 内存集群与数据库的互操作
第 10 章 — Coherence 内存集群的安全访问
第 11 章 — 基于内存集群的事件处理
本章内容,介绍如何使用 Java Persistence API (JPA) 处理 O-R mapping。 并介绍基于 Coherence 和 Oracle TopLink Grid 的集成,演示基于内存的对象映射。
本章内容必须安装有一个Oracle Database Express 版。该软件介质可从如下网址下载:
http://www.oracle.com/technetwork/database/database-technologies/express-edition/downloads/index.html
JPA 定义了关系型数据如何映射到 Java 对象(持久化的条目),这些对象存储在关系型数据库中,以便在之后使用。
为了将数据存在内存集群中,使用 backing map。默认的 Coherence 使用基于内存的 backing map。为持久化数据,有几种 backing map 的实现。
本章 JPA 实现了 Java 到 database 的映射,也允许使用标准的 Coherence get 和 put 方法,并通过 JPA 和 Oracle TopLink 将 Coherence 调用转化成数据库 database 调用。
什么是 Oracle TopLink?
Oracle TopLink 是个持久化框架,包含了在 Java SE 和 EE 环境中开发工具和运行时的转化、映射和持久化操作。TopLink 的技术核心是 EclipseLink,源自 Eclipse 基金会的开源持久化框架。
TopLink 包含了超出 EclipseLink 的附加功能。其中一个是由 TopLink is TopLink Grid 提供的。
What is Oracle TopLink Grid?
Oracle TopLink Grid 是通过 EclipseLink 与 Coherence 的集成,实现了 TopLink JPA 的实现。 你可以通过 JPA 实现的缓存 loader 和 store,使用 Coherence API 缓存访问的关系型数据。在传统的 Coherence 方法中,TopLink Grid 提供了 CacheLoader 和 CacheStore 的实现在 oracle.eclipselink.coherence.standalone 包里 — 这是为 EclipseLink JPA 所优化。
你可以使用 JPA 构建应用,并透明度使用数据网格来提高伸缩性和性能。应用可以被伸缩一是将 Coherence 作为分布式缓存 (L2) 直接使用 JP QL 查询 Coherence 在跨节点中并行查询,节省数据库负载。当 JPA 在多节点环境中运行时, TopLink Grid 提供了一组缓存和查询的配置选项,运行使用 Coherence 控制 EclipseLink JPA。这都在 oracle.eclipselink.coherence.integrated 包中。
TopLink Grid 缓存的 store 和 loader 实现在 toplink-grid.jar 文件中。TopLink Grid 使用标准 JPA 运行时配置文件 persistence.xml 和 JPA 映射文件 orm.xml。Coherence 缓存配置文件 coherence-cache-config.xml 必须重新默认的Coherence 配置并制定 cache store 的 schema。
本例使用 TopLink Grid 缓存 store 和 loader 来优化 EclipseLink JPA 。
JPA 在 Grid 中的方式,不在本例中演示,相关内容见“Integrating TopLink Grid with Oracle Coherence”Oracle 中间件集成 Coherence。
本例处理下面内容:
1. Unlock the OracleXE Database
2. Configure the Project for JPA
3. Create the JPA Persistence Unit and Entities
4. Edit the persistance.xml File
5. Create the Cache Configuration File for JPA
6. Create a Cache Server Start-Up Configuration
7. Create a Class to Interact with the Data Object
8. Create a Run Configuration for RunEmployeeExample
9. Run the JPA Example
安装 OracleXE 数据库不赘述,解锁 HR 用户。
解锁用户需要有数据库的 DBA 权限,并遵从以下步骤:
1. 在Windows平台,Start -> All Programs -> Oracle Database 11g Express Edition->Run SQL Command Line。
2. 作为 sysdba 登录
3. 解锁用户
创建一个新工程项目 chapter08_JPAProject。具体内容见“3.3 在 Eclipse IDE 中创建工程”。
1. 在 Eclipse IDE 环境里,点击 Window ->Open Perspective->Other,打开 Open Perspective 对话框,选择 JPA。
或者
2. 选择 File->New->JPA Project。输入 Enter chapter08_JPAProject 作为项目名称。注意确保项目路径。
3. 点击 Configuration 配置旁边的 Modifyto 按键,打开 Project Facets 对话框。
4. 选择 Oracle Coherence。注意 JPA 和 Java 应已经被选中。
5. 点 Configuration 旁边的 Save As 按键。输入 chapter08_JPAConfiguration。
点 OK
点下一步
6. Java 界面,按默认配置,点下一步
7. 在 JPA Facet 配置里,选择 EclipseLink 2.4.x/2.5.x。点 Manage Libraries,增加 TopLink Grid,EclipseLink 和 Java Persistence 文件。
8. 点 New,在新建 User Library 对话框里,输入 chapter08_TopLink,并选中 System library 复选框:
9. 点 OK 关闭 New User Library 对话框;
10. 在 Preference 对话框里,选中刚才创建的 chapter08_TopLink[system library] 然后点击 Add External JARS。
11. 导航到 Oracle_Home\oracle_common\modules\oracle.toplink_12.1.2 目录,选择 toplink-grid.jar和eclipselink.jar文件。
点击 Open,之后界面如下:
继续点击 Add External Jars,导航到 Oracle_Home\oracle_common\modules 目录,选择 javax.persistence_2.1.0.0_1-0-2.jar 文件,点 Open。操作完成是,界面如下:
12. 点 OK,结束 Preferences 对话框。选择 chapter08_TopLink 和 Coherence 12.1.2
13. 创建一个能访问到 Oracle XE 数据库的连接,点 Add connection。
a) 在 Connection Profile 对话框里,选择 Oracle Database Connection。 输入 XE_HR
b) 数据库驱动的下拉列表,选择 Oracle Database 11g Driver Default。在 General 选卡:
i. 用户名:hr
ii. 密码:hr
iii. 选中 Save Password 复选框
iv. 数据地址:localhost
v. 选中 Connect when the wizard completes 复选框。
vi. 点 Test Connection 按键。确定测试返回成功信息后点 OK,点 Finish。
14. 在 New JPA 的对话框里,选中增加 Add driver library to build path 后点击下一步 Next。
15. 在 Coherence 配置里,确保 Coherence12.1.2 和 TopLink 库是被选中的,点结束。
JPA 通过标识持久化单元 (persistence unit),来管理定义的元数据、类和 JAR。一个持久化单元 (persistence unit) 逻辑上包括以下内容:1) 一个实体管理器工厂 (EntityManagerFactory)、由实体管理器工厂创建的多个实体管理器(EntityManager)、持久化单元的配置信息;2) 持久化单元中被管理实体类的集合;3) 用来将实体类映射到关系表的元数据。实体 (Entities) 包含了持久化数据和被存入的数据库。JPA 的实体从属于一个持久化单元。在 JPA 规范中,持久化单元在 persistence.xml 文件里描述。
创建 JPA 持久化单元和 JPA 实体,按如下操作:
1. 右键点击 chapter08_JPAProject,New->JPA Entities from Tables。会弹出选择数据库表的对话框。
2. 选择表 EMPLOYEES,下一步
3. 在 Table Associations 配置里,见下图,走下一步
4. 在 Customize Defaults 配置里,“Collection properties type”选择 java.util.List,Package 字段里输入 com.oracle.handson.chapter08,并在 Associationsfetch 里选择 Lazy。完成配置时,如下图:
点下一步。
5. 在 Customize Individual Entities 配置里,java.lang.Object 作为父类, 并将默认的类名 Employee 变更为 Employees,如下图:
6. 点结束,OEPE 产生相应的源代码。
persistence.xml 文件里定义了 1 到多个持久化单元 (persistence units)。在 Eclipse IDE 里打开 persistence.xml 文件来编辑。
1. 打开 persistence.xml 文件
2. 在 General 选卡里, 如果 persistence provider 为空,则自己输入 org.eclipse.persistence.jpa.PersistenceProvider。
3. 在 Connection 选卡里,Transaction type 选中 Resource Local。点击连接“Populate from connection”,选中 XE_HR,然后点 OK
数据库连接信息从配置的 HR 数据库连接配置里复制出来,界面如下:
4. 如果 Logging 选卡,Logging level 选择 Fine。选中 Exceptions 复选框,不选 Timestamp、Thread、Session 和 Connection 复选框
5. 在 Source 选卡中,persistence.xm 类似如下
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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_2_0.xsd">
<persistence-unit name="chapter08_JPAProject"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.oracle.handson.chapter08.Employees</class>
<properties>
<property name="eclipselink.target-server" value="None" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="javax.persistence.jdbc.user" value="hr" />
<property name="javax.persistence.jdbc.password" value="hr" />
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.timestamp" value="false" />
<property name="eclipselink.logging.thread" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.connection" value="false" />
<property name="eclipselink.logging.exceptions" value="true" />
</properties>
</persistence-unit>
</persistence>
1. 右键点击 coherence-cache-config.xml,Rename 为 jpa-cache-config.xml
2. 打开这个文件,注意下面示例的黑色字体内容
<?xml version="1.0"?>
<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config http://xmlns.oracle.com/coherence/coherence-cache-config/1.2/coherence-cache-config.xsd">
<defaults>
<serializer system-property="tangosol.coherence.serializer" />
<socket-provider system-property="tangosol.coherence.socketprovider" />
</defaults>
<caching-scheme-mapping>
<cache-mapping>
<!-- Set the name of the cache to be the entity name -->
<cache-name>Employees</cache-name>
<!-- Configure this cache to use the scheme defined below -->
<scheme-name>distributed-eclipselink</scheme-name>
</cache-mapping>
</caching-scheme-mapping>
<caching-schemes>
<distributed-scheme>
<scheme-name>distributed-eclipselink</scheme-name>
<service-name>EclipseLinkJPA</service-name>
<backing-map-scheme>
<read-write-backing-map-scheme>
<!-- Define the cache scheme -->
<internal-cache-scheme>
<local-scheme />
</internal-cache-scheme>
<cachestore-scheme>
<class-scheme>
<class-name>oracle.eclipselink.coherence.standalone.EclipseLinkJPACacheStore</class-name>
<init-params>
<!-- This param is the entity name This param should match the value
of the persistence unit name in persistence.xml -->
<init-param>
<param-type>java.lang.String</param-type>
<param-value>{cache-name}</param-value>
</init-param>
<init-param>
<param-type>java.lang.String</param-type>
<param-value> chapter08_JPAProject </param-value>
</init-param>
</init-params>
</class-scheme>
</cachestore-scheme>
</read-write-backing-map-scheme>
</backing-map-scheme>
<autostart>true</autostart>
</distributed-scheme>
</caching-schemes>
</cache-config>
1. 右键点击项目,Run As->Run Configurations。新建一个 Cache Server 的运行配置
2. 简便的话,复制 chapter06CacheServer,更名为 chapter08CacheServer。注意运行工程为 chapter08_JPAProject,Main class 是 com.tangosol.net.DefaultCacheServer
3. Coherence 选卡的 General 选卡,指定 cache 的配置文件为 jpa-cache-config.xml。选择 Enabled (cache server)。集群端口 3155。
4. Other 选卡,确保 tangosol.pof.config 设为默认的 pof-config.xml。
5. Common 选卡,选择 Shared file,选择 \chapter08_JPAproject。
6. Classpath 选卡,如果类似下图,下图中 User 实体部分没有 Oracle 数据库驱动,则需要再添加驱动
点 User Entries,再点击 Advanced。
点结束,结果如下
然后可以点运行
在工程中创建一个 Java 类,来修改员工的薪水。创建过程依据前述步骤,类名称:RunEmployeeExample
1. 通过 EMPLOYEE_ID 属性,获得员工信息;
2. 显示当前薪资;
3. 增加 10%,并保存;
4. 从新获得该员工信息
package com.oracle.handson.chapter08; import com.tangosol.net.CacheFactory; import com.tangosol.net.NamedCache; import java.math.BigDecimal; public class RunEmployeeExample { public static void main(String[] args) { // TODO Auto-generated method stub long empId = 190L; // emp 190 - Timothy Gates NamedCache employees = CacheFactory.getCache("Employees"); Employees emp = (Employees) employees.get(empId); System.out.println("原先员工信息 " + emp.getFirstName() + " " + emp.getLastName() + ", 薪资 = $" + emp.getSalary()); // give them a 10% pay rise emp.setSalary(emp.getSalary().multiply(BigDecimal.valueOf(1.1))); employees.put(empId, emp); Employees emp2 = (Employees) employees.get(empId); System.out.println("新的员工信息是: " + emp2.getFirstName() + " " + emp2.getLastName() + ", 薪资 = $" + emp2.getSalary()); } }
1. 右键工程,Run As->Run Configurations。复制 chapter08CacheServer,变更为 chapter08CacheClientEmployeeExample。
2. 在 Main 类里,选择 com.oracle.handson.chapter08.RunEmployeeExample
3. 在 General 选卡的 Coherence 选卡里,确保 src\jpa-cache-config.xml,并选择 Disabled (cache client),集群端口 3155。
4. Other 选卡,确保 tangosol.pof.config 是默认的 pof-config.xml。
5. 在 Common 选卡,继承原有配置
6. Classpath 类似如下图
RunEmployeeExample
![]() | 金辉,在 Oracle 负责云和中间件产品的资深售前顾问和团队经理,有十多年的中间件和项目管理经验,专注在企业云构建,企业集成解决方案领域,熟悉业内主要的 SOA 集成产品。参加过北京马拉松和 TNF50 越野比赛。你可以通过邮件 arthur.jin@oracle.com 与他联系。 |