This tutorial shows you how to configure JBoss 6 and your persistence.xml file in order to use JBoss’ embedded HSQL database in Server Mode. “Server Mode” means that HSQLDB will run as a server and will be able to accept connections, just like MySQL. All that is useful in case you use Eclipse and want to validate your project against the datasource.

1. Open $JBOSS_HOME/server/default/deploy/hsqldb-ds.xml

You have to modify the DefaultDS datasource. Some sections of hsqldb-ds.xml will have to be uncommented, and others will have to be commented. I’ll show you such sections as they should be after the alterations.
I’ll consider you want to use a database of your choice: MYPERSONALDB.

      <!-- For server mode db, allowing other processes to use hsqldb over tcp.
      This requires the org.jboss.jdbc.HypersonicDatabase mbean.
      -->
      <connection-url>
          <value-factory bean="ServiceBindingManager" method="getStringBinding">
             <parameter>Hypersonic</parameter>
             <parameter>jdbc:hsqldb:hsql://${hostforurl}:${port}</parameter>
          </value-factory>
      </connection-url>
      <!-- For in-process persistent db, saved when jboss stops.
      The org.jboss.jdbc.HypersonicDatabase mbean is required for proper db shutdown
      -->
      <connection-url>jdbc:hsqldb:${jboss.server.data.dir}${/}hypersonic${/}MYPERSONALDB</connection-url>
      <!-- When using in-process (standalone) mode -->
      <!--<depends>jboss:service=Hypersonic,database=localDB</depends>-->

      <!-- Uncomment when using hsqldb in server mode-->
      <depends>jboss:service=Hypersonic</depends>
   <!-- Uncomment if you want hsqldb accessed over tcp (server mode)-->
   <mbean code="org.jboss.jdbc.HypersonicDatabase" name="jboss:service=Hypersonic">
     <attribute name="Port">
        <value-factory bean="ServiceBindingManager" method="getIntBinding" parameter="jboss:service=Hypersonic"/>
     </attribute>
     <attribute name="BindAddress">
        <value-factory bean="ServiceBindingManager" method="getStringBinding" parameter="jboss:service=Hypersonic"/>
     </attribute>
     <attribute name="Silent">true</attribute>
     <attribute name="Database">MYPERSONALDB</attribute>
     <attribute name="Trace">false</attribute>
     <attribute name="No_system_exit">true</attribute>
   </mbean>
   <!-- For hsqldb accessed from jboss only, in-process (standalone) mode -->
   <!--<mbean code="org.jboss.jdbc.HypersonicDatabase" name="jboss:service=Hypersonic,database=localDB">
     <attribute name="Database">localDB</attribute>
     <attribute name="InProcessMode">true</attribute>
   </mbean>-->

2. Open $JBOSS_HOME/server/default/conf/bindingservice.beans/META-INF/bindings-jboss-beans.xml

You have to uncomment a section:

            <!-- ********************* deploy/hsqldb-ds.xml **************** -->

            <!-- Commented out as tcp/ip access to Hypersonic is not enabled by default -->

            <bean class="org.jboss.services.binding.ServiceBindingMetadata">
               <property name="serviceName">jboss:service=Hypersonic</property>
               <property name="port">1701</property>
               <property name="description">TCP/IP socket for remote connection to Hypersonic database</property>
            </bean>

3. Edit your persistence.xml properly:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
	xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

	<persistence-unit name="MYPU" transaction-type="JTA">

		<provider>org.hibernate.ejb.HibernatePersistence</provider>

		<jta-data-source>java:DefaultDS</jta-data-source>

		<!-- Add all your other mapped classes, for example: -->
		<class>com.myapplication.domain.User</class>

		<properties>
			<property name="hibernate.archive.autodetecion" value="class, hbm" />
			<property name="hibernate.show_sql" value="true"/>
			<property name="hibernate.format_sql" value="true"/>
			<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
			<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
			<property name="hibernate.connection.username" value="sa"/>

			<!-- You must have this if you are using JTA and JBoss! -->
			<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>

		</properties>

	</persistence-unit>
</persistence>
 

Leave a Reply