5 Replies Latest reply on Jun 11, 2008 4:17 PM by Mark Coffin

    Seam managed Hibernate Session

    Frank Bitzer Newbie

      Hi there,


      I'm using Seam 2.0.2.GA and have a Seam managed Hibernate session defined in my components.xml like this:



      <!--  transaction management is disabled since we do not want to rely on any JEE container -->
              <transaction:no-transaction />
      
      
              <!-- let Seam take control of our Hibernate session -->
              <persistence:hibernate-session-factory name="hibernateSessionFactory"/>
      
              <persistence:managed-hibernate-session name="session" 
                                      auto-create="true"
                           session-factory-jndi-name="java:/sessionFactory"/>
      



      In my hibernate.cfg.xml I have



      <property name="hbm2ddl.auto">update</property>
      



      The database is a HSQL in Server mode.


      I'm using jetty with the maven-jetty-plugin to test my application:

      mvn jetty:run



      Everything works fine except one thing:
      when I close jetty and shutdown the web application, the content of my database is deleted (but the schema still exists).


      Usually, this behavior would occur when you set hbm2ddl.auto to create-drop, but not when using update.


      So I guess bad evil Seam deletes the database content for any reason ;-)


      How can I prevent this?


      Thanks in advance,
      Frank



        • 1. Re: Seam managed Hibernate Session
          Gena Batalski Newbie

          Hi Frank,


          are you sure, your transactions were commited and data were persisted successfully? It seems that, you need to commit transactions and possible also to flush the hibernate session manually to get transactions commited and data synchronized.


          If you use the create-drop also the schema will be deleted, excepting the case, when the schema doesn't match your model anymore. I think, in your case the hbm2ddl=update did work fine.


          Gena

          • 2. Re: Seam managed Hibernate Session
            Frank Bitzer Newbie

            Hi,


            thank you for your answer. The entities are saved to database, I can see them when I access the database with SQuirreL or another database explorer.
            The code I use is as follows:


            //inject session
            @In Session session;
            
            ...
            
            User user=new User();
            user.setName("coolUser");
            
            session.save(user);
            session.flush();
            



            Do I have to care for anything concerning transactions? As mentioned, I disabled transaction management.


            Cheers,
            Frank

            • 3. Re: Seam managed Hibernate Session
              Mark Coffin Newbie

              Sounds like one of the features of HSQLDB.



              Memory tables are the default type when the CREATE TABLE command is used. 
              Their data is held entirely in memory but any change to their structure or contents is written to the <dbname>.script file. 
              The script file is read the next time the database is opened, and the MEMORY tables are recreated with all their contents.




              Another possible cause, if your connecting like this:



              Memory-Only Databases
              
              It is possible to run HSQLDB in a way that the database is not persistent and exists entirely in random access memory. 
              As no information is written to disk, this mode should be used only for internal processing of application data, 
              in applets or certain special applications. 
              This mode is specified by the mem: protocol.
              
              Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:aname", "sa", "");





              • 4. Re: Seam managed Hibernate Session
                Frank Bitzer Newbie

                OK, thank you for this information.


                I thought when using HSQL in server mode the database would store the data on disk (and not only the schema...). But it looks like this assumption was wrong.


                So I will try with another database.


                Do you have any proposals for a small, lightweight database system that does not need any installation or administration (like e.g. mySQL does)?


                --
                Frank


                • 5. Re: Seam managed Hibernate Session
                  Mark Coffin Newbie

                  Sorry, your choices are pretty limited.  Even HSQL requires some administration from time to time.


                  I highly recommend you read the documentation, it will save you a lot of time and frustration.


                  HSQL can store the information on disk. Create a properties file for the server and add the following property:



                  hsqldb.default_table_type=cached




                  Documentation for HSQL can be found here.