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

  • #16
    Originally posted by Jörg Heinicke View Post
    And so on ... Just don't do this ...

    Well, Somehow I have to do this. One way or the other. Perhaps I can switch to use the AbstractRoutingDataSource
    Last edited by DJViking; Jun 20th, 2007, 03:50 AM.


    • #17
      I just found out that after makeing a change to the datasource the session factory did not use the new datasource, but the default one set up in the application context. It did not matter if I changed the datasource bean.

      BasicDataSource dataSource = (BasicDataSource) applicationContext.getBean("dataSource");
      private void setApplicationContext(){
      applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
      applicationContextIsSet = true;

      Then later in the code I access the methods that perform the database operations

      This is how I have arranged the context file:

          <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
              <property name="mappingResources" ref="mappingResources"/>
              <property name="hibernateProperties" ref="hibernateProperties"/>
              <property name="dataSource" ref="dataSource"/>
         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
              <property name="maxActive" value="0"/><!-- 0 means unlimited num of connections -->
              <property name="driverClassName" value="${datasource.driverClassName}"/>
              <property name="url" value="${datasource.url}"/>
              <property name="username" value="${datasource.username}"/>
              <property name="password" value="${datasource.password}"/>
          <bean id="someService" class="some.service.SomeServiceImpl">   
            <property name="hibernateTemplate" ref="hibernateTemplate"/>   
          <!-- HibernateTemplate -->
          <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
              <property name="sessionFactory" ref="hibernateSessionFactory"/>
      When I access the "someService" and it gets its data with hibernate, first through hibernateTemplate then hibernateSessionFactory which access the dataSource.
      But it does not seems like changeing the DataSource URL helps(I change it before I call getBean("someService") ).
      Last edited by DJViking; Jun 20th, 2007, 03:12 AM.


      • #18
        Originally posted by Jörg Heinicke View Post
        It should have a reference, not a copy. So it should have the updated values.
        How can that be? I have tried numerous way of changeing the URL of the data source object in the application context, but the session factory does not use a data source with the new URL. It seems like it uses the data source which was created when the application context was set.


        • #19
          Originally posted by DJViking View Post
          It seems like it uses the data source which was created when the application context was set.
          There are not two datasources - at least as far as Spring knows. You change the url property of THE datasource directly, so it is automcatically available in the LocalSessionFactoryBean as well.

          BUT there is a reason why it does not work the way you are doing it at the moment. (Did I mention you should not do it this way? Couldn't resist ... ) I had a look into the source of BasicDataSource. It's actually only a wrapper around the "real" datasource. Only setting the url does not change anything in it. It does not write through the actual datasource, but only sets the url property of BasicDataSource itself and restartNeeded to true. The latter one is never asked for. And createDataSource only creates a new instance if there is none. So always the same datasource is used. There is also following Javadoc comment in it:

           * Returns whether or not a restart is needed. 
           * Note: restart is not currently triggered by property changes.
          It also has an unused method restart() which is private unfortunately. But it only executes the one and only solution at the moment anyway: To reset the internal datasource to null so that it is not reused on next access to getConnection() is to call close() on BasicDataSource.