Created
May 17, 2022 22:06
-
-
Save Marcelo-Rodrigues/57cb5dc1ed761f3a326a3820feacbae7 to your computer and use it in GitHub Desktop.
Connection pool - Limit timeout example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.studylibrary | |
import com.zaxxer.hikari.HikariConfig | |
import com.zaxxer.hikari.HikariDataSource | |
import java.time.Instant.now | |
fun main() { | |
val dataSource = HikariDataSource(HikariConfig().apply { | |
jdbcUrl = "jdbc:postgresql://localhost:5432/postgres" | |
username = "postgres" | |
password = "test_postgres" | |
}) | |
// Executa o getConnection() do HikariCP 10 vezes, consumindo todas as conexões | |
repeat(times = 10) { dataSource.connection } | |
printlnWithTime("Aguardando conexão") | |
try { | |
// A conexão ficará parada na linha abaixo aguardando uma conexão disponível no pool, | |
// pois o limite padrão é de 10 conexões simultâneas, que já foram atingidas acima. | |
// Após 30s é lançada uma Exception com a descrição: | |
// Exception in thread "main" java.sql.SQLTransientConnectionException: | |
// HikariPool-1 - Connection is not available, request timed out after 30000ms. | |
dataSource.connection | |
} finally { | |
printlnWithTime("Fim da execução") | |
} | |
} | |
private fun printlnWithTime(text: String) = println("[${now()}] $text") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment