Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JdbcTemplate - Add support for multitenancy [SPR-17342] #21876

Closed
spring-projects-issues opened this issue Oct 4, 2018 · 7 comments
Closed

JdbcTemplate - Add support for multitenancy [SPR-17342] #21876

spring-projects-issues opened this issue Oct 4, 2018 · 7 comments
Labels
in: data type: enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 4, 2018

robert-niestroj opened SPR-17342 and commented

Please add support for multitenancy in JdbcTemplate.

Scenario: i have an app which uses JPA for CRUD, but for reporting uses native queries executed via JdbcTemplate. Hibernate in our app is working in multitenancy mode with the strategy per schema on MySQL. I tried several things but could not implement similar multitenancy in JdbcTemplate.

Basiclly i do connection.createStatement("USE " + tenantSchema ); in hibernates multitenancyConnectionProvider.

 

I would like to do something similar in JdbcTemplate.


Reference URL: https://stackoverflow.com/questions/52541380/spring-jdbctemplate-how-to-prepend-every-query

@spring-projects-issues spring-projects-issues added in: data type: enhancement labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.x Backlog milestone Jan 11, 2019
@gokhanoner
Copy link

@gokhanoner gokhanoner commented Jul 24, 2019

any update on this one?

One thing to mention, JDBC connection has setCatalog method, which can be used to change schema/database, since each DB driver interpret it differently based on to their model, MySQL call SET <schema>, PostgreSQL changes DB etc. This will be more generic solution. Like Hibernate, if Spring has a way of defining ConnectionProvider and TenantResolver for JdbcTemplate, this should work. I assume some changes needed internally as well, since Spring wrap original connection for Transaction tracking etc.

@brankoiliccc
Copy link

@brankoiliccc brankoiliccc commented Aug 30, 2019

So this is just ridiculous. Basically JdbcTemplate is hard coded to be single tenant. Because of this we do not even use it, but there is a catch ! Spring team uses it so we have to write ton of our own implementations already provided by spring to not use JdbcTemplate. geez

@ashwinbks83
Copy link

@ashwinbks83 ashwinbks83 commented Jan 29, 2020

I did something like this.

@Component
@Scope(scopeName="prototype")
public class MultiTenantJdbcTemplate extends JdbcTemplate {

	
	public MultiTenantJdbcTemplate() {
		
	}
	
	@Override
	public void afterPropertiesSet() {
	}
	
	@Override
	public DataSource getDataSource() {
		System.out.println("getDataSource");
		if(super.getDataSource() != DatabaseContextHolder.getCurrentTenantDataSource()) {
			this.setDataSource(DatabaseContextHolder.getCurrentTenantDataSource());
		}
		// TODO Auto-generated method stub
		return super.getDataSource();
	}
	
}

Autowired this in the dao class. Any thoughts ?

@mp911de
Copy link
Member

@mp911de mp911de commented Jan 31, 2020

Any reason you're not using AbstractRoutingDataSource which is built to resolve a DataSource during runtime?

@rstoyanchev rstoyanchev added the status: waiting-for-feedback label Jan 31, 2020
@ashwinbks83
Copy link

@ashwinbks83 ashwinbks83 commented Feb 2, 2020

@mp911de It was my bad, i made a mistake in the dao implementation and ended up writing all the code that comes with extending JdbcDaoSupport.

@spring-projects-issues spring-projects-issues added status: feedback-provided and removed status: waiting-for-feedback labels Feb 2, 2020
@rstoyanchev rstoyanchev added status: waiting-for-feedback and removed status: feedback-provided labels Feb 3, 2020
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 10, 2020

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder label Feb 10, 2020
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 17, 2020

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback status: feedback-reminder labels Feb 17, 2020
@snicoll snicoll removed this from the 5.x Backlog milestone Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data type: enhancement
Projects
None yet
Development

No branches or pull requests

7 participants