14.在数据库中存储知识库

概述

在介绍安装与配置一节时,很重要一点那就是需要设置好规则相关文件库的存储方式。URule Pro规则引擎中规则相关资源文件存储机制采用的是jackrabbit实现,默认情况下,我们只需要通过urule.repository.dir属性来指定资源库目录,这样就会在这个目录下以文件形式存储规则相关文件。

如果我想将让资源库存储放到当前项目所在目录,比如WEB-INF下的repo目录中,那么我们可以在当前项目的WEB-INF目录中创建一个名为repo的目录,然后设置urule.repository.dir属性值为“/WEB-INF/repo”,这样urule在启动的时候会自动将这个目录作为repository的工作目录。

将知识文件存储到本地文件系统中,对于多人开发的项目来说,就有些不方便,对于这种情况,我们可以将知识库存储于数据库中,这样就可以解决上述问题。

在数据库中存储知识库有两种方式,一种是采用Spring中配置数据源连接数据库;另一种就是用户自定义jackrabbit的存储xml配置文件,通过这个文件来自定义数据库连接信息。当然这里肯定推荐第一种采用Spring中配置数据源连接数据库方式。

这里需要强调的是,当采用xml配置方式来连接数据库,那么就需要手工配置集群信息,如果不配置使用过程中就可能会出现文件丢失的情况,就是说在未配置集群的情况下,一个应用在保存规则时会覆盖整个知识库,从而产生另一个应用配置的规则丢失的情况,这点一定要注意;如果采用spring配置方式的话,那么引擎会自动配置好集群信息。

采用Spring中配置数据源连接数据库

这种配置数据库存储的方式是我们强烈推荐的方法。具体做法是删除属性文件中对urule.repository.dir属性的配置信息(当然注释也可以),然后添加在properties文件中添加一面两个属性即可:

属性名 含义
urule.repository.datasourcename 当前在标准spring context中已配置的数据源的bean id
urule.repository.databasetype 当前采用数据源对应的数据库类型,目前支持的类型有6种,分别是:mysql、mssql、oracle、db2、derby以及postgresql

下面这段是配置在Spring中的一个连接到Mysql数据库的数据源连接配置,它的Bean的Id为"mysqlDatasource",如下所示:

<bean id="mysqlDatasource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="url"
        value="jdbc:mysql://localhost:3306/urule_test?useUnicode=true&amp;characterEncoding=UTF-8" />
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="username" value="root" />
    <property name="password" value="root" />
    <property name="minIdle" value="5" />
    <property name="maxActive" value="10" />
    <property name="maxWait" value="1000" />
    <property name="removeAbandonedTimeout" value="60" />
    <property name="removeAbandoned" value="true" />
    <property name="logAbandoned" value="true" />
</bean>

要让我们的知识库使用这个数据源,我们只需要在properties文件中添加一个名为urule.repository.datasourcename属性,值为mysqlDatasource,同时该连接是一个mysql数据库,所以还需要设置urule.repository.databasetype属性值为mysql,这样就可以使用这个配置在Spring中的数据源来作为URule Pro中知识库相关文件的存储库。

当采用Spring中配置的数据源连接数据库时,引擎会自动添加集群功能,所以这种配置方式是我们推荐的。

如果我们采用后面的配置XML的方式连接数据库则需要手动配置集群,如果不添加集群配置的话,那么在使用中如有一个以上应用连接同一个存储知识库的数据库,在规则编写定义时则可能会出现文件丢失情况。

用户自定义jackrabbit的存储xml配置文件

对于有过jackrabbit使用经验的用户来说,可能需要进一步控制资源文件的存储方式,对于这种情况,URule Pro提供了一个名为“urule.repository.xml”的属性,通过设置这个属性的值,指定一个定义好的jackrabbit配置文件的位置,这样URule Pro中的jackrabbit就可以按这个配置文件配置方式运行,采用这种方式,需要我们将属性文件中配置的urule.repository.dir属性以及urule.repository.datasourcename和urule.repository.databasetype属性删除或注释。

