在Karaf上实现数据持久化(OpenJPA)

来源:互联网 发布:零基础 学python 书 编辑:程序博客网 时间:2024/05/22 12:00

花了些时间,了解了网上相关的示例;比较少,大部分比较旧;
借助 Christian Schneider的例子,对支持库进行了一些调整,提供一个可用版本的实例;
自动化测试使用嵌入式数据库derby,运行使用mysql;

目前OpenJPA2.3.0使用时有些问题。Achim Nierbeck的配置进行修改。
Karaf3.0.0相对于Karaf2.*.*有了很大的改动,包括新增了一下常用特性,例如直接开箱即用的jdbc feature;包括重新定义了命令行使用规则;
当前版本还没有达到商用化的程度,建议直接使用bin/karaf 启动,暂时少用其他模式启动。

主要步骤:
  • 下载最新版本的karaf(3.0.0),并使用bin/karaf启动。
  • 复制derby和mysql的两个数据源配置文件,放置于karaf的deploy目录下。系统可以自动发现两个数据源文件。
  • 需要安装derby和mysql的驱动才能使数据库连接处于可用状态。
  • 便于查看数据库状态建议安装jdbc插件:feature:install jdbc。相关命令可以参考karaf的问官方文档。
  • 构建person例子对应的features.xml相关文件存放在features/features.xml下
  • 目前karaf的mvn下载feature.xml的功能不可用。建议使用: feature:repo-add file:/base/features.xml的方式加载。
  • 加载好后需要install一下:feature:install "此处为你的features的名字"
  • 加载完成后list一下查看其状态是否为active。如果正常,直接就能运行命令:person:add person:list等命令进行操作了。
  • 检查数据库发现OpenJPA建了两张表一张为person一张openjpa_sequence_table;并且在person表中多了一个主键字段ID。
  • karaf结合maven开发起来还是很方便的,特别是提供了features.xml方式来粘合资源,使得开发,发布,运行,维护都变的越来越清爽了。


<feature name="Karaf-JPA-OpenJPA" version="1.0.0-SNAPSHOT">
<!-- feature dependencies -->
<feature>jpa</feature>
<feature>http</feature>
<feature>jndi</feature>
<feature>transaction</feature>

<!-- openjpa library dependencies -->
<bundle>mvn:org.apache.geronimo.specs/geronimo-annotation_1.0_spec/1.1.1</bundle>
<bundle>mvn:org.apache.geronimo.specs/geronimo-jpa_2.0_spec/1.1</bundle>
<bundle>mvn:org.apache.geronimo.specs/geronimo-servlet_2.5_spec/1.2</bundle>
<bundle>mvn:org.apache.geronimo.specs/geronimo-el_1.0_spec/1.0.1</bundle>
<bundle>mvn:org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1</bundle>
<bundle>mvn:commons-lang/commons-lang/2.6</bundle>
<bundle>mvn:commons-collections/commons-collections/3.2.1</bundle>
<bundle>mvn:commons-pool/commons-pool/1.6</bundle>
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-dbcp/1.4_3</bundle>
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.ant/1.7.0_6</bundle>
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.serp/1.14.1_1</bundle>
<bundle>mvn:org.apache.geronimo.specs/geronimo-jms_1.1_spec/1.1.1</bundle>
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.asm/3.3_2</bundle>
<bundle>mvn:org.apache.openjpa/openjpa/2.2.2</bundle>
<!-- mysql -->
<bundle>mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.stax-api-1.0/1.9.0</bundle>
<bundle>mvn:mysql/mysql-connector-java/5.1.18</bundle>
<!-- derby 4 test -->
<bundle>mvn:org.apache.derby/derby/10.8.2.2</bundle>

<bundle>mvn:net.lr.tutorial.karaf.db/db-examplejpa/1.0-SNAPSHOT</bundle>
</feature>

0 0