You could always try and request an update to 2.0.1 or greater. However the second option would work for you.Originally Posted by DJViking
You could always try and request an update to 2.0.1 or greater. However the second option would work for you.Originally Posted by DJViking
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
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) ...How could I check the session factory bean to check which datasource it uses?
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.
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
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:
When I access the "someService" and it gets its data with hibernate, first through hibernateTemplate then hibernateSessionFactory which access the dataSource.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>
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.
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.
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:
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.Code:* Returns whether or not a restart is needed. * Note: restart is not currently triggered by property changes.
Jörg