I have an application that need to connect to few different schema's but everything of same type (ORACLE). The decision of which schema comes from UI.
if User selects schema1, then entity should persist in Schema1, if selects other, then it should be in the selected other schema.
Am using Spring boot + Hibernate with the dependency "spring-boot-starter-data-jpa"
I have created a datasource class like below so that i can change the "schemaName" in the datasource object everytime before invoking the data layer.
@Component
public class SchemaDatasource extends AbstractDataSource {
private String schemaName;
@Autowired
private DSManager dsm;
public void setSchemaName(String schemaName) {
this.schemaName = schemaName;
}
@Override
public Connection getConnection() throws SQLException {
if (schemaName!= null)
return dsm.getConnection(schemaName);
else
return null;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
if (schemaName!= null)
return dsm.getConnection(schemaName);
else
return null;
}
}
My problem is during the startup, the "HibernateJpaAutoConfiguration" tries creating sessionfactory.During creation it tries to check connection with the datasource But as schemaName is null in the startup, my SchemaDatasource returns null connection with which application bootstrap is failed.
is there a way to handle this. Am expecting similar to SessionFactory withnooptions in hibernate.
In case of RoutingDatasource also, i have to set defaultDatasource.
Spring boot version: 1.5.9.RELEASE
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…