Spring Boot LDAP - pooled property for auto configuration - java

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

Related

spring.session.timeout vs server.servlet.session.timeout

I'm looking to extend the user session in a Spring MVC app with Spring boot and Tomcat. Looking at the documentation there seems to be 2 relevant properties:
server.servlet.session.timeout
spring.session.timeout
Most examples out there seem to suggest to use server.servlet.session.timeout; what is the purpose of spring.session.timeout? Which one should be used to extend the user session?
spring.session.timeout is the property from a Spring sub-project called Spring Session. It will fallback to server.servlet.session.timeout if it is not set.
In short , Spring Session allows you to store HttpSession in RDBMS / Redis / Hazelcast Cluster / MongoDB rather than an internal map inside Tomcat .So the sessions is stored in the container agnostic way and make session clustering easier as you do not need to configure a Tomcat cluster.
So if you do not use Spring Session , you should use server.servlet.session.timeout
You should be able to set the server.session.timeout in your application.properties file to do the same
I think, If you add spring.session.timeout in application.properties the session timeout value mentioned using server.session.timeout will be overridden

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.

How to migrate spring DataSource tomcat to hikari properties?

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.

Is there possibility to Spring Config Server gets config from itself?

Is there possibility or workaround to tell Spring Config Server to get config from itself? I have some common configs for all Spring Boot apps depending on profile and I want config server has possibility to access them without copy-paste.
From the docs:
An optional property that can be useful in this case is spring.cloud.config.server.bootstrap which is a flag to indicate that the server should configure itself from its own remote repository.
So setting spring.cloud.config.server.bootstrap=true.

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

Categories

Resources