13 Replies Latest reply on Apr 7, 2009 11:37 PM by Charls Cross

    Seam, Glassfish and Mysql

    Charls Cross Newbie

      Hello again! I need to run an Seam EAR app on glassfish. I've done the examples, and I can deploy them with the dafault database. But I couldn't find any example configurating a Mysql or Oracle database. All the docs are using the jdbc/_default jta-datasource.
      It's because of my ignorance in glassfish, I've been reading and I know that I need to create a connection pool on the glassfish and link it with my persistence.xml and components.xml, but It will be good to find some doc explaning the changes witch  are needed on this files for making it work with a real database.


      On the other hand, seems that I could not use EJBs with glassfish and EAR, well I'm not very sure about this, the fact is that I need to work with EntityHome and after reading a lot seems that I have to make them Stateful sesion bens and to write local Interfaces for every class, but didn't find any doc explaining this proccess. If is posible working with the Seam generated classes, has it a hight cost rewriting generated code?


      Please if someone has experience on this enviroment (seam-gen, mysql, glassfish) and can provide samples of how them make it work, or just share a little of information please do it, I'll be very pleased.


      Thanks!

        • 1. Re: Seam, Glassfish and Mysql
          Dan Allen Master

          I need to run an Seam EAR app on glassfish. I've done the examples, and I can deploy them with the dafault database. But I couldn't find any example configurating a Mysql or Oracle database. All the docs are using the jdbc/_default jta-datasource.
          It's because of my ignorance in glassfish, I've been reading and I know that I need to create a connection pool on the glassfish and link it with my persistence.xml and components.xml, but It will be good to find some doc explaning the changes witch  are needed on this files for making it work with a real database.


          Please see a project created using seam-gen (from SVN or the upcoming 2.1.2 release) for some notes about what needs to change. Also see http://code.google.com/p/seaminaction/wiki/DeployingToGlassFish and http://in.relation.to/Bloggers/JBossAS5AndGlassFishSupportAddedToSeamgen. You can find information about how to add a new datasource to GlassFish in the GlassFish reference guide (though it's pretty easy to figure out what to do using the GlassFish Admin Console).



          On the other hand, seems that I could not use EJBs with glassfish and EAR, well I'm not very sure about this


          This is not true. However, you do need to register resource references in web.xml if you want to access them from Seam.


          The fact is that I need to work with EntityHome and after reading a lot seems that I have to make them Stateful sesion bens and to write local Interfaces for every class, but didn't find any doc explaining this proccess. If is posible working with the Seam generated classes, has it a hight cost rewriting generated code?


          The Seam Application Framework classes cannot easily be made into EJBs. You should consider them for JavaBean components only.

          • 2. Re: Seam, Glassfish and Mysql
            Charls Cross Newbie

            Thanks Dan!
            Can I use the 2.1.2 trunk for buiding the project from there?? I checked out, but when I run the glassfish commands tells me that glassfish.home doesn't point at any valid installation, I don't kown if this happens beacause this version is still under contruction and doesn't work of if I'm doing something wrong. I've edit the build.properties and write:


            glassfish.home=C:/seamGF/glassfish
            



            As you can see, I'm using windows. I think that this line on glasfish-build.xml makes it fail:


            <not><available file="${glassfish.home}\bin\asadmin"/></not>
            


            Running windows should I have to give any type of permissions to glassfish? Glassfish alone is working from the command line, so I don't know where's the problem.


            • 3. Re: Seam, Glassfish and Mysql
              Charls Cross Newbie

              So I don't have to declare the EntityHome classes in web xml? Will they work without any extra work?

              • 4. Re: Seam, Glassfish and Mysql
                Charls Cross Newbie

                Well, seems that the problem happens when ant executes glassfish asadmin command, I've tried with other commands and worked fine, do I need extra configuration on glassfish?

                • 5. Re: Seam, Glassfish and Mysql
                  Charls Cross Newbie

                  This is everything I've done without success:


                  I followed chapter 39 of the reference with seam-2.1.1.GA. I could make the example, so I have an ant buildfile for making the EAR.
                  The I've tried to make the changes suggested on seam-2.1.2-SNAPSHOT in my project:


                  
                   1. Uncomment the <ejb-local-ref> entries in resources/WEB-INF/web.xml (include additional entries as necessary)
                    2. Strip the contents up to and including the # in the element <persistence-unit-name> in resources/WEB-INF/web.xml
                    3. Uncomment the <jar-file> element in resources/META-INF/persistence-dev.xml & resources/META-INF/persistence-prod.xml
                  



                  I also modified the build.xml for sending persistence.xml to the WAR/WEB-INF dir, as I understood to do it reading this in persistence.xml and web.xml:


                   <!-- The relative reference doesn't work on GlassFish. Instead, set the <persistence-unit-name> to "testing",
                             package persistence.xml in the WAR, and add a <jar-file> element in persistence.xml with value "../../testing.jar". -->
                       
                  
                  <!-- The <jar-file> element is necessary if you put the persistence.xml in the WAR and the classes in the JAR -->
                  



                  I have found this exception:


                  EntityManagerFactory not found in JNDI : java:/seamgen_exampleEntityManagerFactory
                  
                  



                  This are the things I added on web.xml:


                   <ejb-local-ref>              
                      <ejb-ref-name>seamgen_example/AuthenticatorAction</ejb-ref-name>                
                      <ejb-ref-type>Session</ejb-ref-type>     
                      <local-home></local-home>
                      <local>com.cross.example.Authenticator</local>  
                    </ejb-local-ref>
                     
                    <ejb-local-ref>
                      <ejb-ref-name>seamgen_example/EjbSynchronizations</ejb-ref-name>  
                      <ejb-ref-type>Session</ejb-ref-type>
                      <local-home></local-home>
                      <local>org.jboss.seam.transaction.LocalEjbSynchronizations</local>
                    </ejb-local-ref>
                    
                    <persistence-unit-ref>
                      <persistence-unit-ref-name>seamgen_example/pu</persistence-unit-ref-name>
                      <persistence-unit-name>seamgen_example</persistence-unit-name>
                  </persistence-unit-ref>
                  



                  On components.xml


                  <transaction:ejb-transaction /> 
                  
                   <core:init debug="@debug@" jndi-pattern="java:comp/env/seamgen_example/#{ejbName}"/>
                  
                  <persistence:managed-persistence-context name="entityManager"
                         auto-create="true"
                    persistence-unit-jndi-name="java:/seamgen_exampleEntityManagerFactory"/>
                  



                  And persistence.xml:


                  <persistence-unit name="seamgen_example">
                        <provider>org.hibernate.ejb.HibernatePersistence</provider>
                        <jta-data-source>java:/seamgen_exampleDatasource</jta-data-source>
                        <jar-file>../../seamgen_example.jar</jar-file> 
                        <properties>
                           <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
                           <property name="hibernate.hbm2ddl.auto" value="validate"/>
                           <property name="hibernate.jdbc.batch_size" value="20"/>
                           <property name="jboss.entity.manager.factory.jndi.name" value="java:/seamgen_exampleEntityManagerFactory"/>
                        </properties>
                     </persistence-unit>
                  



                  Finally, I create a jdbc_resource on glassfish named 


                  jdbc/__seamgen_exampleDatasource
                  

                   

                  • 6. Re: Seam, Glassfish and Mysql
                    Christiaan Ypma Newbie

                    Running windows should I have to give any type of permissions to glassfish? Glassfish alone is working from the command line, so I don't know where's the problem.

                    It is not the permission, it is the fact that it is asadmin.bat on windows, so change your glassfish-build.xml and add .bat, then it works nicely (took some time for me also to figure that one out :) )

                    • 7. Re: Seam, Glassfish and Mysql
                      Charls Cross Newbie

                      I tried before, It just finds the archive in the avaliable, but when it tries to do exec it fails, I've read that could be an XP Bug in ant.
                      But I followed also your instructions, adding a .bat on every exec asadmin command, and fails too.
                      Which OS are you running? If it worked for you (build.xml ant commands for Glassfish on Seam 2.1.2 on WinXP), please can you tell how you did it? 

                      • 8. Re: Seam, Glassfish and Mysql
                        Dan Allen Master

                        My bad! I forgot to check Windows! I just upgraded the glassfish-build.xml in SVN with the following two fixed:



                        1. Properly detect asadmin.bat when the OS is Windows

                        2. Use cmd.exe to execute asadmin.bat



                        Please test!

                        • 9. Re: Seam, Glassfish and Mysql
                          Dan Allen Master

                          I have found this exception:

                          EntityManagerFactory not found in JNDI : java:/seamgen_exampleEntityManagerFactory




                          Here in lies your problem. This is a JBoss AS specific JNDI name.



                          <persistence:managed-persistence-context name="entityManager"
                                 auto-create="true"
                            persistence-unit-jndi-name="java:/seamgen_exampleEntityManagerFactory"/>




                          Again, this JNDI name is wrong. It should be java:comp/env/seamgen_example/pu as defined by the persistence unit reference in web.xml.


                          The data source in persistence.xml should be java:comp/env/jdbc/__seamgen_exampleDatasource.


                          Please pay close attention to the replacements performed by the seam-gen EAR build for GlassFish. If you are using a java:/ prefix anywhere, you know you are doing something wrong.

                          • 10. Re: Seam, Glassfish and Mysql
                            Dan Allen Master

                            You don't have to do anything special with any Seam JavaBean component. That would include the EntityHome and EntityQuery classes, amongst others.

                            • 11. Re: Seam, Glassfish and Mysql
                              Christiaan Ypma Newbie

                              hmm, I'm running Vista 64 bit, maybe ant behaves differently on Vista compared to XP? Dan, I will give it a try tomorrow when I have time.

                              • 12. Re: Seam, Glassfish and Mysql
                                Charls Cross Newbie

                                Ok, now works on macrodef asadmin, but you must do the same fix to target gf-deploy-hibernate, I think with this we are ok.


                                Thanks a lot! I'm starting to check it now! 

                                • 13. Re: Seam, Glassfish and Mysql
                                  Charls Cross Newbie

                                  Really surprised of how are now seam and glassfish integrated! It has be an easy task to create the seam project and take it to the glassfish server, generating pools and datasources, and deploying the project from the ant script!


                                  Good job!
                                  Thanks a lot both of you!