如:urule.repository.xml=/WEB-INF/test.xml,那就表示系统将采用当前应用根下的WEB-INF目录下的text.xml文件来运行jackrabbit;urule.repository.xml=classpath:com/bstek/app/test.xml,则表示系统将采用当前应用classpath下com/bstek/app目录下的test.xml文件来运行jackrabbit。

下面是几种常见的用数据库来作为repository的xml配置文件,实际使用时我们只需要修改其中的数据库连接即可。

连接数据库的配置方式有两种,分别是连接池方式以及JNDI方式,我们先来看看连接池方式的XML配置。

连接池方式配置

MySQL连接配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Repository PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 2.6//EN" "view-source:http://jackrabbit.apache.org/dtd/repository-2.6-elements.dtd">
<Repository>
    <DataSources>
        <DataSource name="dataSource">
            <param name="driver" value="com.mysql.jdbc.Driver" />
            <param name="url" value="jdbc:mysql://localhost:3306/urule?useUnicode=true&amp;characterEncoding=UTF-8" />
            <param name="user" value="root" />
            <param name="password" value="qwertyuioplm" />
            <param name="databaseType" value="mysql"/>
            <param name="maxPoolSize" value="10"/>
        </DataSource>
    </DataSources>
  <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_"/>
  </FileSystem>
  <Security appName="Jackrabbit">
    <AccessManager class="org.apache.jackrabbit.core.security.simple.SimpleAccessManager"></AccessManager>
    <LoginModule class="org.apache.jackrabbit.core.security.simple.SimpleLoginModule">
      <param name="anonymousId" value="anonymous" />
      <param name="adminId" value="admin" />
    </LoginModule>
  </Security>
  <DataStore class="org.apache.jackrabbit.core.data.db.DbDataStore">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_ds_"/>
  </DataStore>
  <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" />
  <Workspace name="default">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_${wsp.name}_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MySqlPersistenceManager">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_pm_${wsp.name}_"/>
    </PersistenceManager>
  </Workspace>
  <Versioning rootPath="${rep.home}/version">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_fsver_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MySqlPersistenceManager">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_ver_"/>
    </PersistenceManager>
  </Versioning>
</Repository>

Oracle连接配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Repository PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 2.6//EN" "view-source:http://jackrabbit.apache.org/dtd/repository-2.6-elements.dtd">
<Repository>
    <DataSources>
        <DataSource name="dataSource">
            <param name="driver" value="oracle.jdbc.driver.OracleDriver"/>
            <param name="url" value="jdbc:oracle:thin:@192.168.18.140:1521:ORCL"/>
            <param name="user" value="bstek"/>
            <param name="password" value="bstek"/>
            <param name="databaseType" value="oracle"/>
            <param name="maxPoolSize" value="10"/>
        </DataSource>
    </DataSources>
  <FileSystem class="org.apache.jackrabbit.core.fs.db.OracleFileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_"/>
  </FileSystem>
  <Security appName="Jackrabbit">
    <AccessManager class="org.apache.jackrabbit.core.security.simple.SimpleAccessManager"></AccessManager>
    <LoginModule class="org.apache.jackrabbit.core.security.simple.SimpleLoginModule">
      <param name="anonymousId" value="anonymous" />
      <param name="adminId" value="admin" />
    </LoginModule>
  </Security>
  <DataStore class="org.apache.jackrabbit.core.data.db.DbDataStore">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_ds_"/>
  </DataStore>
  <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" />
  <Workspace name="default">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.OracleFileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_${wsp.name}_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.OraclePersistenceManager">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_pm_${wsp.name}_"/>
    </PersistenceManager>
  </Workspace>
  <Versioning rootPath="${rep.home}/version">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.OracleFileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_fsver_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.OraclePersistenceManager">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_ver_"/>
    </PersistenceManager>
  </Versioning>
</Repository>

