2.8. Database configuration for Hibernate

As usual, it is quite simple to use the configuration XML syntax to configure and parameterize different databases for eXo tables but also for your own use.

Generic configuration

The default DB configuration uses HSQLDB - a Java database that is quite useful for demonstration.


<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>
  • The init-params section defines default properties of Hibernate, including DB URL, driver and credentials in use.

  • For any portals, those configurations can be overridden, depending on needs of your environment.

  • HSQLDB can only be used for development environments and for demonstration. In production, many databases are supported. For example, 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

You can use the eXo Hibernate service and register your Hibernate files (hbm.xml) to leverage some features, such as table auto-creation and the Hibernate session cache in a ThreadLocal object during the whole request lifecycle. To do so, you just need 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>
Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus