Spring boot datasource implementation
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am implementing a library using Spring boot framework. The library offers custom starters for Kafka, Datasource etc for the consumers. They should use the library to develop microservices.
I am facing issues in implementing the datasource autoconfiguration.
Requirements
The library must implement a datasource autoconfiguration which should provide the developers to use upto 2 different datasources configured in application.yml
. This is what I have done so far.
library/
|__autoconfigure/
|__datasource/
|__DataSourceAutoConfiguration.java
|__PrimaryDataSourceConfiguration.java
|__SecondaryDataSourceConfiguration.java
|__datasource/
|__CustomRepository.java
|
|__datasource-spring-boot-starter/
Code
DataSourceAutoConfiguration.java
@Configuration
@ConditionalOnBean({DataSource.class})
@ConditionalOnClass({JpaRepository.class})
public class DataSourceAutoConfiguration {
}
PrimaryDataSourceConfiguration.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
)
public class PrimaryDataSourceConfiguration {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "service.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
application.yml
service:
datasource:
hikari:
jdbcUrl: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
When the service starts, I am getting the following error:
Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:1063) ~[HikariCP-2.7.9.jar:?]
What am I missing here?
spring-boot datasource hikaricp
add a comment |
I am implementing a library using Spring boot framework. The library offers custom starters for Kafka, Datasource etc for the consumers. They should use the library to develop microservices.
I am facing issues in implementing the datasource autoconfiguration.
Requirements
The library must implement a datasource autoconfiguration which should provide the developers to use upto 2 different datasources configured in application.yml
. This is what I have done so far.
library/
|__autoconfigure/
|__datasource/
|__DataSourceAutoConfiguration.java
|__PrimaryDataSourceConfiguration.java
|__SecondaryDataSourceConfiguration.java
|__datasource/
|__CustomRepository.java
|
|__datasource-spring-boot-starter/
Code
DataSourceAutoConfiguration.java
@Configuration
@ConditionalOnBean({DataSource.class})
@ConditionalOnClass({JpaRepository.class})
public class DataSourceAutoConfiguration {
}
PrimaryDataSourceConfiguration.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
)
public class PrimaryDataSourceConfiguration {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "service.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
application.yml
service:
datasource:
hikari:
jdbcUrl: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
When the service starts, I am getting the following error:
Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:1063) ~[HikariCP-2.7.9.jar:?]
What am I missing here?
spring-boot datasource hikaricp
whereapplication.yml
located? are you sure it was loaded? should be insrc/main/java/resources
– user7294900
Jan 6 at 9:20
add a comment |
I am implementing a library using Spring boot framework. The library offers custom starters for Kafka, Datasource etc for the consumers. They should use the library to develop microservices.
I am facing issues in implementing the datasource autoconfiguration.
Requirements
The library must implement a datasource autoconfiguration which should provide the developers to use upto 2 different datasources configured in application.yml
. This is what I have done so far.
library/
|__autoconfigure/
|__datasource/
|__DataSourceAutoConfiguration.java
|__PrimaryDataSourceConfiguration.java
|__SecondaryDataSourceConfiguration.java
|__datasource/
|__CustomRepository.java
|
|__datasource-spring-boot-starter/
Code
DataSourceAutoConfiguration.java
@Configuration
@ConditionalOnBean({DataSource.class})
@ConditionalOnClass({JpaRepository.class})
public class DataSourceAutoConfiguration {
}
PrimaryDataSourceConfiguration.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
)
public class PrimaryDataSourceConfiguration {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "service.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
application.yml
service:
datasource:
hikari:
jdbcUrl: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
When the service starts, I am getting the following error:
Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:1063) ~[HikariCP-2.7.9.jar:?]
What am I missing here?
spring-boot datasource hikaricp
I am implementing a library using Spring boot framework. The library offers custom starters for Kafka, Datasource etc for the consumers. They should use the library to develop microservices.
I am facing issues in implementing the datasource autoconfiguration.
Requirements
The library must implement a datasource autoconfiguration which should provide the developers to use upto 2 different datasources configured in application.yml
. This is what I have done so far.
library/
|__autoconfigure/
|__datasource/
|__DataSourceAutoConfiguration.java
|__PrimaryDataSourceConfiguration.java
|__SecondaryDataSourceConfiguration.java
|__datasource/
|__CustomRepository.java
|
|__datasource-spring-boot-starter/
Code
DataSourceAutoConfiguration.java
@Configuration
@ConditionalOnBean({DataSource.class})
@ConditionalOnClass({JpaRepository.class})
public class DataSourceAutoConfiguration {
}
PrimaryDataSourceConfiguration.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
)
public class PrimaryDataSourceConfiguration {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "service.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
application.yml
service:
datasource:
hikari:
jdbcUrl: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
When the service starts, I am getting the following error:
Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:1063) ~[HikariCP-2.7.9.jar:?]
What am I missing here?
spring-boot datasource hikaricp
spring-boot datasource hikaricp
edited Jan 3 at 13:09
cppcoder
asked Jan 3 at 12:10
cppcodercppcoder
15k53470
15k53470
whereapplication.yml
located? are you sure it was loaded? should be insrc/main/java/resources
– user7294900
Jan 6 at 9:20
add a comment |
whereapplication.yml
located? are you sure it was loaded? should be insrc/main/java/resources
– user7294900
Jan 6 at 9:20
where
application.yml
located? are you sure it was loaded? should be in src/main/java/resources
– user7294900
Jan 6 at 9:20
where
application.yml
located? are you sure it was loaded? should be in src/main/java/resources
– user7294900
Jan 6 at 9:20
add a comment |
1 Answer
1
active
oldest
votes
You should use jdbcUrl property (especially for Spring Boot and MySQL):
service:
datasource:
hikari:
jdbcUrl: jdbc:mysql://localhost:3306/test
Note: Spring Boot auto-configuration users, you need to use jdbcUrl-based configuration.
The MySQL DataSource is known to be broken with respect to network timeout support. Use jdbcUrl configuration instead.
I get the same error even after using this property
– cppcoder
Jan 3 at 12:34
@cppcoder addservice
level beforedatasource
in yaml
– user7294900
Jan 3 at 12:42
It does not help
– cppcoder
Jan 3 at 12:47
Try adding url in level:spring.service.datasource.url
– user7294900
Jan 3 at 15:45
Tried that with no luck. Looks like problem is not with the properties.
– cppcoder
Jan 3 at 15:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022035%2fspring-boot-datasource-implementation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should use jdbcUrl property (especially for Spring Boot and MySQL):
service:
datasource:
hikari:
jdbcUrl: jdbc:mysql://localhost:3306/test
Note: Spring Boot auto-configuration users, you need to use jdbcUrl-based configuration.
The MySQL DataSource is known to be broken with respect to network timeout support. Use jdbcUrl configuration instead.
I get the same error even after using this property
– cppcoder
Jan 3 at 12:34
@cppcoder addservice
level beforedatasource
in yaml
– user7294900
Jan 3 at 12:42
It does not help
– cppcoder
Jan 3 at 12:47
Try adding url in level:spring.service.datasource.url
– user7294900
Jan 3 at 15:45
Tried that with no luck. Looks like problem is not with the properties.
– cppcoder
Jan 3 at 15:55
add a comment |
You should use jdbcUrl property (especially for Spring Boot and MySQL):
service:
datasource:
hikari:
jdbcUrl: jdbc:mysql://localhost:3306/test
Note: Spring Boot auto-configuration users, you need to use jdbcUrl-based configuration.
The MySQL DataSource is known to be broken with respect to network timeout support. Use jdbcUrl configuration instead.
I get the same error even after using this property
– cppcoder
Jan 3 at 12:34
@cppcoder addservice
level beforedatasource
in yaml
– user7294900
Jan 3 at 12:42
It does not help
– cppcoder
Jan 3 at 12:47
Try adding url in level:spring.service.datasource.url
– user7294900
Jan 3 at 15:45
Tried that with no luck. Looks like problem is not with the properties.
– cppcoder
Jan 3 at 15:55
add a comment |
You should use jdbcUrl property (especially for Spring Boot and MySQL):
service:
datasource:
hikari:
jdbcUrl: jdbc:mysql://localhost:3306/test
Note: Spring Boot auto-configuration users, you need to use jdbcUrl-based configuration.
The MySQL DataSource is known to be broken with respect to network timeout support. Use jdbcUrl configuration instead.
You should use jdbcUrl property (especially for Spring Boot and MySQL):
service:
datasource:
hikari:
jdbcUrl: jdbc:mysql://localhost:3306/test
Note: Spring Boot auto-configuration users, you need to use jdbcUrl-based configuration.
The MySQL DataSource is known to be broken with respect to network timeout support. Use jdbcUrl configuration instead.
edited Jan 3 at 12:40
answered Jan 3 at 12:21


user7294900user7294900
24.3k123666
24.3k123666
I get the same error even after using this property
– cppcoder
Jan 3 at 12:34
@cppcoder addservice
level beforedatasource
in yaml
– user7294900
Jan 3 at 12:42
It does not help
– cppcoder
Jan 3 at 12:47
Try adding url in level:spring.service.datasource.url
– user7294900
Jan 3 at 15:45
Tried that with no luck. Looks like problem is not with the properties.
– cppcoder
Jan 3 at 15:55
add a comment |
I get the same error even after using this property
– cppcoder
Jan 3 at 12:34
@cppcoder addservice
level beforedatasource
in yaml
– user7294900
Jan 3 at 12:42
It does not help
– cppcoder
Jan 3 at 12:47
Try adding url in level:spring.service.datasource.url
– user7294900
Jan 3 at 15:45
Tried that with no luck. Looks like problem is not with the properties.
– cppcoder
Jan 3 at 15:55
I get the same error even after using this property
– cppcoder
Jan 3 at 12:34
I get the same error even after using this property
– cppcoder
Jan 3 at 12:34
@cppcoder add
service
level before datasource
in yaml– user7294900
Jan 3 at 12:42
@cppcoder add
service
level before datasource
in yaml– user7294900
Jan 3 at 12:42
It does not help
– cppcoder
Jan 3 at 12:47
It does not help
– cppcoder
Jan 3 at 12:47
Try adding url in level:spring.service.datasource.url
– user7294900
Jan 3 at 15:45
Try adding url in level:spring.service.datasource.url
– user7294900
Jan 3 at 15:45
Tried that with no luck. Looks like problem is not with the properties.
– cppcoder
Jan 3 at 15:55
Tried that with no luck. Looks like problem is not with the properties.
– cppcoder
Jan 3 at 15:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022035%2fspring-boot-datasource-implementation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
where
application.yml
located? are you sure it was loaded? should be insrc/main/java/resources
– user7294900
Jan 6 at 9:20