如果当前Oracle的版本Oracle9,那么上述配置中的"org.apache.jackrabbit.core.fs.db.OracleFileSystem"类要改成“org.apache.jackrabbit.core.fs.db.Oracle9FileSystem”;“org.apache.jackrabbit.core.persistence.pool.OraclePersistenceManager”类同样也需要改“org.apache.jackrabbit.core.persistence.pool.Oracle9PersistenceManager”。

SQLServer连接配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Repository PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 2.6//EN" "view-source:http://jackrabbit.apache.org/dtd/repository-2.6-elements.dtd">
<Repository>
    <DataSources>
        <DataSource name="dataSource">
            <param name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
            <param name="url" value="jdbc:sqlserver://192.168.18.108:1433;DatabaseName=urule"/>
            <param name="user" value="sa"/>
            <param name="password" value="helpdesk"/>
            <param name="databaseType" value="mssql"/>
            <param name="maxPoolSize" value="10"/>
        </DataSource>
    </DataSources>
  <FileSystem class="org.apache.jackrabbit.core.fs.db.MSSqlFileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_"/>
  </FileSystem>
  <Security appName="Jackrabbit">
    <AccessManager class="org.apache.jackrabbit.core.security.simple.SimpleAccessManager"></AccessManager>
    <LoginModule class="org.apache.jackrabbit.core.security.simple.SimpleLoginModule">
      <param name="anonymousId" value="anonymous" />
      <param name="adminId" value="admin" />
    </LoginModule>
  </Security>
  <DataStore class="org.apache.jackrabbit.core.data.db.DbDataStore">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_ds_"/>
  </DataStore>
  <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" />
  <Workspace name="default">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.MSSqlFileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_${wsp.name}_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MSSqlPersistenceManager">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_pm_${wsp.name}_"/>
    </PersistenceManager>
  </Workspace>
  <Versioning rootPath="${rep.home}/version">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.MSSqlFileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_fsver_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MSSqlPersistenceManager">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_ver_"/>
    </PersistenceManager>
  </Versioning>
</Repository>

DB2连接配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Repository PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 1.5//EN" "http://jackrabbit.apache.org/dtd/repository-1.5.dtd">
<Repository>
    <DataSources>
        <DataSource name="dataSource">
            <param name="driver" value="com.ibm.db2.jcc.DB2Driver"/>
            <param name="url" value="jdbc:db2:rule"/>
            <param name="user" value="bstek"/>
            <param name="password" value="bstek"/>
            <param name="databaseType" value="db2"/>
            <param name="maxPoolSize" value="10"/>
        </DataSource>
    </DataSources>
  <FileSystem class="org.apache.jackrabbit.core.fs.db.DB2FileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_"/>
  </FileSystem>
  <Security appName="Jackrabbit">
    <AccessManager class="org.apache.jackrabbit.core.security.simple.SimpleAccessManager"></AccessManager>
    <LoginModule class="org.apache.jackrabbit.core.security.simple.SimpleLoginModule">
      <param name="anonymousId" value="anonymous" />
      <param name="adminId" value="admin" />
    </LoginModule>
  </Security>
  <DataStore class="org.apache.jackrabbit.core.data.db.DbDataStore">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_ds_"/>
  </DataStore>
  <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" />
  <Workspace name="default">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DB2FileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_${wsp.name}_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.BundleDbPersistenceManager">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_pm_${wsp.name}_"/>
    </PersistenceManager>
  </Workspace>
  <Versioning rootPath="${rep.home}/version">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DB2FileSystem">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_fsver_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.BundleDbPersistenceManager">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="repo_ver_"/>
    </PersistenceManager>
  </Versioning>
</Repository>

值得注意的是,一旦配置好基于数据库的存储方式,那么在第一次启动时jackrabbit会帮助我们自动创建所需要的表信息。

在我们通过urule.repository.xml属性指定一个文件采用数据库方式存储资源文件时,系统启动时还需要一个目录来存储一些临时文件,这时如果我们通过urule.repository.dir属性指定了一个目录,那么这些临时文件将会存储在这个目录中;如果没有通过urule.repository.dir属性指定了一个目录,那么引擎将在JVM临时目录中存储(通过System.getProperty("java.io.tmpdir")获取到的目录)。

