Navid Galt
Navid Galt

Reputation: 31

Overriding timeout for database connection in properties file

I was wondering if there is a specific way to override database connection timeout in the properties file in my Java web project? I am using Hibernate, Spring, and MySQL DB. I have tried several different property fields and reduced the timeout time to 1 millsecond, yet the connection is still completed with transactions still being processed properly.

These are the property fields I have used to no avail...

Is hibernate overriding this timeout value or am I just setting it improperly? Thanks in advance!

Upvotes: 3

Views: 9255

Answers (1)

Denis Zavedeev
Denis Zavedeev

Reputation: 8297

Assuming that you're using Spring Boot you can try:

spring.transaction.defaultTimeout=1

This property sets defaultTimeout for transactions to 1 second.

(Looking at the source code of TransactionDefinition it seems that it is not possible to use anything more precise than seconds.)

See also: TransactionProperties


javax.persistence.query.timeout

This is a hint for Query. It is supposed to work if you use it like this:

entityManager.createQuery("select e from SampleEntity e")
    .setHint(QueryHints.SPEC_HINT_TIMEOUT, 1)
    .getResultList();

See also QueryHints


spring.jdbc.template.query-timeout

Remember that according to the JdbcTemplate#setQueryTimeout javadoc:

Any timeout specified here will be overridden by the remaining transaction timeout when executing within a transaction that has a timeout specified at the transaction level.


hibernate.c3p0.timeout

I suspect that this property specifies timeout for getting from the connection pool, not for a query execution

Upvotes: 3

Related Questions