klogger
Jul 31st, 2008, 08:28 AM
I have two datasources defined in my spring-config file:
<bean id="dataSourceProsoc" class="org.springframework.jdbc.datasource.DriverManagerD ataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/prosoc</value>
</property>
<property name="username">
<value>prosoc</value>
</property>
<property name="password">
<value>prosoc-</value>
</property>
</bean>
<!--
Datasource for prosoc_forum database
-->
<bean id="dataSourceProsocForum" class="org.springframework.jdbc.datasource.DriverManagerD ataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/prosoc_forum</value>
</property>
<property name="username">
<value>prosoc</value>
</property>
<property name="password">
<value>prosoc-</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFac toryBean">
<property name="dataSource" ref="dataSourceProsoc"/>
<property name="mappingResources">
<list>
<value>uk/co/prodia/prosoc/user/User.hbm.xml</value>
<value>uk/co/prodia/prosoc/forum/mvnforum/MVNForumMember.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransa ctionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="user" class="uk.co.prodia.prosoc.persistence.hibernate.DAOUserI mpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
and in my class I would like to swap from the first dataSource to the second one. Currently I am trying:
DriverManagerDataSource driverManagerDataSource = (DriverManagerDataSource) Config.getSpringBeans().getBean("sessionFactory");
but that seems to return a class of type $Proxy4 and so throws a ClassCastException.
How can I get my sessionFactory so that I can call the setDataSource(.) method on it?
<bean id="dataSourceProsoc" class="org.springframework.jdbc.datasource.DriverManagerD ataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/prosoc</value>
</property>
<property name="username">
<value>prosoc</value>
</property>
<property name="password">
<value>prosoc-</value>
</property>
</bean>
<!--
Datasource for prosoc_forum database
-->
<bean id="dataSourceProsocForum" class="org.springframework.jdbc.datasource.DriverManagerD ataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/prosoc_forum</value>
</property>
<property name="username">
<value>prosoc</value>
</property>
<property name="password">
<value>prosoc-</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFac toryBean">
<property name="dataSource" ref="dataSourceProsoc"/>
<property name="mappingResources">
<list>
<value>uk/co/prodia/prosoc/user/User.hbm.xml</value>
<value>uk/co/prodia/prosoc/forum/mvnforum/MVNForumMember.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransa ctionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="user" class="uk.co.prodia.prosoc.persistence.hibernate.DAOUserI mpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
and in my class I would like to swap from the first dataSource to the second one. Currently I am trying:
DriverManagerDataSource driverManagerDataSource = (DriverManagerDataSource) Config.getSpringBeans().getBean("sessionFactory");
but that seems to return a class of type $Proxy4 and so throws a ClassCastException.
How can I get my sessionFactory so that I can call the setDataSource(.) method on it?