4 Replies Latest reply on Apr 25, 2006 11:18 AM by Carsten Hoehne

    Database configuration with Seam+Hibernate on Tomcat/Microco

    Carsten Hoehne Master

      Hello,
      after searching all forums, documentation, FAQs for Seam, hibernate and the microcontainer i have still 2 questions.
      The first is:
      How can i configure multiple databases with the microcontainer?
      I have come this far:
      1. created a hibernate config file for each database (hibernate.cfg.xml and userdb.cfg.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
      
      <hibernate-configuration>
       <session-factory name="java:/bazDatabase">
       <property name="connection.datasource">java:/hibernateDatasource</property>
       <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
       <property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
       <property name="transaction.flush_before_completion">true</property>
       <property name="connection.release_mode">after_statement</property>
       <property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
       <property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
      <!-- Here are the mappings -->
       </session-factory>
      </hibernate-configuration>

      Both files differ in the name (bazDatabase,userDatabase) of the session-factory and the value of connection.datasource.

      2. Set up jboss-beans.xml
      i took the jboss-beans.xml file from the hibernate example of Jboss Seam 1.0 Beta2 and changed the prefix booking consistently with baz.
      This results in a running configuration for one database.
      I duplicated the configuration for beans called bazDatasourceFactory,bazDatasource,bazDatabase and bazDatabaseFactory and substituted the prefix baz with user. The configuration for userDatasourceFactory is adapted to point to the right database and i obeyed that the jndiName has to correspond with that in userdb.cfg.xml

      The question seems to be : How can i pass userdb.cfg.xml to the bean called userDatabaseFactory so that the sessionfactory uses a supplied hibernate configuration file.

      The second question is:
      How can i configure the databases at runtime?
      Before adapting my application to seam i used a Tomcat supplied Datasource. There was a context.xml file in the META-INF directory of the app. This file configured the datasource. At runtime the deployed version of context.xml is editable.
      But how can i achieve such a behavior with the microcontainer?

      Thanks for your attention and
      Ciao,
      Carsten Hoehne
      -------------------------------------------------
      For completness here is my jboss.beans.xml file:
      <?xml version="1.0" encoding="UTF-8"?>
      
      <deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_1_0.xsd"
       xmlns="urn:jboss:bean-deployer">
      
       <bean name="Naming" class="org.jnp.server.SingletonNamingServer"/>
      
      
       <bean name="TransactionManagerFactory" class="org.jboss.seam.microcontainer.TransactionManagerFactory"/>
       <bean name="TransactionManager" class="java.lang.Object">
       <constructor factoryMethod="getTransactionManager">
       <factory bean="TransactionManagerFactory"/>
       </constructor>
       </bean>
      
      
       <bean name="bazDatasourceFactory" class="org.jboss.seam.microcontainer.DataSourceFactory">
       <property name="driverClass">com.mysql.jdbc.Driver</property>
       <property name="connectionUrl">jdbc:mysql://localhost:3306/baz</property>
       <property name="userName">baz</property>
       <property name="jndiName">java:/hibernateDatasource</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>
      
       </bean>
       <bean name="bazDatasource" class="java.lang.Object">
       <constructor factoryMethod="getDataSource">
       <factory bean="bazDatasourceFactory"/>
       </constructor>
       </bean>
      
       <bean name="bazDatabaseFactory" class="org.jboss.seam.microcontainer.HibernateFactory"/>
       <bean name="bazDatabase" class="java.lang.Object">
       <constructor factoryMethod="getSessionFactory">
       <factory bean="bazDatabaseFactory"/>
       </constructor>
       <depends>bazDatasource</depends>
       </bean>
      
       <bean name="bazDatabase" class="java.lang.Object">
       <constructor factoryMethod="getSessionFactory">
       <factory bean="bazDatabaseFactory"/>
       </constructor>
       <depends>bazDatasource</depends>
       </bean>
      
      <!-- User Database -->
       <bean name="userDatasourceFactory" class="org.jboss.seam.microcontainer.DataSourceFactory">
       <property name="driverClass">com.mysql.jdbc.Driver</property>
       <property name="connectionUrl">jdbc:mysql://localhost:3306/user</property>
       <property name="userName">baz</property>
       <property name="jndiName">java:/userDatasource</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>
      
       </bean>
       <bean name="userDatasource" class="java.lang.Object">
       <constructor factoryMethod="getDataSource">
       <factory bean="userDatasourceFactory"/>
       </constructor>
       </bean>
       <!-- Question: how to supply the correct hibernate config file userDB.cfg.xml -->
       <bean name="userDatabaseFactory" class="org.jboss.seam.microcontainer.HibernateFactory"/>
      
       <bean name="userDatabase" class="java.lang.Object">
       <constructor factoryMethod="getSessionFactory">
       <factory bean="userDatabaseFactory"/>
       </constructor>
       <depends>userDatasource</depends>
       </bean>
      
       </deployment>