Port of schuchert.wikispaces.com


Ejb_3_Tutorial_2_Optional_Data_Source_Configuration

Ejb_3_Tutorial_2_Optional_Data_Source_Configuration

The persistence.xml file mentions the possibility of using your own data source rather than the default data source to hit a different database.

When you use the embedded container, it looks for a file called embedded-jboss-beans.xml for its datasources (and several other things). In the one that ships with the ALPHA 9 release, you’ll see the following two entries near the bottom:

   <bean name="DefaultDSBootstrap"
         class="org.jboss.resource.adapter.jdbc.local.LocalTxDataSource">
      <property name="driverClass">org.hsqldb.jdbcDriver</property>
      <property name="connectionURL">jdbc:hsqldb:.</property>
      <property name="userName">sa</property>
      <property name="jndiName">java:/DefaultDS</property>
      <property name="minSize">0</property>
      <property name="maxSize">10</property>
      <property name="blockingTimeout">1000</property>
      <property name="idleTimeout">100000</property>
      <property name="transactionManager">
          <inject bean="TransactionManager"/>
      </property>
      <property name="cachedConnectionManager">
          <inject bean="CachedConnectionManager"/>
      </property>
      <property name="initialContextProperties">
          <inject bean="InitialContextProperties"/>
      </property>
   </bean>

   <bean name="DefaultDS" class="java.lang.Object">
      <constructor factoryMethod="getDatasource">
         <factory bean="DefaultDSBootstrap"/>
      </constructor>
   </bean>

To create your own data source, you basically copy both of these and update the values accordingly. Here is one example that uses HSQL with a local server:

   <bean name="HypersonicLocalServerDSBootstrap" 
         class="org.jboss.resource.adapter.jdbc.local.LocalTxDataSource">
      <property name="driverClass">org.hsqldb.jdbcDriver</property>
      <property name="connectionURL">jdbc:hsqldb:hsql://localhost/xdb</property>
      <property name="userName">sa</property>
      <property name="jndiName">java:/HypersonicLocalServerDS</property>
      <property name="minSize">0</property>
      <property name="maxSize">10</property>
      <property name="blockingTimeout">1000</property>
      <property name="idleTimeout">100000</property>
      <property name="transactionManager">
          <inject bean="TransactionManager"/>
      </property>
      <property name="cachedConnectionManager">
          <inject bean="CachedConnectionManager"/>
      </property>
      <property name="initialContextProperties">
          <inject bean="InitialContextProperties"/>
      </property>
   </bean>
   
   <bean name="HypersonicLocalServerDS" class="java.lang.Object">
      <constructor factoryMethod="getDatasource">
         <factory bean="HypersonicLocalServerDSBootstrap"/>
      </constructor>
   </bean>

To use this, you need to do two things. Update persistence.xml and start a HSQL server.

Update persistence.xml

Replace the following line:

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

With this one:

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

To start an HSQL server, you can use the following steps:

java -cp ../lib/hsqldb.jar org.hsqldb.Server -database.0 file:mydb -dbname.0 xdb

Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.