我有一个Spring + Hibernate / Flex应用程序需要在数据库模式之间动态切换。为了实现这一点,我在this文章之后实现了一个AbstractRoutingDataSource。不幸的是,在dataSource之间进行更改是行不通的。 有人可以帮帮我吗?
我点了这个链接:Spring + Hibernate SessionFactory + AbstractRoutingDataSource
我的代码是: - ApplicationContext:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<bean id="dataSource2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url2}</value>
</property>
<property name="username">
<value>${jdbc.username2}</value>
</property>
<property name="password">
<value>${jdbc.password2}</value>
</property>
</bean>
<bean id="routingDS" class="br.com.cpb.gtf.infra.RoutingDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="br.com.cpb.gtf.infra.SchemaConstants.TESTE" value-ref="dataSource2"/>
<entry key="br.com.cpb.gtf.infra.SchemaConstants.PRODUCAO" value-ref="dataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource2"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="br.com.cpb.*" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.query.substitutions">true '1', false '0'" </prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
-RoutingDataSource:
package br.com.cpb.gtf.infra;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class RoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey()
{
return Globals.getSchema();
}
}
-SchemaConstants
package br.com.cpb.gtf.infra;
public class SchemaConstants {
public static final String PRODUCAO = "dataSource";
public static final String TESTE = "dataSource2";
}
-Globals:
package br.com.cpb.gtf.infra;
public class Globals {
private static final ThreadLocal<String> schemaHolder = new ThreadLocal<String>();
public static void setSchema(String schema) {
schemaHolder.set(schema);
}
public static String getSchema() {
return schemaHolder.get();
}
public static void clearCustomerType() {
schemaHolder.remove();
}
}
- 服务:
@RemotingInclude
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public Adm_Usuario logar(Adm_Usuario user) {
Globals.clearCustomerType();
Globals.setSchema(SchemaConstants.TESTE);
Adm_UsuarioDao dao = new Adm_UsuarioDao(sessionFactory);
Adm_Usuario admUsuario = dao.logar(user);
setLoggedUserOnSession(admUsuario);
return admUsuario;
}
答案 0 :(得分:1)
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
在属性dataSource中,ref应该是routingDS:
<property name="dataSource" ref="routingDS" />