3 Replies Latest reply on Dec 14, 2007 4:43 AM by Loic

    second level cache

    Loic Newbie

      Hi All,

      I'm using EJB3 on jboss4.0.5.GA connected on MSSQLServer using jtds.

      Everything's going well instead a problem on cache.

      Here is the test i'm doing :

      I have a sample entity :

      @Entity
      @Table(name="FileToSend")
      public class FileToSend


      And i'm using it in a stateful session bean where I use a fonction to test cache
      @Stateful
      public class FileToSendBean implements RemoteFileToSend
      




      public void findAll() {
       // TODO Auto-generated method stub
       for(int i=0;i<6;i++){
       em.createQuery("SELECT c FROM FileToSend c").getResultList();
       }
       }



      I am calling it threw a fonction in a servlet :


      ctx = new InitialContext();
      fileToSendService= (RemoteFileToSend) ctx.lookup("FileToSendBean/remote");
      
       for(int i=0;i<6;i++){
      fileToSendService.findAll();
      }




      Each call to the findAll fonction is well done and i get the results each time.

      But The matter that i have is that the request is processed each time on SQL Server.

      I think if i'm not crazy that launching several times the same query doesn't need to get the information in SQL server but in the cache, isn't it?

      So I think I'm missing something... please help!!


      Here is the log i got when starting my ejb ... all seems well to me.. and second level cache is enable :

      2007-11-14 19:23:27,437 INFO [org.jboss.ejb3.JmxKernelAbstraction] installing MBean: persistence.units:jar=ejbsupplier.jar,unitName=edieyessupplier with dependencies:
      2007-11-14 19:23:27,437 INFO [org.jboss.ejb3.JmxKernelAbstraction] jboss.jca:name=TestDS,service=DataSourceBinding
      2007-11-14 19:23:27,453 INFO [org.hibernate.ejb.Ejb3Configuration] Processing PersistenceUnitInfo [
      name: edieyessupplier
      ...]
      2007-11-14 19:23:27,453 INFO [org.hibernate.ejb.Ejb3Configuration] found EJB3 Entity bean: com.edieyes.supplier.ejb3.entities.FileToSend
      2007-11-14 19:23:27,468 WARN [org.hibernate.ejb.Ejb3Configuration] Persistence provider caller does not implements the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.
      2007-11-14 19:23:27,468 INFO [org.hibernate.cfg.Configuration] Reading mappings from resource: META-INF/orm.xml
      2007-11-14 19:23:27,531 INFO [org.hibernate.ejb.Ejb3Configuration] [PersistenceUnit: edieyessupplier] META-INF/orm.xml found
      2007-11-14 19:23:27,546 INFO [org.hibernate.cfg.AnnotationBinder] Binding entity from annotated class: com.edieyes.supplier.ejb3.entities.FileToSend
      2007-11-14 19:23:27,546 INFO [org.hibernate.cfg.annotations.EntityBinder] Bind entity com.edieyes.supplier.ejb3.entities.FileToSend on table FileToSend
      2007-11-14 19:23:27,562 INFO [org.hibernate.connection.ConnectionProviderFactory] Initializing connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
      2007-11-14 19:23:27,578 INFO [org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider] Using provided datasource
      2007-11-14 19:23:27,578 INFO [org.hibernate.cfg.SettingsFactory] RDBMS: Microsoft SQL Server, version: 08.00.0194
      2007-11-14 19:23:27,578 INFO [org.hibernate.cfg.SettingsFactory] JDBC driver: jTDS Type 4 JDBC Driver for MS SQL Server and Sybase, version: 1.2.2
      2007-11-14 19:23:27,578 INFO [org.hibernate.dialect.Dialect] Using dialect: org.hibernate.dialect.SQLServerDialect
      2007-11-14 19:23:27,578 INFO [org.hibernate.transaction.TransactionFactoryFactory] Transaction strategy: org.hibernate.ejb.transaction.JoinableCMTTransactionFactory
      2007-11-14 19:23:27,578 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
      2007-11-14 19:23:27,578 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] instantiated TransactionManagerLookup
      2007-11-14 19:23:27,578 INFO [org.hibernate.cfg.SettingsFactory] Automatic flush during beforeCompletion(): disabled
      2007-11-14 19:23:27,578 INFO [org.hibernate.cfg.SettingsFactory] Automatic session close at end of transaction: disabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Scrollable result sets: enabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] JDBC3 getGeneratedKeys(): enabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Connection release mode: auto
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Default batch fetch size: 1
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Generate SQL with comments: disabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Order SQL updates by primary key: disabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
      2007-11-14 19:23:27,593 INFO [org.hibernate.hql.ast.ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Query language substitutions: {}
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] JPA-QL strict compliance: enabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Second-level cache: enabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Query cache: disabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Cache provider: org.hibernate.cache.HashtableCacheProvider
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Optimize cache for minimal puts: disabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Structured second-level cache entries: disabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Statistics: disabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Deleted entity synthetic identifier rollback: disabled
      2007-11-14 19:23:27,593 INFO [org.hibernate.cfg.SettingsFactory] Default entity-mode: pojo
      2007-11-14 19:23:27,609 INFO [org.hibernate.impl.SessionFactoryImpl] building session factory
      2007-11-14 19:23:27,625 INFO [org.hibernate.impl.SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
      2007-11-14 19:23:27,625 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] Running hbm2ddl schema update
      2007-11-14 19:23:27,625 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] fetching database metadata
      2007-11-14 19:23:27,625 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] updating schema
      2007-11-14 19:23:27,656 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] table found: optixml2.dbo.FileToSend
      2007-11-14 19:23:27,656 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] columns: [typecommunication, sendrdf, nomfile, sendhourbegin, sujet, sendhourend, mqueuename, otherfile, attach, dest, id]
      2007-11-14 19:23:27,656 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] foreign keys: []
      2007-11-14 19:23:27,656 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] indexes: [pk__filetosend__1cbc4616]
      2007-11-14 19:23:27,656 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] schema update complete
      2007-11-14 19:23:27,656 INFO [org.hibernate.util.NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
      2007-11-14 19:23:27,656 INFO [org.jboss.ejb3.JmxKernelAbstraction] installing MBean: jboss.j2ee:jar=ejbsupplier.jar,name=FileToSendBean,service=EJB3 with dependencies:
      2007-11-14 19:23:27,656 INFO [org.jboss.ejb3.JmxKernelAbstraction] persistence.units:jar=ejbsupplier.jar,unitName=edieyessupplier
      2007-11-14 19:23:27,687 INFO [org.jboss.ejb3.EJBContainer] STARTED EJB: com.edieyes.supplier.ejb3.sessions.FileToSendBean ejbName: FileToSendBean
      2007-11-14 19:23:27,703 INFO [org.jboss.ejb3.cache.simple.SimpleStatefulCache] Initializing SimpleStatefulCache with maxSize: 100000 timeout: 300 for jboss.j2ee:jar=ejbsupplier.jar,name=FileToSendBean,service=EJB3
      2007-11-14 19:23:27,703 INFO [org.jboss.ejb3.EJB3Deployer] Deployed: file:/C:/jboss-4.0.5.GA/server/all/deploy/ejbsupplier.jar


        • 1. Re: second level cache
          Andrew Batman Newbie

          first of all, you have to read this 2 articles about performance in hibernate:
          http://www.hibernate.org/hib_docs/reference/en/html/performance.html
          Especially, chapter 19.2. The Second Level Cache.
          And http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html#d0e2196

          Then you should try to make your entity cachable by adding @Cache annotation with needed "usage" parameter .
          Also try to set up "cacheable" property of the query. (there are several ways to make it, one of them is to set up global hibernate property hibernate.cache.use_query_cache)

          • 2. Re: second level cache
            Loic Newbie

            Thanks a lot, i get it worked well.

            But i have another matter with cache.

            I use my findAll function well, and know i would like to get a subpart of the list putted in cache.

            So i created a new function

            public void findTest() {
             // TODO Auto-generated method stub
             em.createQuery("SELECT c FROM FileToSend c where name =?1").setParameter(1,"test").getResultList();
             }
             }



            When i use it, even if the resulted entities are already in cache, it connects to the database to get results...
            (For the second time call it doesn't... but i need it to get data from cache for the first call).


            Any idea on how to do it??

            I did test with HibernateSession createFilter function on my first loaded collection but i get an error :

            20:04:59,109 ERROR [STDERR] javax.ejb.EJBException: org.hibernate.QueryException
            : The collection was unreferenced


            Thanks a lot for helping.


            • 3. Re: second level cache
              Loic Newbie

              No idea on it? (to get a sublist on a list already in cache)

              It seems that it's due to the way to go of hibernate, that doesn't allow it, am i wrong?

              We did it on .Net wihtout any problem!... so i imagined that it was possible too on Jboss.