How to replace default hikari cp to tomcat pool on spring boot 2.0 - spring-boot

I have migrated spring boot application to 2.0 and found out some problems with hikari connection pool. When I am fetching database data this results to hikari cp timeout ie. connection is not available. I don't know why when in the previous version this worked correctly.
Therefore I tried to use tomcat pool with this config in application.yml but it did not work (in correct YAML formatting).
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
My pom.xml has these dependencies related to DB things:
spring-boot-jpa
spring-boot-jdbc
jdbc7
How to exclude hikari and use tomcat connection pool?

I have found out the solution.
This can be resolved in pom.xml by modifying like that:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
However the hikari problem was probably with default small size of connection pool. So this problem could be resolved also with this change but not verified by myself. Just note for others. Something like that:
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=5

Since Spring Boot 2.0 release, spring-boot-starter-jdbc and spring-boot-starter-data-jpa resolve HikariCP dependency by default and spring.datasource.type property has HikariDataSource as default value.So if u have both dependency in your application you should exclude it from both like below.
implementation('org.springframework.boot:spring-boot-starter-data-jpa') {
exclude group: 'com.zaxxer', module: 'HikariCP'
}
implementation('org.springframework.boot:spring-boot-starter-jdbc') {
exclude group: 'com.zaxxer', module: 'HikariCP'
}
After that you can configure other pooling technologies that u likes to use, like below
.
In your application.yml file :
spring:
datasource:
type: org.apache.tomcat.jdbc.pool.DataSource
In dependency :
implementation('org.apache.tomcat:tomcat-jdbc')

Also:
spring:
datasource:
type: org.apache.tomcat.jdbc.pool.DataSource
works with
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>

Related

How to resolve NoSuchMethodError in spring TransactionSynchronizationManager.currentTransaction when using pring-data-r2dbc

I am writing service based on Spring webflux, which reads data from PostgreSQL using r2dbc. I need to use latest release of r2dbc, however I am getting NoSuchMethodError exception in using TransactionSynchronizationManager spring-tx 5.2.0.RELEASE library.
I basically need to know what is correct spring-tx library version to be compatible with version of spring-data-r2dbc which works correctly with latest r2dbc-postgresql and r2dbc-spi libraries.
Here are my Maven dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>1.0.0.M2</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>0.8.0.RC2</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-spi</artifactId>
<version>0.8.0.M8</version>
</dependency>
I am using interface extending ReactiveCrudRepository interface to retrieve table data per below.
#Query("...")
Flux<QuoteHistory> findAllBySecIdAndDateTimeBetweenAndUpdateTypeIn(LocalDate date, Long secId);
I was able to get this code to work with earlier versions of r2dbc-postgresql and r2dbc-spi but now I am getting following exception.
java.lang.NoSuchMethodError: org.springframework.transaction.reactive.TransactionSynchronizationManager.currentTransaction()Lreactor/core/publisher/Mono;
at org.springframework.data.r2dbc.connectionfactory.ConnectionFactoryUtils.doGetConnection(ConnectionFactoryUtils.java:88) ~[spring-data-r2dbc-1.0.0.M2.jar:1.0.0.M2]
at org.springframework.data.r2dbc.connectionfactory.ConnectionFactoryUtils.getConnection(ConnectionFactoryUtils.java:70) ~[spring-data-r2dbc-1.0.0.M2.jar:1.0.0.M2]
at org.springframework.data.r2dbc.core.DefaultDatabaseClient.getConnection(DefaultDatabaseClient.java:189) ~[spring-data-r2dbc-1.0.0.M2.jar:1.0.0.M2]
These are r2dbc dependencies which code works with.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>1.0.0.M1</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>1.0.0.M7</version>
</dependency>
Please use the following dependency combination:
R2DBC Postgres: 0.8.0.RC2
R2DBC SPI: 0.8.0.RC2
Spring Data R2DBC: 1.0.0.RC1
Spring Framework: 5.2.0.RELEASE
Alternatively, go to https://start.spring.io to get a dependency-managed project with versions that work together.

Tracing SQL Queries with X-Ray and Spring Boot 2

The current X-Ray SQL tracing interceptor uses Tomcat JDBC Pool but Spring Boot 2 uses HikariCP as default pool, is it possible to configure the jdbc tracing in HikariCP instead?
Here (https://forums.aws.amazon.com/thread.jspa?threadID=254847) they suggest to use both Datasources:
DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
HikariDataSource hikariDataSource = new HikariDataSource();
... // data source configuration
dataSource.setJdbcInterceptors("com.amazonaws.xray.sql.postgres.TracingInterceptor;");
hikariDataSource.setDataSource(dataSource);
But if I have the HikariCP library in the classpath spring will configure that as datasource.
I've tried with a DatasourceBuilder and also forcing the type using the parameter spring.datasource.type
Any hint?
In Spring boot , you can use still use Tomcat over HikariCP as connection pool:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
We are currently investigating a solution that will work with both Tomcat JDBC and HikariCP. We are aware that there are currently no work arounds without having Tomcat JDBC as a dependency. Please stay tuned.
Resolved the same using TracingDataSource, while still using HikariCP as the connection pool
Reference https://github.com/aws/aws-xray-sdk-java/issues/88#issuecomment-570328275
Code: (Note, I am using AWS Secrets Manager JDBC Library aws-secretsmanager-jdbc to connect to the database using secrets stored in AWS Secrets Manager)
import com.amazonaws.xray.sql.TracingDataSource;
...
...
#Bean
#ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return TracingDataSource
.decorate(DataSourceBuilder.create()
.driverClassName("com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver")
.url("jdbc-secretsmanager:postgresql://" + System.getenv("PGHOST") + ":"
+ System.getenv("PGPORT") + "/" + System.getenv("PGDATABASE"))
.username(System.getenv("SECRET_NAME")).build());
}
Dependency:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-sql</artifactId>
</dependency>