在实际使用当中,如果我们采用数据库作为repository,同时有一个以上URule Server应用将此库作为repository,那么对于jackrabbit来说,就是一个典型的cluster配置,在这种情况下,如果直接使用上述数据库配置,那么各个URule Server应用中只会看到应用启动时数据库中存储的repository信息,而对于其它后续其它URule Server对库的修改是看不到的,为解决这一问题,需要在上述xml配置中添加jackrabbit的cluster配置,这段cluster配置放在<Versioning/>节点之后即可,不同数据库的cluster配置如下。

  • SQLServer的Cluster配置

<Cluster syncDelay="5000">
    <Journal class="org.apache.jackrabbit.core.journal.MSSqlDatabaseJournal">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="journal_"/>
    </Journal>
</Cluster>
  • Oracle的Cluster配置

<Cluster syncDelay="5000">
    <Journal class="org.apache.jackrabbit.core.journal.OracleDatabaseJournal">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="journal_"/>
    </Journal>
</Cluster>
  • MySQL的Cluster配置

<Cluster syncDelay="5000">
    <Journal class="org.apache.jackrabbit.core.journal.DatabaseJournal">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="journal_"/>
    </Journal>
</Cluster>
  • DB2的Cluster配置

<Cluster syncDelay="5000">
    <Journal class="org.apache.jackrabbit.core.journal.DatabaseJournal">
      <param name="dataSourceName" value="dataSource"/>
      <param name="schemaObjectPrefix" value="journal_"/>
    </Journal>
</Cluster>

在上述配置当中,Cluster节点都有一个名为syncDelay的属性,该属性用于定义当前节点每隔多久与数据库进行一次同步,单位是毫秒,这里设置的都是5000,就表示5000毫秒,也就是每隔5秒与数据库同步一次。同时,如果运行时出现下面错误,可以在Cluster节点还可以添加一个id属性,用于标识当前节点的ID。

集群下没有为Cluster节点指定id属性时出现的错误:

Failed to access cluster node id: \cluster_node.id

JNDI方式

MySQL连接配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Repository PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 2.6//EN" "view-source:http://jackrabbit.apache.org/dtd/repository-2.6-elements.dtd">
<Repository>
  <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
       <param name="driver" value="javax.naming.InitialContext"/>
       <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_"/>
  </FileSystem>
  <Security appName="Jackrabbit">
    <AccessManager class="org.apache.jackrabbit.core.security.simple.SimpleAccessManager"></AccessManager>
    <LoginModule class="org.apache.jackrabbit.core.security.simple.SimpleLoginModule">
      <param name="anonymousId" value="anonymous" />
      <param name="adminId" value="admin" />
    </LoginModule>
  </Security>
  <DataStore class="org.apache.jackrabbit.core.data.db.DbDataStore">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/DataStore"/>
        <param name="databaseType" value="mysql"/>
        <param name="schemaObjectPrefix" value="repo_ds_"/>
  </DataStore>
  <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" />
  <Workspace name="default">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_${wsp.name}_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MySqlPersistenceManager">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/Workspaces"/>
        <param name="schema" value="mysql"/>
        <param name="schemaObjectPrefix" value="repo_pm_${wsp.name}_"/>
    </PersistenceManager>
  </Workspace>
  <Versioning rootPath="${rep.home}/version">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
       <param name="driver" value="javax.naming.InitialContext"/>
       <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_fsver_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MySqlPersistenceManager">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/Workspaces"/>
        <param name="schema" value="mysql"/>
      <param name="schemaObjectPrefix" value="repo_ver_"/>
    </PersistenceManager>
  </Versioning>
</Repository>

