eXo Platform 3.5

## Chapter 79. Database Configuration for Hibernate

As usual, it is quite simple to use our configuration XML syntax to configure and parametrize different Databases for eXo tables but also for your own use.

## Generic configuration

The default DB configuration uses HSQLDB, a Java Database quite useful for demonstrations.

    <component>

       <key>org.exoplatform.services.database.HibernateService</key>

       <jmx-name>exo-service:type=HibernateService</jmx-name>

       <type>org.exoplatform.services.database.impl.HibernateServiceImpl</type>

       <init-params>

          <properties-param>

             <name>hibernate.properties</name>

             <description>Default Hibernate Service</description>

             <property name="hibernate.show_sql" value="false"/>

             <property name="hibernate.cglib.use_reflection_optimizer" value="true"/>

             <property name="hibernate.connection.url" value="jdbc:hsqldb:file:../temp/data/portal"/>

             <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>

             <property name="hibernate.connection.autocommit" value="true"/>

             <property name="hibernate.connection.username" value="sa"/>

             <property name="hibernate.connection.password" value=""/>

             <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>

             <property name="hibernate.c3p0.min_size" value="5"/>

             <property name="hibernate.c3p0.max_size" value="20"/>

             <property name="hibernate.c3p0.timeout" value="1800"/>

             <property name="hibernate.c3p0.max_statements" value="50"/>

          </properties-param>

       </init-params>

    </component>

In the init parameter section, we define the default hibernate properties including the DB URL, the driver and the credentials in use.

For any portals that configuration can be overridden, depending on the needs of your environment.

Several databases have been tested and can be used in production....which is not the case of HSQLDB, HSQLDB can only be used for development environments and for demonstrations.

## Example DB configuration

For MySQL

    <component>

       <key>org.exoplatform.services.database.HibernateService</key>

       <jmx-name>database:type=HibernateService</jmx-name>

       <type>org.exoplatform.services.database.impl.HibernateServiceImpl</type>

       <init-params>

          <properties-param>

             <name>hibernate.properties</name>

             <description>Default Hibernate Service</description>

             <property name="hibernate.show_sql" value="false"/>

             <property name="hibernate.cglib.use_reflection_optimizer" value="true"/>

             <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/exodb?relaxAutoCommit=true&amp;amp;autoReconnect=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf8"/>

             <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

             <property name="hibernate.connection.autocommit" value="true"/>

             <property name="hibernate.connection.username" value="exo"/>

             <property name="hibernate.connection.password" value="exo"/>

             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

             <property name="hibernate.c3p0.min_size" value="5"/>

             <property name="hibernate.c3p0.max_size" value="20"/>

             <property name="hibernate.c3p0.timeout" value="1800"/>

             <property name="hibernate.c3p0.max_statements" value="50"/>

           </properties-param>

       </init-params>

    </component>

## Registering custom Hibernate XML files into the service

It is possible to use the eXo hibernate service and register your hibernate hbm.xml files to leverage some add-on features of the service such as the table automatic creation as well as the cache of the hibernate session in a ThreadLocal object during all the request lifecycle. To do so, you just have to add a plugin and indicate the location of your files.

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <configuration>

      <external-component-plugins>

        <target-component>org.exoplatform.services.database.HibernateService</target-component>

        <component-plugin>

          <name>add.hibernate.mapping</name>

          <set-method>addPlugin</set-method>

          <type>org.exoplatform.services.database.impl.AddHibernateMappingPlugin</type>

          <init-params>

            <values-param>

              <name>hibernate.mapping</name>

              <value>org/exoplatform/services/organization/impl/UserImpl.hbm.xml</value>

              <value>org/exoplatform/services/organization/impl/MembershipImpl.hbm.xml</value>

              <value>org/exoplatform/services/organization/impl/GroupImpl.hbm.xml</value>

              <value>org/exoplatform/services/organization/impl/MembershipTypeImpl.hbm.xml</value>

              <value>org/exoplatform/services/organization/impl/UserProfileData.hbm.xml</value>

            </values-param>

          </init-params>

        </component-plugin>

      </external-component-plugins>

    </configuration>