Correct the classpath of your application so that it contains a single, compatible version of org.elasticsearch.common.logging.Loggers

I'm getting following error while running my Spring boot app, I'm new to Spring boot and elastic search, please help to solve this issue.And attached my pom dependencies below.
Thanks in advance,
*************************** APPLICATION FAILED TO START ***************************
Description:
An attempt was made to call the method org.elasticsearch.common.logging.Loggers.getLogger(Ljava/lang/String;)Lorg/apache/logging/log4j/Logger; but it does not exist. Its class, org.elasticsearch.common.logging.Loggers, is available from the following locations:
jar:file:/C:/Users/Sudhakar/.m2/repository/org/elasticsearch/elasticsearch/6.6.2/elasticsearch-6.6.2.jar!/org/elasticsearch/common/logging/Loggers.class
It was loaded from the following location:
file:/C:/Users/Sudhakar/.m2/repository/org/elasticsearch/elasticsearch/6.6.2/elasticsearch-6.6.2.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.elasticsearch.common.logging.Loggers
Process finished with exit code 1
Maven dependencies:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.6.2</version>
</dependency>
Spring Boot uses Elasticsearch 6.4 by default. By using 6.6.2 as the versions for two Elasticsearch modules you will have ended up with a mixture of the two versions. You should remove the <version> configuration in your pom. If you are able to use Spring Boot’s default version there’s nothing more to do. If you need to use 6.6.2 you should add an entry in your pom’s <properties>:
<elasticsearch.version>6.6.2</elasticsearch.version>

using embedded amqp swith spring boot and camel

I want to integration test a custom camel component and therefor need an embedded/in memory messanging I can easily use to test from/to endpoints.
I am hoping that I can achieve this via spring-boot-amqp-starter.
I used this example for a start, which has the dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
and the config:
spring:
activemq:
broker-url: vm://embedded?broker.persistent=false,useShutdownHook=false
// ...
This is working, when I use regular Listener annotations in spring, I have a sender using the template and a consumer logging the messages.
Now I go one step further and use camel, but it does not recognize the vm:embedded broker but tries to connect to tcp://localhost, which is not running.
return new RouteBuilder() {
#Override
public void configure() throws Exception {
from("activemq:foo").to("log:sample");
from("timer:bar").setBody(constant("Hello from Camel")).to("activemq:foo");
}
};
How can I configure activemq-camel to use the embedded broker?
Update:
I use dependency management imports for spring-boot-dependencies (1.5.9)
and camel-spring-boot-dependencies (2.20.1).
This has been fixed in newer versions of activemq-camel when you use that with Spring Boot. Now the activemq-camel component will honor the spring boot configuration of the spring.activemq.* settings.

Spring boot and apache spark - container conflict

I am trying to use spring boot 1.1.5 and apache spark 1.0.2 together in project. Look like apache spark uses Jetty container internally and I have configured spring-boot to use Tomcat container. However application startup fails with some securityException at root cause. If I see full stack trace looks like spring boot trying to initialize "jettyEmbeddedServletContainerFactory" which it shouldn't in first place. It probably picks it up from classpath due to jetty presence via spark. If I exclude jetty from spark and run again I don't see same error again but then SparkContext initialization fails due to not finding jetty. How do i tell spring-boot runtime to look for "TomcatEmbeddedServletContainerFactory" instead of jetty one?
I got "java.lang.SecurityException: class "javax.servlet.http.HttpSessionIdListener"'s signer information does not match signer information of other classes in the same package"
To fix this issue I was need to remove all javax.servlet dependencies.
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>1.3.1</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty.orbit</groupId>
<artifactId>javax.servlet</artifactId>
</exclusion>
</exclusions>
</dependency>
#Joakim Erdfelt, Thanks.
I was just waiting to see if someone is familiar with this situation and if it's just a small configuration change. As it turns out it is! #Configuration
#EnableAutoConfiguration(exclude={EmbeddedServletContainerFactory.class})
public class MyConfiguration { }
I defined my own "EmbeddedServletContainerFactory" bean as a "org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContaine‌​rFactory" and it started working as I expected.

Resources