Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Include Pageshared:Setting up Embedded H2 (V1)shared:Setting up Embedded H2 (V1)The following sections describe how to replace the default H2 databases with Embedded H2:

Table of Contents
maxLevel3
styleborder:1
locationtop
separatorpipe

Creating The Database

Follow the steps below to set up an Embedded H2 database:

Setting up H2

Download and install the H2 database engine in your computer.

Info

For instructions on installing DB2 Express-C, see H2 installation guide.

Setting up drivers

WSO2 currently ships H2 database engine version h2-1.2.140.* and its related H2 database driver. If you want to use a different H2 database driver, take the following steps:

  1. Delete the following H2 database-related JAR file, which is shipped with WSO2 products:
    <PRODUCT_HOME>/repository/components/plugins/h2-database-engine_1.2.140.wso2v3.jar
  2. Find the JAR file of the new H2 database driver (<H2_HOME>/bin/h2-*.jar, where <H2_HOME> is the H2 installation directory) and copy it to your WSO2 product's <PRODUCT_HOME>/repository/components/lib/ directory.

Creating database tables

Create the database tables either manually by running a script or automatically by using a startup parameter as described below.

Anchor
Using the script
Using the script
Using the script

You can create database tables manually by executing the following scripts in H2 shell or web console:

  • To create tables for the registry and user manager database, use the following script: 
    <PRODUCT_HOME>/dbscripts/h2.sql
  • To create tables for the product specific databases, use the following script:
    <PRODUCT_HOME>/dbscripts/<PRODUCT_NAME>/h2.sql
  • Excerpt
    hiddentrue

    NOTE TO WRITERS: Add the following content only i f it is applicable to your product.

    To create tables for the identity databases, use the following script:

    Code Block
    languagepowershell
    <PRODUCT_HOME>/dbscripts/identity/h2.sql
  • Excerpt
    hiddentrue

    NOTE TO WRITERS: Add the following content only i f it is applicable to your product.

    To create tables for the storage databases, use the following script:

    Code Block
    languagepowershell
    <PRODUCT_HOME>/dbscripts/storage/h2.sql

Follow the steps below to run the script in web console:

  1. Run the ./h2.sh command to start the web console.
  2. Copy the script text from the SQL file.
  3. Paste it into the console.
  4. Click Run.
  5. After setting up the database tables, start the server by executing one of the following commands:
  • For Linux:
    sh wso2server.sh
  • For Windows:
    wso2server.bat
Anchor
Using a startup parameter
Using a startup parameter
Using a startup parameter

You can create database tables automatically when starting the product for the first time by using the -Dsetup parameter as follows:

  • For Linux:
    sh wso2server.sh -Dsetup
  • For Windows:
    wso2server.bat -Dsetup

Creating A Datasource For The Database

After creating the database, you create a datasource to point to it in the following files:

  1. Edit the default datasource configuration defined in the master-datasources.xml file, located in the <PRODUCT_HOME>/repository/conf/datasources/ directory, as shown below. The database configurations in registry.xml and user-mgt.xml refer to this datasource.

    Info

    Replace the  urlusernamepassword  and  driverClassName  settings with your custom values and also the other values accordingly in the  master-datasources.xml  file as shown below.

    Code Block
    languagehtml/xml
    <datasource>
        <name>WSO2_CARBON_DB</name>
        <description>The datasource used for registry and user manager</description>
        <jndiConfig>
             <name>jdbc/WSO2CarbonDB</name>
        </jndiConfig>
        <definition type="RDBMS">
             <configuration>
                 <url>jdbc:h2:repository/database/WSO2CARBON_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000</url>
                 <username>wso2carbon</username>
                 <password>wso2carbon</password>
                 <driverClassName>org.h2.Driver</driverClassName>
                 <maxActive>50</maxActive>
                 <maxWait>60000</maxWait>
                 <minIdle>5</minIdle>
                 <testOnBorrow>true</testOnBorrow>
                 <validationQuery>SELECT 1</validationQuery>
                 <validationInterval>30000</validationInterval>
             </configuration>
        </definition>
    </datasource>
    

    Following are the datasource configuration options:

    • url - The URL of the database.
    • userName - The name of the database user.
    • password - The password of the database user.
    • driverClassName - The class name of the database driver.
    • maxActive - The maximum number of active connections that can be allocated from this pool at the same time, or enter a negative value for no limit.
    • maxWait - The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception. You can enter zero or a negative value to wait indefinitely.
    • minIdle - The minimum number of active connections that can remain idle in the pool, without extra ones being created, or enter zero to create none.
    • testOnBorrow -  The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and another attempt will be made to borrow another. 
    • validationQuery - The SQL query that will be used to validate connections from this pool before returning them to the caller.
    • validationInterval -  The indication to avoid excess validation, and only run validation at the most, at this frequency (time in milliseconds). If a connection is due for validation, but has been validated previously within this interval, it will not be validated again. 

      Info

      For more information on other parameters that can be defined in the master-datasources.xml file, see Tomcat JDBC Connection Pool.

  2. Excerpt
    hiddentrue

    NOTE TO WRITERS: Add the following content as the second step, and replace <PRODUCT_HOME>/repository/conf/<PRODUCT_NAME>/<PRODUCT_NAME>.xml, with the correct folder path of your product.

    If you changed the product specific database, add the datasource configuration in the  <PRODUCT_HOME>/repository/conf/<PRODUCT_NAME>/<PRODUCT_NAME>.xml file as well. 

    Info

    This step is not relevant if you changed the registry and user manager database.

  3. Excerpt
    hiddentrue

    NOTE TO WRITERS: Add the following content only i f it is applicable to your product.

    If you changed the databases used for identity and storage, copy the WSO2_CARBON_DB datasource configuration for each of them in < PRODUCT_HOME>/repository/conf/datasources/master-datasources.xml file and replace its default values with your custom values.

  4. Excerpt
    hiddentrue

    NOTE TO WRITERS: Add the following content only i f it is applicable to your product.

    Replace the default values in the datasource configurations of the following files with your custom values accordingly, to point to the created registry and user manager, identity and storage databases.

    • <PRODUCT_HOME>/repository/conf/identity.xml
    • <PRODUCT_HOME>/repository/conf/user-mgt.xml
    • <PRODUCT_HOME>/repository/conf/registry.xml