Oracle连接配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Repository PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 2.6//EN" "view-source:http://jackrabbit.apache.org/dtd/repository-2.6-elements.dtd">
<Repository>
  <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
       <param name="driver" value="javax.naming.InitialContext"/>
       <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_"/>
  </FileSystem>
  <Security appName="Jackrabbit">
    <AccessManager class="org.apache.jackrabbit.core.security.simple.SimpleAccessManager"></AccessManager>
    <LoginModule class="org.apache.jackrabbit.core.security.simple.SimpleLoginModule">
      <param name="anonymousId" value="anonymous" />
      <param name="adminId" value="admin" />
    </LoginModule>
  </Security>
  <DataStore class="org.apache.jackrabbit.core.data.db.DbDataStore">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/DataStore"/>
        <param name="databaseType" value="oracle"/>
        <param name="schemaObjectPrefix" value="repo_ds_"/>
  </DataStore>
  <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" />
  <Workspace name="default">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_${wsp.name}_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MySqlPersistenceManager">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/Workspaces"/>
        <param name="schema" value="oracle"/>
        <param name="schemaObjectPrefix" value="repo_pm_${wsp.name}_"/>
    </PersistenceManager>
  </Workspace>
  <Versioning rootPath="${rep.home}/version">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
       <param name="driver" value="javax.naming.InitialContext"/>
       <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_fsver_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MySqlPersistenceManager">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/Workspaces"/>
        <param name="schema" value="oracle"/>
      <param name="schemaObjectPrefix" value="repo_ver_"/>
    </PersistenceManager>
  </Versioning>
</Repository>

如果当前Oracle的版本Oracle9,那么上述配置中的"org.apache.jackrabbit.core.fs.db.OracleFileSystem"类要改成“org.apache.jackrabbit.core.fs.db.Oracle9FileSystem”;“org.apache.jackrabbit.core.persistence.pool.OraclePersistenceManager”类同样也需要改“org.apache.jackrabbit.core.persistence.pool.Oracle9PersistenceManager”。

SQLServer连接配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Repository PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 2.6//EN" "view-source:http://jackrabbit.apache.org/dtd/repository-2.6-elements.dtd">
<Repository>
  <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
       <param name="driver" value="javax.naming.InitialContext"/>
       <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_"/>
  </FileSystem>
  <Security appName="Jackrabbit">
    <AccessManager class="org.apache.jackrabbit.core.security.simple.SimpleAccessManager"></AccessManager>
    <LoginModule class="org.apache.jackrabbit.core.security.simple.SimpleLoginModule">
      <param name="anonymousId" value="anonymous" />
      <param name="adminId" value="admin" />
    </LoginModule>
  </Security>
  <DataStore class="org.apache.jackrabbit.core.data.db.DbDataStore">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/DataStore"/>
        <param name="databaseType" value="mssql"/>
        <param name="schemaObjectPrefix" value="repo_ds_"/>
  </DataStore>
  <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" />
  <Workspace name="default">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_${wsp.name}_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MySqlPersistenceManager">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/Workspaces"/>
        <param name="schema" value="mssql"/>
        <param name="schemaObjectPrefix" value="repo_pm_${wsp.name}_"/>
    </PersistenceManager>
  </Workspace>
  <Versioning rootPath="${rep.home}/version">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
       <param name="driver" value="javax.naming.InitialContext"/>
       <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_fsver_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MySqlPersistenceManager">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/Workspaces"/>
        <param name="schema" value="mssql"/>
      <param name="schemaObjectPrefix" value="repo_ver_"/>
    </PersistenceManager>
  </Versioning>
</Repository>

