Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Accessing the hibernateSessionFactory bean

  1. #11
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Quote 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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  2. #12
    Join Date
    Sep 2004
    Location
    London, UK
    Posts
    64

    Default

    Quote 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) ...

  3. #13
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote 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.

  4. #14
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote 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 at 01:22 PM.

  5. #15
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote 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

  6. #16
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote Originally Posted by Jörg Heinicke View Post
    And so on ... Just don't do this ...

    Jörg
    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 at 03:50 AM.

  7. #17
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    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.

    Code:
    if(!applicationContextIsSet)
    setApplicationContext();
     
    BasicDataSource dataSource = (BasicDataSource) applicationContext.getBean("dataSource");
    if(!dataSource.getUrl().equals(URL))
    dataSource.setUrl(URL);
     
    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:

    Code:
     
        <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
            init-method="afterPropertiesSet">
     
            <property name="mappingResources" ref="mappingResources"/>
            <property name="hibernateProperties" ref="hibernateProperties"/>
            <property name="dataSource" ref="dataSource"/>
         </bean>
     
       <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>
     
        <bean id="someService" class="some.service.SomeServiceImpl">   
          <property name="hibernateTemplate" ref="hibernateTemplate"/>   
        </bean>   
     
        <!-- HibernateTemplate -->
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
            <property name="sessionFactory" ref="hibernateSessionFactory"/>
        </bean>
    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 at 03:12 AM.

  8. #18
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote Originally Posted by Jörg Heinicke View Post
    It should have a reference, not a copy. So it should have the updated values.
    Jörg
    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.

  9. #19
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote 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:

    Code:
     * 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.

    Jörg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •