Is there any way to timeout slow queries on application server - java

I am using MySQL DB and spring boot 2.0.0 deployed on tomcat server, is there any way I can timeout slow queries from my application server.

In hikari Connection Pool there is a property - maxLifetimeMs which you can manipulate in two different ways:
Adding values in property file :
spring:
hikari:
max-lifetime: 840000
Changing in the config :
final HikariDataSource dataSource;
config.setMaxLifetime(840000);
This will have set the maximum time taken out by a query. No query can run beyond the duration set by you.
For identification of slow query you need to integrate any APM tool with your application server

Related

How to modify the number of milliseconds (30000 ms by default) that a Spring Boot application takes to connect to a MongoDB database?

My understanding is that if my MongoDB database is not running, my Spring Boot application will take 30000ms by default to try to connect to the database.
How can I modify the value of 30000 ms? I tried putting the following in the application.properties but it doesn't work:
spring.data.mongodb.option.server-selection-timeout=1000

Spring JDBC Hikari constantly logon logoffs in Oracle Database

I am using spring boot hikari db pool to connect to an Oracle database (19C).
Our properties are as below:
spring.datasource.app.hikari.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.app.hikari.minimumIdle=5
spring.datasource.app.hikari.maximumPoolSize=10
spring.datasource.app.hikari.idleTimeout=600000
spring.datasource.app.hikari.connectionTimeout=180000
spring.datasource.app.hikari.max-lifetime=60000
Our Database's AUD table is filling up due to constant logon logoff actions getting registered from the services every second.
I used solution from below question (to decrease minimumIdle and increase idleTimeout for long idle times), but it did not resolve the issue.
spring jdbc hikari connection pool - constantly logs on and off to database
Can somebody help in this case. Thanks in advance.
If you have not override datasource with different property name for hikaricp then use below properties
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.connection-timeout=180000
spring.datasource.hikari.max-lifetime=60000

Testing Spring boot validation Query for Oracle spring.datasource.validationQuery=SELECT 1 from dual

I have a spring boot application in production. The spring boot version is 1.3.5. The connection to database is getting closed since no requests are being hit for a large amount of time. I have decided to use below properties to keep the connection alive
**spring.datasource.validationQuery=SELECT 1 from dual
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=30000**
Ideally the query need to be run for every 30 seconds. I would like to test whether the query is being working properly. I am unable to understand how to test these properties. Can anyone let me know is there any way to test this. I have added below properties in properties file but still query is not getting printed in console.
**spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=INFO
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
logging.level.org.springframework.jdbc.core.StatementCreatorUtils=TRACE**

Prevent Spring Boot startup failure on couchbase connection error

Using a combination of Spring Boot 1.4.1.Release and Spring Data Couchbase 2.1.3.RELEASE, is there a way to prevent application startup failure if it cannot connect to couchbase?
Current behavior: Application cannot connect to couchbase on startup and exits.
The goal: Application would continue to start even though it cannot connect to Couchbase, use the timeout configurations in CouchbaseEnvironment, and continually try to connect in the background.
In Spring Boot, we can configure Spring JDBC to ignore failure on startup with spring.datasource.continue-on-error=true. Any chance something like that coupled with a retry on failure exists in Spring Data Couchbase?
You can configure CouchbaseEnvironment by overriding getCouchbaseEnvironment using JavaConfig and try increasing the connect timeout. Is there a specific connection failure you are running into?
There isn't continue-on-error property for spring-data-couchbase. This property is available on Spring Boot for relational JDBC and it is useful to ignore initialize failures such as failing to create tables and loading into data sources using scripts.

How do I get Spring Boot to automatically reconnect to PostgreSQL?

I am running Spring Boot connecting to a PostgreSQL database. I have verified that data is written to the database if Spring Boot is started after the database.
spring.datasource.url = jdbc:postgresql://localhost/dbname
spring.datasource.username = user
spring.datasource.password = secret
spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
When there are changes in the database in development, I run into exceptions:
PreparedStatementCallback; SQL []; This connection has been closed.; nested exception is org.postgresql.util.PSQLException: This connection has been closed.
Note that the database is accessible again when the exceptions occur; I think the problem is that connection is stale because the database endpoint it originally connected to is no longer available because of the restart. Restarting Spring Boot resolves the issue.
How do I configure Spring Boot to reconnect to PostgreSQL so that I do not need to restart it?
I have attempted to follow the answers in Spring Boot JPA - configuring auto reconnect and How to reconnect database if the connection closed in spring jpa?. I am not sure whether the PostgreSQL behavior is different from MySQL. My reading of the Spring Boot documentation has not helped; I do not know the components described well enough to understand what documentation I should be looking at.
Spring boot should be configured to reconnect automatically, problem is that it is not aware of the broken connection.
Since you are already using test on borrow and validation query, just try reducing validation interval so it is executed every time.
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validation-query=SELECT 1
spring.datasource.tomcat.validation-interval=0
Tomcat jdbc connection pool on testOnBorrow:
(boolean) The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another. NOTE - for a true value to have any effect, the validationQuery or validatorClassName parameter must be set to a non-null string. In order to have a more efficient validation, see validationInterval. Default value is false
But be aware of validationInterval:
(long) avoid excess validation, only run validation at most at this frequency - time in milliseconds. If a connection is due for validation, but has been validated previously within this interval, it will not be validated again. The default value is 30000 (30 seconds).

Categories