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

    Database configuration with Seam+Hibernate on Tomcat/Microco

    baz

      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>


        • 1. Re: Database configuration with Seam+Hibernate on Tomcat/Mic
          gavin.king

          (1) Currently the built-in component org.jboss.seam.core.hibernate only supports bootstrapping a single instance of Hibernate. This is a silly limitation and there is a JIRA task to fix this. I'll get on it.

          In the meantime, you can should be able to simply create a second component that knows how to boot a second instance of Hibernate like so:

          @Scope(ScopeType.APPLICATION)
          @Intercept(NEVER)
          @Startup(depends="org.jboss.seam.core.microcontainer")
          @Name("org.mydomain.myapp.core.myHibernate")
          public class MyHibernate extends Hibernate {}


          Then set

          org.mydomain.myapp.core.myHibernate.cfgResourceName=myHibernate.cfg.xml



          (2) I'm not sure, you'll need to ask Adrian in the microcontainer forum...

          • 2. Re: Database configuration with Seam+Hibernate on Tomcat/Mic
            mruegner

            To reference a special (not the default) hibernate configuration file you can specify the cfgResourceName-Attribute for the factory.

            <bean name="someDatabaseFactory" class="org.jboss.seam.microcontainer.HibernateFactory">
             <property name="cfgResourceName">someHibernate.cfg.xml</property>
            </bean>
            


            Hope this helps...

            • 3. Re: Database configuration with Seam+Hibernate on Tomcat/Mic
              gavin.king

               

              "mruegner" wrote:
              To reference a special (not the default) hibernate configuration file you can specify the cfgResourceName-Attribute for the factory.

              <bean name="someDatabaseFactory" class="org.jboss.seam.microcontainer.HibernateFactory">
               <property name="cfgResourceName">someHibernate.cfg.xml</property>
              </bean>
              


              Hope this helps...


              Ohyes, this is another way, it works only if you are running on JBoss Microcontainer, however - not if you are running in JavaEE.

              • 4. Re: Database configuration with Seam+Hibernate on Tomcat/Mic
                baz

                Hello,
                thanks for the fast answers.

                <bean name="someDatabaseFactory" class="org.jboss.seam.microcontainer.HibernateFactory">
                 <property name="cfgResourceName">someHibernate.cfg.xml</property>
                </bean>

                This is exactly what i am searching for.
                when looking at http://docs.jboss.com/seam/api/org/jboss/seam/microcontainer/HibernateFactory.html i had no idea that
                'cfgResourceName' could be the name of the property. Many thanks for helping me out.
                But do also not forget to set org.jboss.seam.core.init.managedSessions (e.g. in web.xml) to all of your hibernatesessions:-)

                Ciao,
                Carsten