Announcement Announcement Module
Collapse
No announcement yet.
Memory Problems with two session factories Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Memory Problems with two session factories

    Hi,
    I am creating two hibernate session factories using Spring. I get memory heap space when i start my application. I know its expensive to create a session factory but i need to create two as they point to two different datasources. Can i somehow load these hbms only once.I think thats what creating a problem(loading all hbms twice).
    I am using opensession in view filter

    Code:
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref local="dataSource" />
    </property>
    <property name="mappingDirectoryLocations">
    <list>
    <value>classpath:/com/cmt/acq/db</value>
    <value>classpath:/com/cmt/auth/db</value>
    <value>classpath:/com/cmt/pcm/db</value>
    <value>classpath:/com/cmt/vendor/db</value>
    <value>classpath:/com/cmt/db</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">com.cmt.dialect.CustomDial ect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.use_sql_comments">true</prop>
    <prop key="hibernate.max_fetch_depth">2</prop>
    <prop key="hibernate.autocommit">false</prop>
    <prop key="hibernate.current_session_context_class">thre ad</prop>
    <prop key="hibernate.generate_statistics">true</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate .cache.EhCacheProvider</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    <prop key="hibernate.jdbc.batch_size">20</prop>
    </props>
    </property>
    </bean>


    Code:
    <bean id="archivalSessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref local="archivalDataSource" />
    </property>
    <property name="mappingDirectoryLocations">

    <list>
    <value>classpath:/com/cmt/acq/db</value>
    <value>classpath:/com/cmt/auth/db</value>
    <value>classpath:/com/cmt/pcm/db</value>
    <value>classpath:/com/cmt/vendor/db</value>
    <value>classpath:/com/cmt/db</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">com.cmt.dialect.CustomDial ect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.use_sql_comments">true</prop>
    <prop key="hibernate.max_fetch_depth">2</prop>
    <prop key="hibernate.autocommit">false</prop>
    <prop key="hibernate.current_session_context_class">thre ad</prop>
    <prop key="hibernate.generate_statistics">true</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate .cache.EhCacheProvider</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    <prop key="hibernate.jdbc.batch_size">20</prop>
    </props>
    </property>
    </bean>
    Last edited by jforjava1981; Jul 27th, 2011, 08:45 AM.

  • #2
    Both session factories will load hbm's separately. You will have to increase the heap size by using -Xmx option while starting server. Open session in view filter is not the cause as it is not used when application is being initialized.
    Out of curiosity, why do you need 2 session factories pointing to the same database?

    Comment


    • #3
      Yes I did that. i have incresed it to 2g. my physical memory is 2GB. but still its not working. Alos if i remove second session factory configuration it works fine.
      I am not facing any problem when application initializes. It starts and initializes properly but breaks when i fire any request.
      I am using Mysql5.0. And i am pointing to two different schemas one is primary and one is backup.

      when I want to archive the data I will insert the records into archival schema otherwise the normal one.

      Also, when i am in archival mode i will fetch records from archival schema;otherwise from primary schema.

      Comment


      • #4
        Session factory by itself should not take up a lot of memory. I think the cause of OOM might be something else. It might be caused by the request. Does it crash with one particular request or crashes with any request? Are you using any second level cache?
        How much memory does the server take after startup, before serivicing the request. After startup connect to the JVM by using a tool like VisualVM & force a GC & then check the size of the JVM.
        You can also take a heap dump to see the type of objects that are there in the JVM when it throws OOM. You could set JVM option to create heap dump on OOM. This might give you some clues on what is taking up memory.

        Comment


        • #5
          I think every request. but i am not able to determine that as my first request itself is crashing. n as i said it doesn't happen when i comment out second declaration of sessionfactory.

          Yes i am using second level cache. as you can find out in my code

          <prop> key="hibernate.cache.provider_class">org.hibernate .cache.EhCacheProvider</prop>

          i am configuring it the same way for the session factories.

          Another observation I would like to note :

          Earlier i didn't have the second session factory declared through my Spring applicationcontext. But I had it in hibernate.cfg.xml.then this was not happening.
          In hibernate.cfg.xml I was declaring the datasource as a property and not as a JNDI object.

          Will follow your pointers.meanwhile if u find anything wrong in my config please let me know its urgent.

          Comment


          • #6
            the only thing that is different for you is the data source, you should take a look at org.springframework.jdbc.datasource.lookup.Abstrac tRoutingDataSource

            Comment


            • #7
              hey guys, thanks a lot for your help. it solved my problem when i updated my java jvm with latest update. i think they optimized the garbage collection mechanism in the JVM itself.

              Comment


              • #8
                Originally posted by jforjava1981 View Post
                I think every request. but i am not able to determine that as my first request itself is crashing. n as i said it doesn't happen when i comment out seo second declaration of sessionfactory.

                Yes i am using second level cache. as you can find out in my code

                <prop> key="hibernate.cache.provider_class">org.hibernate .cache.EhCacheProvider</prop>

                i am configuring it the same way for the session factories.

                Another observation I would like to note :

                Earlier i didn't have the second session factory declared through my Spring applicationcontext. But I had it in hibernate.cfg.xml.then this was not happening.
                In hibernate.cfg.xml I was declaring the datasource as a property and not as a JNDI object.

                Will follow your pointers.meanwhile if u find anything wrong in my config please let me know its urgent.
                hey guys, thanks a lot for your help. it solved my troubles

                Comment

                Working...
                X