您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 >
插入式持久性提供者的承諾:Kodo、OpenJPA和Hibernate
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/2/27 14:47:53 ] 推薦標(biāo)簽:

      <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WeblogicTransactionManagerLookup"/>
        <property name="hibernate.transaction.flush_before_completion" value="true"/>
        <property name="hibernate.transaction.auto_close_session" value="true"/>

后,使用Hibernate進(jìn)行端到端的測(cè)試

現(xiàn)在,我們專為Weblogic Server的JTA事務(wù)配置了Hibernate。這時(shí)運(yùn)行測(cè)試沒有出現(xiàn)錯(cuò)誤,表明使用了正確的提供者,并為消息給定了一個(gè)正確的標(biāo)識(shí)符。

$ ant -q -Dprovider=hibernate
     [echo] =====================================================
     [echo]     Build Configuration for hibernate
     [echo] =====================================================
     [echo] Base directory  : D:projectswitch
     [echo] Deployed Target : D:projectswitch/JPAService.ear
     [echo] EJB Module      : D:projectswitch/tmp/hibernate-ejb.jar
     [echo] Configuration   : D:projectswitch/META-INF/hibernate/persistence.xml
     [echo] Packaging EJB Module for hibernate at D:projectswitch/tmp/hibernate-ejb.jar
     [echo] Packaging EAR Module for hibernate at D:projectswitch/JPAService.ear
     [echo] Packaging D:projectswitch/tmp/test-JPAService.jar for running the tests
     [echo] Undeploying JPAService from t3://localhost:7001 ...
     [echo] Deploying JPAService to t3://localhost:7001 ...
     [echo] Running JUnit Test: junit.TestJPAService ...
     [junit] Logical Persistence Provider is [hibernate]
     [junit] Actual  Persistence Provider is [org.hibernate.impl.SessionImpl]
     [junit] Persisted Message [id:1 timestamp:1182755464176 body:A message sent for logging on 1182755464166]
     [junit] Time elapsed between the message to send and persisted is 10ms

重復(fù)測(cè)試表明已經(jīng)用ID:2創(chuàng)建了新的消息。

我檢查了MySQL數(shù)據(jù)庫,發(fā)現(xiàn)當(dāng)它的值配置為自動(dòng)生成的時(shí)候,主鍵列標(biāo)記為自動(dòng)增加。

