Spring Boot 1.2.1.RELEASE and JNDI Errors - java

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

Related

Read property from config server in spring boot 2.4

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

Default configuration of H2 in-memory database set up by Springboot

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=

Spring Cloud Config unable to get correct values

I am unable to get the value I am expecting, An exception is thrown at this line #Value("${message:this-is-class-value}").
SERVER SIDE
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
src/main/resources/application.properties
server.port=8888
spring.application.name=config-service
spring.cloud.config.server.git.uri=file:///C:/config
management.endpoints.web.exposure.include=*
spring.security.user.name=root
spring.security.user.password=abc123
Application class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
#SpringBootApplication
#EnableConfigServer
public class ConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}
}
Local git folder
Configurations files with the same property but the different value to detect
c:/config/application.properties
c:/config/api-gateway.properties
c:/config/api-gateway-PROD.properties
output while server startup
Completed initialization in 5 ms
WARN : Could not merge remote for master remote: null
INFO : o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/config/application.properties
if I access this url
http://localhost:8888/api-gateway/PROD
console output is as follows
WARN : Could not merge remote for master remote: null
INFO : o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/config/api-gateway-PROD.properties
INFO : o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/config/api-gateway.properties
INFO : o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/config/application.properties
CLIENT SIDE (Separate Project)
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
Application class
#SpringBootApplication
#EnableZuulProxy
#EnableDiscoveryClient
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
Controller
#RestController
public class SettingsController {
#Value("${message:this-is-class-value}")
String name = "World";
#RequestMapping("/")
public String home() {
return "Hello " + name;
}
}
resources/application.yml
server:
port: 8282
spring:
application:
name: api-gateway
eureka:
instance:
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
resources/bootstrap.yml
spring:
profiles:
active: PROD
cloud:
config:
name: api-gateway
uri: http://localhost:8888/
username: root
password: abc123
management:
endpoints:
web:
exposure:
include: refresh
Console output
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'settingsController': Injection of autowired dependencies failed; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.LinkedHashMap<?, ?>] to type [java.lang.String]
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.LinkedHashMap<?, ?>] to type [java.lang.String]
Please do let me know if anything else is required.
add message=Hello from property file1 in src/main/resources/application.properties. Or if you want to read this property from c:/config/application.properties or c:/config/config-service.properties you need to configure external configuration file by using #ConfigurationProperties annotation.
Looks, you are trying to use the #Value annotation on controller class which resides in your Cloud config server application. While the concept is like cloud config server application will be the provider to other applications and it can provide the properties from git or local file system (configuration) to requester client application. Other applications will connect to your config server application by providing it's URL, application name and profile for which it want to get the properties. You can check below URL for cloud config server and client application.
https://www.thetechnojournals.com/2019/10/spring-cloud-config.html
Your cloud config server has security configuration as below.
spring.security.user.name=root
spring.security.user.password=abc123
You client configuration doesn't provide any security configuration which is causing 401 error in your logs.
To solve this issue please do below changes to your client configuration.
Client configuration:
spring:
profiles:
active: PROD
cloud:
config:
name: api-gateway
uri: http://localhost:8888/
username: root
password: abc123

Heroku Postgres + Spring Boot config org.postgresql.util.PSQLException: ResultSet is closed

I would like to connect Heroku Postgres with Spring Boot App.
Properties:
spring:
datasource:
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://ec2-54-75-251-84.eu-west-1.compute.amazonaws.com:5432/ddjbpu7qcel282?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
username: my_username
password: my_password
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: update
Exception during application start:
Caused by: org.postgresql.util.PSQLException: This ResultSet is closed.
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkClosed(AbstractJdbc2ResultSet.java:2839) ~[postgresql-9.3-1100-jdbc41.jar:na]
What should i do to get my database connection working?
Thanks.
PostGres DB Properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:postgresql://<host>:5432/<db_name>?ssl=true&sslmode=require&user=<username><password>&sslfactory=org.postgresql.ssl.NonValidatingFactory
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driverClassName=org.postgresql.Driver
and following dependencies in POM.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
Nothing else is required for connecting heroku postgres with your local application.

test-on-borrow config in hikariCP

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?

Categories

Resources