I have spring boot 2.0.2.RELEASE project and I want configure datasources with pool connection.
I use Spring data JPA + hibernate + HikariCP:
#DataBase properties
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=***
spring.datasource.username=*****
spring.datasource.password=*****
# Hikari
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
#jpa config
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.data.jpa.repositories.enabled=true
In #DataBase properties - I think everything is fine
In #Hikari - I do not know how implement this:
in tomcat I can write spring.datasource.tomcat.test-on-borrow=true but how can I make like this with Hikari ?
And mayby I forgote some important configs?
Related
In my Java Spring Boot project I try to migrate User entity into mysql database.
When I mvn spring-boot:run, I see following error:
HHH90000026: MySQL57Dialect has been deprecated; use org.hibernate.dialect.MySQLDialect instead
Here's my application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?createDatabaseIfNotExist=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database=mysql
Here's the dependency for mysql:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
What am I missing?
With the help of Raushan Kumar I solved the connection problem with:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
I have below implementation currently working in spring boot 2.3. But when I migrate the same to spring boot 2.4. properties not reading from config server.
Code
#ConditionalOnProperty({"app.xzy.hosts"})
public class clientConfig {
// implementation
}
bootstrap.yml
spring:
cloud:
config:
uri: http://main-config-server.com
username: user
password: pass
fail-fast: true
application.yml
app:
xyz:
hosts: ${app.main.config.hosts}
application.yml in config server
app:
main:
config
hosts: http://myhost.com
This implementation is working fine in spring boot 2.3. But after the upgrade to spring boot 2.4 this giving below error in the startup.
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'app.xzy.hosts' in value "${app.main.config.hosts}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
Finally figured out the issue.
bootstrap.yml file is no longer enabled by default. You need enable it by adding new dependency in spring cloud 2020.0.0 and it solved my issue.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Reference: https://spring.io/blog/2020/10/07/spring-cloud-2020-0-0-m4-aka-ilford-is-available
I am using H2 database with Spring Boot (version 2.3.3.RELEASE) with all default settings for H2 database.
Here are the all files of my application.
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
application.properties
spring.h2.console.enabled=true
Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.sql.SQLException;
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
After starting the application when i am trying to connect the H2 database (configured and started by springboot with all default configuration) using below credentials,
I am getting error saying
Database "mem:testDB" not found, either pre-create it or allow remote
database creation
How can I connect to H2 database configured and started by Spring Boot with all the default credentials.
I don't want to override any configuration in application.properties files except spring.h2.console.enabled=true.
In newer version of Spring Boot (2.2+), look for the following log message on your console: Use the JDBC URL to connect on /h2-console page:
Spring Boot 2.2+:
INFO H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
Spring Boto 2.3+:
INFO H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:621dd224-01db-4137-807f-b9c3046de64d'
Only enabling console wouldn't be sufficient, you also need to mention which db you want connect. In your case if you want connect in memory db add below properties as well, then try to connect your in memory db using same creds
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
I have configured it like this.why do i report the above error?
spring.jpa.show-sql = true
logging.level.org.springframework.data=DEBUG
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.namingstrategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MYSQL5Dialect
You have a typo in your configuration
The correct one should be
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
I receive an error when I deploy my web application, using IntelliJ 14.0, to a local Apache Tomcat instance. I am using Spring Boot 1.2.1.RELEASE and with a JNDI connection.
The contents of my application.yaml file looks like this:
spring:
profiles:
active: production
---
spring:
profiles: development
datasource:
platform: h2
---
spring:
profiles: production
datasource:
jndi-name: java:/comp/env/jdbc/teams
platform: mysql
jpa:
hibernate:
ddl-auto: create-drop
My configuration file, Application.java, looks like this:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I am using Apache Tomcat 8.0.15 and have configured a JNDI connection in the context.xml file.
<Resource name="jdbc/teams" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="***" password="***" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/teams"/>
I am using IntelliJ 14.0 to deploy my application locally to Apache Tomcat using the production profile.
The error I receive when when I attempt to deploy the application is as follows:
org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [org.apache.tomcat.dbcp.dbcp2.BasicDataSource#4f83492a] with key 'dataSource'; nested exception is javax.management.InstanceAlreadyExistsException: Catalina:type=DataSource,host=localhost,context=/project,class=javax.sql.DataSource,name="jdbc/teams"
What could be wrong with the way the application is configured?
I modified the application.yaml file and the application is now working. I added spring.jmx.enabled and spring.jpa.database-platform properties to the application.yaml file.
spring:
profiles:
active: production
spring:
profiles: development
datasource:
platform: h2
spring:
profiles: production
datasource:
jndi-name: java:/comp/env/jdbc/teams
platform: mysql
jpa:
hibernate:
ddl-auto: create-drop
database-platform: org.hibernate.dialect.MySQL5Dialect
jmx:
enabled: false