DB2连接配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Repository PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 2.6//EN" "view-source:http://jackrabbit.apache.org/dtd/repository-2.6-elements.dtd">
<Repository>
  <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
       <param name="driver" value="javax.naming.InitialContext"/>
       <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_"/>
  </FileSystem>
  <Security appName="Jackrabbit">
    <AccessManager class="org.apache.jackrabbit.core.security.simple.SimpleAccessManager"></AccessManager>
    <LoginModule class="org.apache.jackrabbit.core.security.simple.SimpleLoginModule">
      <param name="anonymousId" value="anonymous" />
      <param name="adminId" value="admin" />
    </LoginModule>
  </Security>
  <DataStore class="org.apache.jackrabbit.core.data.db.DbDataStore">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/DataStore"/>
        <param name="databaseType" value="db2"/>
        <param name="schemaObjectPrefix" value="repo_ds_"/>
  </DataStore>
  <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" />
  <Workspace name="default">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_${wsp.name}_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MySqlPersistenceManager">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/Workspaces"/>
        <param name="schema" value="db2"/>
        <param name="schemaObjectPrefix" value="repo_pm_${wsp.name}_"/>
    </PersistenceManager>
  </Workspace>
  <Versioning rootPath="${rep.home}/version">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.DbFileSystem">
       <param name="driver" value="javax.naming.InitialContext"/>
       <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="repo_fsver_"/>
    </FileSystem>
    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.MySqlPersistenceManager">
        <param name="driver" value="javax.naming.InitialContext"/>
        <param name="url" value="java:comp/env/jdbc/Workspaces"/>
        <param name="schema" value="db2"/>
      <param name="schemaObjectPrefix" value="repo_ver_"/>
    </PersistenceManager>
  </Versioning>
</Repository>

一旦配置好基于数据库的存储方式,那么在第一次启动时jackrabbit会帮助我们自动创建所需要的表信息。

在我们通过urule.repository.xml属性指定一个文件采用数据库方式存储资源文件时,系统启动时还需要一个目录来存储一些临时文件,这时如果我们通过urule.repository.dir属性指定了一个目录,那么这些临时文件将会存储在这个目录中;如果没有通过urule.repository.dir属性指定了一个目录,那么引擎将在JVM临时目录中存储(通过System.getProperty("java.io.tmpdir")获取到的目录)。

在实际使用当中,如果我们采用数据库作为repository,同时有一个以上URule Server应用将此库作为repository,那么对于jackrabbit来说,就是一个典型的cluster配置,在这种情况下,如果直接使用上述数据库配置,那么各个URule Server应用中只会看到应用启动时数据库中存储的repository信息,而对于其它后续其它URule Server对库的修改是看不到的,为解决这一问题,需要在上述xml配置中添加jackrabbit的cluster配置,这段cluster配置放在<Versioning/>节点之后即可,不同数据库的cluster配置如下。

  • SQLServer的Cluster配置

<Cluster syncDelay="5000">
    <Journal class="org.apache.jackrabbit.core.journal.MSSqlDatabaseJournal">
      <param name="driver" value="javax.naming.InitialContext"/>
      <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="journal_"/>
    </Journal>
</Cluster>
  • Oracle的Cluster配置

<Cluster syncDelay="5000">
    <Journal class="org.apache.jackrabbit.core.journal.OracleDatabaseJournal">
      <param name="driver" value="javax.naming.InitialContext"/>
      <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="journal_"/>
    </Journal>
</Cluster>
  • MySQL的Cluster配置

<Cluster syncDelay="5000">
    <Journal class="org.apache.jackrabbit.core.journal.DatabaseJournal">
      <param name="driver" value="javax.naming.InitialContext"/>
      <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="journal_"/>
    </Journal>
</Cluster>
  • DB2的Cluster配置

<Cluster syncDelay="5000">
    <Journal class="org.apache.jackrabbit.core.journal.DatabaseJournal">
      <param name="driver" value="javax.naming.InitialContext"/>
      <param name="url" value="java:comp/env/jdbc/DataStore"/>
      <param name="schemaObjectPrefix" value="journal_"/>
    </Journal>
</Cluster>

在上述配置当中,Cluster节点都有一个名为syncDelay的属性,该属性用于定义当前节点每隔多久与数据库进行一次同步,单位是毫秒,这里设置的都是5000,就表示5000毫秒,也就是每隔5秒与数据库同步一次。同时,如果运行时出现下面错误,可以在Cluster节点还可以添加一个id属性,用于标识当前节点的ID。

集群下没有为Cluster节点指定id属性时出现的错误:

Failed to access cluster node id: \cluster_node.id

results matching ""

    No results matching ""