mysql> show create table message;
mysql>| message | CREATE TABLE `message` (
  `id` bigint(20) NOT NULL auto_increment,
  `body` varchar(255) default NULL,
  `createdOn` datetime default NULL,
  PRIMARY KEY  (`id`)
The configuration that create the table definition for us is <property name="hibernate.hbm2ddl.auto" value="create"/>

關(guān)于Hibernate部署的摘要

總結(jié)起來,在Weblogic Server 10.0環(huán)境中安裝和運(yùn)行使用Hibernate的JPA應(yīng)用程序步驟如下。

1. 在Weblogic Server 域中的共享庫里添加Hibernate庫

2. 配置JTA事務(wù)和自動(dòng)的表定義屬性

3. 封裝、部署和運(yùn)行測(cè)試進(jìn)行驗(yàn)證

現(xiàn)在來看一下要采用Kodo運(yùn)行完全相同的應(yīng)用程序,我們需要做些什么。
為何不需要在weblogic Server 10.0中安裝Kodo

Kodo是Weblogic Server 10.0整體的一部分。核心Kodo庫隨Weblogic Server安裝一起提供,可以在${bea.home}/modules/com.bea.core.kodo_4.1.3.jar中獲得。Kodo 4.1.3構(gòu)建于OpenJPA之上, Weblogic Server安裝后還在${bea.home}/modules/org.apache.openjpa_0.9.7.jar中提供OpenJPA庫。Kodo和OpenJPA依賴于其他幾個(gè)開源jar(其中的是用于字節(jié)碼增強(qiáng)的serp)和規(guī)范jar,如jpa、jdo、jca或jta。所有這些必要的jar也可以從${bea.home}/modules/目錄中獲得。

為了使用Kodo運(yùn)行完全相同的應(yīng)用程序,只需一個(gè)不同的persistence.xml。

persistence.xml

01 <?xml version="1.0"?>
02
03 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
04   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
06     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
07   version="1.0">
08   <persistence-unit name="test" transaction-type="JTA">
09     <provider>kodo.persistence.PersistenceProviderImpl</provider>
10     <properties>
11       <property name="kodo.ConnectionURL"           value="jdbc:mysql://localhost/kodoDB"/>
12       <property name="kodo.ConnectionDriverName"     value="com.mysql.jdbc.Driver"/>
13       <property name="kodo.jdbc.SynchronizeMappings" value="buildSchema"/>
14     </properties>
15   </persistence-unit>
16 </persistence>

與使用Hibernate時(shí)的配置比較,惟一顯著的變化是將提供者類名改為kodo.persistence.PersistenceProviderImpl。

現(xiàn)在屬性名不一樣了。例如,在Kodo中可以通過設(shè)置kodo.jdbc.SynchronizeMappings為buildSchema來配置自動(dòng)模式創(chuàng)建。

經(jīng)過小限度的改變,我再使用Kodo作為提供者運(yùn)行測(cè)試。

$ ant -Dprovider=kodo

$ ant -q -Dprovider=kodo
     [echo] =====================================================
     [echo]     Build Configuration for kodo
     [echo] =====================================================
     [echo] Base directory  : D:projectswitch
     [echo] Deployed Target : D:projectswitch/JPAService.ear
     [echo] EJB Module      : D:projectswitch/tmp/kodo-ejb.jar
     [echo] Configuration   : D:projectswitch/META-INF/kodo/persistence.xml
     [echo] Enhancing persistent classes
     [echo] Packaging EJB Module for kodo at D:projectswitch/tmp/kodo-ejb.jar
     [echo] Packaging EAR Module for kodo at D:projectswitch/JPAService.ear
     [echo] Packaging D:projectswitch/tmp/test-JPAService.jar for running the tests
     [echo] Undeploying JPAService from t3://localhost:7001 ...
     [echo] Deploying JPAService to t3://localhost:7001 ...
     [echo] Running JUnit Test: junit.TestJPAService ...
    [junit] Logical Persistence Provider is [kodo]
    [junit] Actual  Persistence Provider is [kodo.persistence.KodoEntityManagerImpl]
    [junit] Persisted Message [id:251 timestamp:1182762774929 body:A message sent for logging on 1182762774918]
    [junit] Time elapsed between the message to send and persisted is 11ms

一切順利。先前部署的Hibernate單元撤除了。新的部署使用Kodo作為提供者,并返回了正確的提供者。

Kodo定義了什么模式呢?

mysql> use kododb;
mysql> show create table message;
 | message | CREATE TABLE `message` (
  `id` bigint(20) NOT NULL,
  `body` varchar(255) default NULL,
  `createdOn` datetime default NULL,
  PRIMARY KEY  (`id`)
)

注意,Kodo沒有像 Hibernate那樣標(biāo)記auto-increment的id 。Kodo由自己指派自動(dòng)生成的標(biāo)識(shí)符。
這個(gè)bug是怎么回事?

到目前為止,一切似乎都運(yùn)行良好。我們安裝了Hibernate,然后使用Hibernate運(yùn)行了一個(gè)應(yīng)用程序。然來又將提供者切換為Kodo。您可以用OpenJPA進(jìn)行類似的測(cè)試(它的提供者是org.apache.openjpa.persistence.PersistenceProviderImpl。但是,由于在Weblogic Server中這個(gè)提供者是默認(rèn)的,所以您甚至可以省略它)。那么切換提供者產(chǎn)生的bug在哪兒呢?

如果您決定以不同的方式安裝Hibernate,那么會(huì)出現(xiàn)bug。如果不把Hibernate庫放在域的共享庫中,Hibernate庫還可以放在EAR里。如果這樣進(jìn)行部署封裝,則應(yīng)用程序不能再次進(jìn)行部署和撤除(即便將同樣的Hibernate作為提供者)。這是為什么呢?

答案在于JPA自身提供的一個(gè)javax.persistence.Persistence類。這個(gè)類用于引導(dǎo)。它搜索可用的提供者并要求每個(gè)提供者創(chuàng)建一個(gè)持久性單元,即EntityManagerFactory。然而,這個(gè)引導(dǎo)類javax.persistence.Persistence會(huì)將PersistenceProvider類緩存在一個(gè)內(nèi)部靜態(tài)Set中,并且不再對(duì)Set成員進(jìn)行更新。

因此,如果用Web應(yīng)用程序W封裝了Hibernate,一旦用戶應(yīng)用程序直接調(diào)用或者由注入過程調(diào)用Persistence.createEntityManagerFactory(),則Hibernate持久性提供者X將被Web應(yīng)用程序W的類裝載器 L1加載,并且緩存在javax.persistence.Persistence的靜態(tài)Set中。隨后,Web應(yīng)用程序W被撤除。L1便會(huì)離開作用域。Web應(yīng)用程序W將被重新部署。這時(shí)的類裝載器是L2。如果程序再次調(diào)用Persistence.createEntityManagerFactory(),則javax.persistence.Persistence中的代碼將試圖調(diào)用X的方法(由L1加載的,而L1已經(jīng)不在了),而且代碼將開始與ClassCastException和長(zhǎng)的堆棧跟蹤斷開。

如何解決這一bug呢?

一個(gè)簡(jiǎn)單的解決方案是遵守這里所描述的封裝模式。還有一個(gè)有難度的方案是修改javax.persistence.Persistence本身。這個(gè)方案有難度是因?yàn)檫@一過程中要升級(jí)/修補(bǔ)/重新分配一個(gè)按照規(guī)范定義的(提供的)實(shí)現(xiàn)類。

在另一篇文章中,我將討論在如何對(duì)javax.persistence.Persistence類進(jìn)行修改,從而允許在涉及多個(gè)(可能是不相干的)類裝載器的環(huán)境中更加良好地運(yùn)行。

上一頁123456下一頁
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd