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;
}







0















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?










share|improve this question

























  • where application.yml located? are you sure it was loaded? should be in src/main/java/resources

    – user7294900
    Jan 6 at 9:20


















0















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?










share|improve this question

























  • where application.yml located? are you sure it was loaded? should be in src/main/java/resources

    – user7294900
    Jan 6 at 9:20














0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 13:09







cppcoder

















asked Jan 3 at 12:10









cppcodercppcoder

15k53470




15k53470













  • 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

















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












1 Answer
1






active

oldest

votes


















0














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.







share|improve this answer


























  • 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











  • 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












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
});


}
});














draft saved

draft discarded


















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









0














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.







share|improve this answer


























  • 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











  • 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
















0














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.







share|improve this answer


























  • 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











  • 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














0












0








0







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.







share|improve this answer















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.








share|improve this answer














share|improve this answer



share|improve this answer








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 add service level before datasource 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











  • @cppcoder add service level before datasource 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory