天天看點

在OFBIZ中使用多個資料庫的方法

要在OFBIZ中使用多個資料庫,我們就需要知道OFBIZ實體與資料庫的關聯關系。然後要知道實體分組。 我們每定義一個實體就應該明确的将該實體分到對應的實體分組中,一個實體分組可以包含多個實體。每個分組又可以單獨的定義它所使用的資料庫。是以,我們就要可以通過将實作進行分組,然後使用不同的資料庫。實體分組的定義在%OFBIZ_HOME%/framework/entity/conf/entityengine.xml檔案中定義。預設情況下,實體被分組到了 org.ofbiz分組中。

    <entity-group group="org.ofbiz.olap" entity="SalesInvoiceItemFact"/>

<entity-group group="org.ofbiz.olap" entity="SalesInvoiceItemStarSchema"/>

Entity-group(一般定義在各個子產品的\entitydef\entitygroupXXX.xml中) 對實體進行分組,使不同的實體分屬不同的entity-group。

也許你會發現并不是每個entity都進行了entity-group 分組。事實上如果你沒有對實體進行分組歸類的話,系統啟動的時候他會将實體預設歸類到"org.ofbiz"中。

檢視資料庫定義檔案%ofbiz_home%/framework/entity/config/entityengine.xml

可以發現:

<delegator name="default" entity-model-reader="main" entity-group-reader="main" entity-eca-reader="main" distributed-cache-clear-enabled="false">

        <group-map group-name="org.ofbiz" datasource-name="localderby"/>

        <group-map group-name="org.ofbiz.olap" datasource-name="localderbyolap"/>

        <group-map group-name="org.ofbiz.tenant" datasource-name="localderbytenant"/>

</delegator>

可以發現delegator 将多個group-name組織到一起并将group-name與datasource-name對應起來,datasource-name又是什麼?通過檢視 entityengine.xml 我們可以發現:

<datasource name="localderby"

            helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"

            schema-name="OFBIZ"

            field-type-name="derby"

            check-on-start="true"

            add-missing-on-start="true"

            use-pk-constraint-names="false"

            use-indices-unique="false"

            alias-view-columns="false"

            use-order-by-nulls="true">

        <read-data reader-name="seed"/>

        <read-data reader-name="seed-initial"/>

        <read-data reader-name="demo"/>

        <read-data reader-name="ext"/>

        <inline-jdbc

                jdbc-driver="org.apache.derby.jdbc.EmbeddedDriver"

                jdbc-uri="jdbc:derby:ofbiz;create=true"

                jdbc-username="ofbiz"

                jdbc-password="ofbiz"

                isolation-level="ReadCommitted"

                pool-minsize="2"

                pool-maxsize="250"

                time-between-eviction-runs-millis="600000"/>

</datasource>

Datasource定義了資料庫驅動,資料庫使用者名、密碼等,是以datasource就是我們說的資料庫。 

總結一下:我們通過entity-group将各個實體和資料庫之間關聯起來,然後再将一個或多個資料庫歸屬到一個delegator 中,這樣我們就可以通過相同的delegator從不同的資料庫中取值了。

那我們又是怎麼使用資料庫進行資料庫操作的呢??檢視每個子產品應用底下的web.xml 我們可以發現:

<context-param>

        <param-name>entityDelegatorName</param-name>

        <param-value>default</param-value>

        <description>The Name of the Entity Delegator to use, defined in entityengine.xml</description>

</context-param>

針對不同的應用,我們可以使用不同的delegator .如果不定義則使用default.

在啟動各個應用子產品的時候,系統會根據web.xml 中的 entityDelegatorName 

生成delegator 對象,然後将delegator 對象存放到servletContext 中備用。

我們就是使用這個delegator對象執行資料庫操作,以後會介紹如何使用。

delegator = DelegatorFactory.getDelegator(delegatorName);

    servletContext.setAttribute("delegator", delegator);

繼續閱讀