How to migrate spring DataSource tomcat to hikari properties? - java

I have the following tomcat connection pool configuration from spring-boot-1.x. As of spring 2 the default is hikaricp. How can I migrate the following properties to the hikari pendant? I could not find them:
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.initial-size=1
spring.datasource.tomcat.max-idle
spring.datasource.tomcat.initial-size
spring.datasource.tomcat.max-age
spring.datasource.tomcat.test-while-idle
spring.datasource.tomcat.test-on-connect
spring.datasource.tomcat.test-on-return
spring.datasource.tomcat.time-between-eviction-runs-mills
spring.datasource.hikari.*?

You can find all possible properties for HikariCP here.
Although some of the properties you define, are nonexistant in Hikari because of a completely different implementation. Some of them are not even needed anymore.

Related

Spring Boot Hikari configuration

I'm configuring HikariCP for Spring Boot application, the database is Postgresql.
The documentation says:
"We recommended using dataSourceClassName instead of jdbcUrl, but either is acceptable."
However, the next line says:
"Note: Spring Boot auto-configuration users, you need to use jdbcUrl-based configuration."
If we use jdbcUrl-based configuration and specify dataSourceClassName then jdbcUrl will be ignored, if we don't specify data source - HikariDataSource will be created. So they recommend using HikariDataSource for Spring Boot apps.
If we use dataSourceClassName - it will be created with given properties (in my case it is PGSimpleDataSource with its ancestor BaseDataSource).
Both these configurations work for me.
So, my questions are:
What is the difference between HikariDataSource and PGSimpleDataSource (or any other recommended)?
Why it is recommended to use jdbcUrl-based configuration (and thus HikariDataSource) in Spring Boot?
HikariCP is a connection pool, and a very good one. We've been using it in several projects in production and it's fast and just works.
If you want to use HikariCP you use HikariDataSource. Spring Boot has started to use it as a default and recommends it (for the same reasons: it's fast and solid).
If you just use the default configuration with spring.datasource.url, it will use HikariCP and should work out-of-the-box.
However, when you manually configure your datasource(s), there is a small issue with Spring Boot 2 and HikariCP. HikariCP expects jdbcUrl or dataSourceClassName, but the Spring Boot configuration property uses url.
See the documentation or this question for that.

Spring Boot LDAP - pooled property for auto configuration

In Spring Boot I can pass properties which will be picked for auto configuration:
spring.ldap.url
spring.ldap.username
spring.ldap.password
Is there any spring.ldap.* property to set pooled=true to avoid using explicit LdapTemplate config?
LdapContextSource contextSource = new LdapContextSource();
contextSource.setPooled(true);
According to the Appendix that lists all of the Spring Boot properties, no. I also looked at the LdapProperties class which stores these values at runtime and didn't see anything in there that would help with pooling. I suspect you'll have to keep doing this manually.
Perhaps file a PR on them? They seem open to adding things if there is a need in the community.
You can set ldap pool properties with JVM parameters. You specify them when launching your application.
For example:
-Dcom.sun.jndi.ldap.connect.pool.maxsize=10
-Dcom.sun.jndi.ldap.connect.pool.prefsize=5
-Dcom.sun.jndi.ldap.connect.pool.timeout=300000
Everything that I have read on this recommends that configuration be done using the PoolingContextSource. The LDAP authentication process requires a two-stage process that is problematic. The below is from the blog of Mattias Hellborg Arthursson, a Spring LDAP guru.
Built-in JNDI Connection Pooling
The pooled property of ContextSource has previously defaulted to true,
enabling the built-in Java LDAP connection pooling by default. However
the built-in LDAP connection pooling suffers from several deficiencies
(most notable there is no way of doing connection validation and
configuration is cumbersome), which is why we decided to change the
default to false . If you require connection pooling we strongly
recommend using the Spring LDAP PoolingContextSource instead.
https://blog.jayway.com/2008/10/27/whats-new-in-spring-ldap-13/
https://docs.spring.io/spring-ldap/docs/1.3.2.RELEASE/reference/html/pooling.html

Use Tomcat JDBC Pool as Hibernate Connection pool

In stand alone application which is build on top of hibernate I like to use Tomcat JDBC Connection Pool.
My guess is that i need to add some configs in hibernate.cfg.xml.
Can someone guide me how to configure hibernate to use "Tomcat JDBC Connection Pool" as connection pool.
In your hibernate.cfg.xml configuration file, you need to set the following property:
hibernate.connection.datasource=java:/comp/env/jdbc/TestDB
assuming this is the JNDI name of your Tomcat CP DataSource.

Dynamically datasource with hibernate and netbeans

How I can create web application project which is loading the jndi datasource set in the server configuration? I want to make the web application independent from the server and the database. Is it possible.
You create the DataSource in your web/application server and expose it through JNDI
You configure the following hibernate property:
<property name="hibernate.connection.datasource">java:comp/env/jdbc/MyDataSource</p>
This way the application is decoupled from the JNDI provider. Even the JNDI url can be configured using a Maven property, so switching to an application server (that uses a different JNDI resource pattern) is just a mater of adding a new Maven profile.
One better approach is to set up the DataSource in your application configuration.
#Autowired
private DataSource dataSource;
...
properties.put("hibernate.connection.datasource", dataSource);
This way you can use any connection pooling framework, like the fastest connection pooling framework, HikariCP.
You can even set up DataSource proxies, like:
datasource-proxy, to log SQL queries with PreparedStatement parameters
FlexyPool, to monitor connection pooling usage and adjust the pool size using adapting startegies

EclipseLink connection pooling configuration

I am currently implementing the data-layer for an EE application. I am using latest version of JPA, EclipseLink and Spring. The servlet container is VMware vFabric tc (Tomcat).
As the title mentioned, how should I do the connection pooling part ?
Most tutorials are using Hibernate with an external library like DBCP or C3PO, but EclipseLink tutorials use Springs DriverManagerDataSource that has no connection pooling.
Does EclipseLink have a production level connection pooling feature integrated ?
Do I need to set my datasource with JNDI to use Tomcat's connection pooling ?
Can someone provide guidelines or an example configuration for a similar scenario, please ?
Thank you.

Categories

Resources