Is it possible to configure dynamic route datasource by application.properties and JdbcTemplate ? I can use this stuff to configure simple datasource.
Thanks in advance for your help
You can't use multiple datasources only by configuration in application.properties.
You have to define multiple Datasources as bean like described here
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources
and than define new bean of type AbstractRoutingDataSource.
Related
Is there any possible solution to dynamically create a datasource at runtime? I have come across AbstractRoutingDataSource e.g. here a few times however in my case I would not know the database name at the time of config beans starting up as we will be creating databases during client on-boarding and would prefer not to have to bounce the app, so I would want to select the correct datasource based on some client id in the request header, and add the new datasource to the spring context on the first request for this client.
Yes, like any other bean you can add DataSource bean in runtime.
There are several ways to do this: https://medium.com/#venkivenki4b6/spring-dynamically-register-beans-in-4-ways-at-run-time-c1dc45dcbeb9
TL;DR
When using MyBatis with Spring boot, is following step possible?
Create Datasource, SqlSessionFactory, SqlTransactionManager beans without mapperscan
Deploy 1 as library
import 2, create Mapper with beans declared in 1 with #Mapper annotation
Question
(First of all, apologize my poor english)
I'm creating mybatis datasource autoconfiguration library for my spring boot project.
Datasource will used at some applications, and they would have different mapper.
If mappers are already defined, creating DataSource, SqlSessionFactory and SqlTransactionManager beans with #MapperScan can bind Mapper and those beans.
But when case like this; DataSource, SqlSessionFactory, SqlTransactionManager beans declared,
and Mappers will created later, is possible binding specific beans with #Mapper annotation?
Those beans are default currently, so project would works.
But if another datasource added without mapper scan,
application initialization failed due to duplicate bean definition.
For some reasons, i prefer these done with mapper annotation(not mapper xml)
If any another advice for this design, looking forward to answer.
Thank you.
I'm currently working on implementing schema-based multi-tenancy for my Spring Boot / JdbcTemplate API application. I figured out that for it to work, in the DAO layer, I need to dynamically change the schema of the DataSource used by JDBCTemplate during the runtime, in other word create a new one, but I can't find any information on how to set properly the schema for the DataSource I'm creating.
EDIT
Here are some details that might be important : the schema is defined in the url of the API endpoints I created, as a mandatory variable: if the user calls the URL localhost:9090/schema/MyNewSchema/Test, the schema variable is MyNewSchema and I have to create a DataSource with the proper pointed schema MyNewSchema.
Found a possible answer if someone faces the same issue : https://springboot-vuejs-reactjs.blogspot.com/2019/08/springboot-multi-tenancy-with.html
I decided to use an AbstractDataSource class, as mentioned in the article, and to override the public DataSource dataSource() bean in the #SpringBootApplication class, with an injection of the AbstractDataSource class created earlier. This allows me to manipulate the DataSource used by JDBCTemplate dynamically during the application runtime.
Furthermore, in my AbstractDataSource class, the DataSource objects I manipulate are HikariDataSource, allowing me to define the schema I want the DataSource to point with a hikariDs.setConnectionInitSql("ALTER SESSION SET CURRENT_SCHEMA = MY_SCHEMA") statement (cf Configure OracleDataSource programmatically in Spring Boot with a default schema). In my case, I'm using Postgresql, so the SQL statement is SET SEARCH_PATH TO <schema-name>
It works pretty well !
My team is using latest spring boot for one of the project.
Spring provides database configuration in application.properties file and for password this is the key "spring.datasource.password"
Now, we want to change this to "spring.ds.pwd", but, the change should not required to create new Bean for datasource is it possible?
I followed below link, but, here new beans are creating to handle the session, which should not be expected. Other search information is also similar.
https://dzone.com/articles/spring-boot-jpa-mysql-sample-app-code-example
I was able to achieve what I want by overriding the DataSourceProperties java class with same package name.
I'm trying to use the application.properties file to configure the datasource Spring Boot will have to use.
I've put the following properties inside :
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.user=test
spring.datasource.password=test
spring.datasource.jdbcUrl=jdbc:postgresql://localhost:5432/test
The application.properties file is well used by other systems. But I can't get it to work for the automatic datasource configuration.
I'm still getting this Exception :
org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database url for database type NONE.
The postgresql driver is included and loaded. And I can configure the datasource using a Configuration class, and the same parameters as above.
I've also added the #EnableAutoConfiguration and #EnableJpaRepositories to my Application.class.
Any clues?
You should use spring.datasource.url to configure the JDBC URL rather than spring.datasource.jdbcUrl.
spring.datasource.jdbcUrl will work if the specific DataSource implementation that you're using has a setJdbcUrl method (HikariCP, for example) where as spring.datasource.url will work with any of the supported datasources.
Using spring.datasource.url also has the added benefit that you don't need to specify spring.datasource.driverClassName as it will be inferred from the url.