

Spring boot provides straightforward ways to create datasource beans – either using properties configuration or using java configuration. To autowire another non-primary datasource, use annotation. While autowiring the datasource, spring boot will prefer the primary datasource i.e., “mySqlDataSource”. class JpaConfig = "h2DataSource")ĭataSourceBuilder.url("jdbc:mysql://localhost/testdb") In this case, we are responsible for providing configurations for all datasource beans. To configure multiple data sources, create as many bean definitions as you want but mark one of the DataSource instances as that if we create our own DataSource, the auto-configuration backs off. It is also possible to fine-tune implementation-specific settings by using their respective prefix ( .*, .*, and 2.*).įor example, we can use the below properties to customize a DBCP2 connection pool. Spring-boot-starter-data-jpa starter automatically get a dependency to HikariCP.
#STS FETCHING LOCAL CLOUD SERVICES DRIVER#
If we set -class-name property then the mentioned driver class must be found and loaded. HikariCP, Tomcat Pooling and Commons DBCP2įor a pooling datasource to be created, Spring boot verifies that a valid Driver class is available. In that case, we might want to configure and manage the DataSource by using the Application Server’s built-in features and access it by using JNDI. Suppose we deploy our Spring Boot application to an application server. class JpaConfig DataSource getDataSource()ĭataSourceBuilder dataSourceBuilder = DataSourceBuilder.create() ĭataSourceBuilder.driverClassName("org.h2.Driver") ĭataSourceBuilder.url("jdbc:h2:file:C:/temp/test") Please configure other beans as necessary. The recommended way to create a DataSource bean is using DataSourceBuilder class within a class annotated with the annotation. =jdbc:sqlserver://localhost databaseName=springbootdb We often do not need to specify the driver-class-name, since Spring Boot can deduce it for the most databases from the connection url. This way, we can import the datasource configurations from even configuration provider systems.īelow given configuration shows sample properties for H2, MySQL, Oracle and SQL Server databases. The properties configuration decouples the configuration from the application code. ĭataSource configuration is provided by configuration properties entries ( spring.datasource.* ) in application.properties file. If we plan to use an embedded database at some step (e.g., testing), we can import H2 DB separately. mysql-connector-java for connecting to MySQL database.
#STS FETCHING LOCAL CLOUD SERVICES DRIVERS#
This dependency brings all necessary dependencies including JDBC drivers for various databases e.g. If not already defined, include spring-boot-starter-data-jpa to project. Spring boot allows defining datasource configuration in two ways:ĭuring application startup, the DataSourceAutoConfiguration checks for DataSource.class (or EmbeddedDatabaseType.class) on the classpath and a few other things before configuring a DataSource bean for us.

HikariCP, Tomcat Pooling and Commons DBCP2
