6 Replies Latest reply on May 7, 2009 5:16 PM by Arbi Sookazian

    Seam + EJB

    Maxim Kuzmik Newbie

      Hello, All.


      I'm neber with Seam. And have the next question: how can I create the bean using EJB3 and used it with Seam? And how to define the DataSource on Postgres? So my one Bean must use one DS, others second DS. How can I create this? Thanks.
      AS - is Jboss.

        • 1. Re: Seam + EJB
          Arbi Sookazian Master

          foo-ds.xml:


          <?xml version="1.0" encoding="UTF-8"?>
          
          <!DOCTYPE datasources
              PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
              "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
              
          <datasources>
             
             <local-tx-datasource>
                <jndi-name>fooDatasource</jndi-name>
                <connection-url>jdbc:sqlserver://CORG0DB901:1433;databaseName=foo</connection-url>
                <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
                <user-name>_AppUser_JavaTestAcct</user-name>
                <password>1234</password>     
             </local-tx-datasource>
             
             <!-- DB02 is prod instance, currently using for dev, UAT and prod envmts... -->   
             <local-tx-datasource>
                <jndi-name>barDatasource</jndi-name>
                <connection-url>jdbc:sqlserver://CORG0DB02\ORGSQLDW;databaseName=bar</connection-url>
                <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
                <user-name>_AppUser_SQL</user-name>
                <password>1234</password>     
             </local-tx-datasource>
               
          </datasources>



          replace all values with whatever is appropriate for your RDBMS.


          persistence.xml:


          <?xml version="1.0" encoding="UTF-8"?>
          <!-- Persistence deployment descriptor for dev profile -->
          <persistence 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_1_0.xsd" 
                       version="1.0">
                       
             <persistence-unit name="foo">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <jta-data-source>java:/fooDatasource</jta-data-source>
                <properties>
                   <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
                   <!-- <property name="hibernate.hbm2ddl.auto" value="validate"/>   -->
                   <property name="hibernate.show_sql" value="true"/>
                   <property name="hibernate.format_sql" value="true"/>
                   <property name="hibernate.generate_statistics" value="true"/>
                   <property name="jboss.entity.manager.factory.jndi.name" value="java:/fooEntityManagerFactory"/>
                   <property name="hibernate.default_catalog" value="foo"/>
                   <property name="hibernate.default_schema" value="dbo"/>
                </properties>
             </persistence-unit>
                
             <persistence-unit name="bar">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <jta-data-source>java:/barDatasource</jta-data-source>
                <properties>
                   <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>         
                   <property name="hibernate.show_sql" value="true"/>
                   <property name="hibernate.format_sql" value="true"/>
                   <property name="jboss.entity.manager.factory.jndi.name" value="java:/barEntityManagerFactory"/>
                   <property name="hibernate.default_catalog" value="bar"/>
                   <property name="hibernate.default_schema" value="dbo"/>
                </properties>
             </persistence-unit>
             
          </persistence>



          Again, replace values as appropriate.


          components.xml:


          <persistence:managed-persistence-context name="entityManagerFoo"
                                               auto-create="true"
                                persistence-unit-jndi-name="java:/fooEntityManagerFactory"/>     
                                
             <persistence:managed-persistence-context name="entityManagerBar"
                                               auto-create="true"
                                persistence-unit-jndi-name="java:/barEntityManagerFactory"/>   



          EJB (assuming you are using Hibernate as your persistence provider):


          @Name("myBean")
          @Stateful
          public class MyBeanAction implements MyBeanLocal {
          
             @In EntityManager entityManagerFoo;  //inject SMPC
          
             @Begin(join=true, flushMode=FlushModeType.MANUAL)
             public void begin(){
                   //Stuff is a JPA entity class...
                   List<Stuff> stuffList = entityManagerFoo.createQuery("select s from Stuff s").getResultList();
             ...
             }
          
             public void otherMethod(){
             //perform some biz logic
             ...
             }
          
             @End
             public submit() {
                   EntityManagerFoo.flush();
             }
          }



          You can inject EntityManagerBar into the same EJB as well if you need to.  Make sure you don't use both EntityManager instances in the same transaction, otherwise you will need XA/2PC support.


          This example demonstrates how to implement an atomic conversation (aka application tx as per DAllen) using Seam Managed Persistence Context (SMPC) and Hibernate MANUAL flush mode.


          Read the Seam ref doc and Seam books for more info.

          • 2. Re: Seam + EJB
            Arbi Sookazian Master

            also, check out the booking example in the Seam distro.


            In Seam 2.1.1.GA distro, notice the following.


            HotelBookingAction:


            @PersistenceContext(type=EXTENDED)
            private EntityManager em;



            HotelSearchingAction:


            @PersistenceContext
            private EntityManager em;



            It's interesting that the Seam core dev(s), use @PersistenceContext to inject the EntityManager instance rather than @In@PersistenceContext is EJB3 specific and @In is Seam specific.


            The first example above is an extended PC, which is scoped to the lifetime of the SFSB.  The second example above is a regular PC, scoped to the lifetime/duration of a single transaction (each business method in EJB3 requires a transaction by default).


            It is recommended to use @In to inject SMPC in Seam apps.  SMPC is conversation-scoped, and is available as the same PC in multiple Seam components that are injected/involved in the same LRC.

            • 3. Re: Seam + EJB
              Maxim Kuzmik Newbie

              Only one question where I must put this file foo-ds.xml?

              • 4. Re: Seam + EJB
                Maxim Kuzmik Newbie
                <property name="hibernate.default_catalog" value="foo"/>
                <property name="hibernate.default_schema" value="dbo"/>
                



                hibernate.defaultcatalog - this is the DB name if i rgiht undertand?

                hibernate.default
                schema - this is schema?

                • 5. Re: Seam + EJB
                  Arbi Sookazian Master

                  Maxim Kuzmik wrote on May 07, 2009 09:20:


                  Only one question where I must put this file foo-ds.xml?


                  foo/resources (check the booking example)

                  • 6. Re: Seam + EJB
                    Arbi Sookazian Master

                    default_catalog is the DB name.  This is pre-configured for you when you run 'ant setup' via seam-gen and answer the interview questions.  Read the seam ref docs for more info.