I have a Spring + Hibernate / Flex application that should dynamically switch between database schemas. For this, I implemented AbstractRoutingDataSource after this . Unfortunately this will not work. It actually executes SQL in the default schema (logical_public). Any help would be greatly appreciated. Thanks.
Here is my setup:
applicationContext.xml contains two data sources. Each data source connects to a database with a different login role. The routing data source selects the correct data source using a string key. The SchemaConstants class contains a pair of open static leaf fields.
<bean id="parentDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver"/>
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/mystore"/>
<property name="acquireIncrement" value="3"/>
<property name="minPoolSize" value="1"/>
<property name="maxPoolSize" value="15"/>
<property name="maxStatementsPerConnection" value="100"/>
<property name="automaticTestTable" value="c3p0_test_table"/>
<property name="numHelperThreads" value = "20"/>
</bean>
<bean id="publicDS" parent="parentDataSource">
<property name="user" value="postgres"/>
<property name="password" value="password"/>
</bean>
<bean id="tempSchemaDS" parent="parentDataSource">
<property name="user" value="temp_role"/>
<property name="password" value="tmppsw"/>
</bean>
<bean id="routingDS" class="flex.RoutingDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="flex.SchemaConstants.LOGICAL_PUBLIC" value-ref="publicDS"/>
<entry key="flex.SchemaConstants.TEMP_SCHEMA" value-ref="tempSchemaDS"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="publicDS"/>
</bean>
RoutingDataSource: .
public class RoutingDataSource extends AbstractRoutingDataSource
{
@Override
protected Object determineCurrentLookupKey()
{
return Globals.getSchema();
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException
{
return null;
}
}
Globals: .
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
public void test()
{
Globals.setSchema(SchemaConstants.TEMP_SCHEMA);
SomeDataOther someOtherData = new SomeDataOther();
someOtherData.setName("Jorjinio");
this.sessionFactory.getCurrentSession().save(someOtherData);
Globals.setSchema(SchemaConstants.LOGICAL_PUBLIC);
SomeData someData = new SomeData();
someData.setFirstName("Hulio");
someData.setLastName("Julio");
this.sessionFactory.getCurrentSession().save(someData);
}
. ? @Transactional, , . transactionManager org.springframework.orm.hibernate3.HibernateTransactionManager. - , - , .