Announcement Announcement Module
Collapse
No announcement yet.
Accessing the hibernateSessionFactory bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing the hibernateSessionFactory bean

    I am using Spring with Hibernate using multiple databases(datasources). The approach I have chosen is to set a default value for my datasource and then when the user switch I then read the dataSource bean which is set in my application context. I then change the URL in that datasource(an BasicDataSource object from org.apache.commons.dbcp) in order to use the new datasource

    But I am not sure if the session factory is using the new datasource or the default datasource which was created for the session factory when the application context was loaded.

    How could I check the session factory bean to check which datasource it uses?

    When I read the bean I do a:
    LocalSessionFactoryBean session = (LocalSessionFactoryBean) applicationContext.getBean("hibernateSessionFactor y");

    But checking this object I am not sure how to find out which datasource it uses? Would I find that in getConfiguration() or getHibernateProperties() ?

  • #2
    Which factory bean do you use to config hibernateSessionFactory? Can you post configuration for this bean?

    Comment


    • #3
      Originally posted by mpetrashev View Post
      Which factory bean do you use to config hibernateSessionFactory? Can you post configuration for this bean?
      <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean"
      init-method="afterPropertiesSet">

      <property name="mappingResources" ref="mappingResources"/>
      <property name="hibernateProperties" ref="hibernateProperties"/>
      <property name="dataSource" ref="dataSource"/>
      </bean>

      Comment


      • #4
        Code:
        LocalSessionFactoryBean factoryBean = (LocalSessionFactoryBean) applicationContext.getBean("&hibernateSessionFactory");
        Properties hibernateProperties = factoryBean.getHibernateProperties() ;
        (Don't forget about & in bean name)

        Comment


        • #5
          Originally posted by mpetrashev View Post
          Code:
          LocalSessionFactoryBean factoryBean = (LocalSessionFactoryBean) applicationContext.getBean("&hibernateSessionFactory");
          Properties hibernateProperties = factoryBean.getHibernateProperties() ;
          (Don't forget about & in bean name)
          What does the & mean ? I have never seen a use of it in Java
          When I access my DataSource bean I do not use a & and that one works fine.

          Comment


          • #6
            It's not java, it's Spring. When you call getBean method and configured FactoryBean for this bean name you will get instance(s) that this FactoryBean produce, not FactoryBean itself. So, when you call
            Code:
            applicationContext.getBean("hibernateSessionFactor y");
            you receive SessionFactory instance, not LocalSessionFactoryBean instance. But if you add & in front of bean name you will get original instance of bean that was configured in application context even if this is FactoryBean

            Comment


            • #7
              There are better solutions when needing multiple datasources with one HibernateSessionFactory. You might want to read this and this

              Comment


              • #8
                Originally posted by mdeinum View Post
                There are better solutions when needing multiple datasources with one HibernateSessionFactory. You might want to read this and this
                The first one wont work. I am using Spring 2.0 and I need 2.0.1 for that. And it is out of my controll as I get my libraries from Maven and I do not update it.

                Comment


                • #9
                  Originally posted by mpetrashev View Post
                  Code:
                  LocalSessionFactoryBean factoryBean = (LocalSessionFactoryBean) applicationContext.getBean("&hibernateSessionFactory");
                  Properties hibernateProperties = factoryBean.getHibernateProperties() ;
                  (Don't forget about & in bean name)
                  getHibernateProperties() does not give me any information about the dataSource it was instansiated with, only some properties like hibernate.dialect etc.

                  Comment


                  • #10
                    Originally posted by DJViking
                    The first one wont work. I am using Spring 2.0 and I need 2.0.1 for that. And it is out of my controll as I get my libraries from Maven and I do not update it.
                    You could always try and request an update to 2.0.1 or greater. However the second option would work for you.

                    Comment


                    • #11
                      Originally posted by DJViking View Post
                      I am using Spring 2.0 and I need 2.0.1 for that. And it is out of my controll as I get my libraries from Maven and I do not update it.
                      Is Spring 2.0 really the last Maven-distributed release?? Anyway that's really a reason not to upgrade? Very questionable policy IMO.

                      The other solution of manipulating LocalSessionFactoryBean is even worse. You should not change properties of a singleton bean in general! The same applies to the DataSource IMO.

                      The best is probable to copy the code for AbstractRoutingDataSource (or the concrete implementation that meets your needs) if you really can't upgrade Spring.

                      Jörg

                      Comment


                      • #12
                        Originally posted by DJViking View Post
                        getHibernateProperties() does not give me any information about the dataSource it was instansiated with, only some properties like hibernate.dialect etc.
                        What do you mean under dataSource? You can call getDataSource() method or repeat the same magic with "&dataSource" bean. But anyway, can you clarify what do you mean under:

                        How could I check the session factory bean to check which datasource it uses?
                        Bean instance? Url? JNDI name? DataSource property has abstract type (interface) ...

                        Comment


                        • #13
                          Originally posted by Jörg Heinicke View Post
                          Is Spring 2.0 really the last Maven-distributed release?? Anyway that's really a reason not to upgrade? Very questionable policy IMO.

                          The other solution of manipulating LocalSessionFactoryBean is even worse. You should not change properties of a singleton bean in general! The same applies to the DataSource IMO.

                          The best is probable to copy the code for AbstractRoutingDataSource (or the concrete implementation that meets your needs) if you really can't upgrade Spring.

                          Jörg
                          Well as it is the repository I use is on an internal server which I do not maintain. I could probally issue a request for the person in charge to download and make avaiable the latest Spring release.

                          Comment


                          • #14
                            Originally posted by mpetrashev View Post
                            What do you mean under dataSource? You can call getDataSource() method or repeat the same magic with "&dataSource" bean. But anyway, can you clarify what do you mean under:



                            Bean instance? Url? JNDI name? DataSource property has abstract type (interface) ...
                            I do read the datasource bean in my current code from the application context, but I want to know if the session factory uses the new updated datasource or the old that was instansiated with it when the application context was set.

                            The application context gets its properties for the datasource bean from a properties file. In this properties file I have 8 URL for different databases, but same table structure. When the user choose a new datasource I update the datasource bean I retrieve from the application context.

                            When I look up in the database does the session factory look up in the datasource object from the application context or does it have a local copy of it which was instansiated when the application context created its bean objects ??
                            Last edited by DJViking; Jun 19th, 2007, 01:22 PM.

                            Comment


                            • #15
                              Originally posted by DJViking View Post
                              When I look up in the database does the session factory look up in the datasource object from the application context or does it have a local copy of it which was instansiated when the application context created its bean objects ??
                              It should have a reference, not a copy. So it should have the updated values. BUT you get into concurrency issues with this approach. What happens when a connection is tried to open while you are reconfiguring the datasource? I wonder how you want to preclude this. And even if for any reason the concurrent access can't happen you add a invisible global state to your application. What happens if a database access fails and you have to find out why? Which datasource configuration was actually in use? And so on ... Just don't do this ...

                              Jörg

                              Comment

                              Working...
                              X