9

Before I get started, I'd like to say I've checked the following and they didn't help me:

Essentially, I'm getting a HikariCP stracktrace and I don't know what's causing it.

java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms.
at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:548)
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:186)
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:145)
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:83)
at de.arraying.Arraybot.managers.ManagerSync.addCustomCommand(ManagerSync.java:192)
at de.arraying.Arraybot.commands.CommandsCommand.onCommand(CommandsCommand.java:100)
at de.arraying.Arraybot.commands.Command.execute(Command.java:72)
at de.arraying.Arraybot.listeners.ListenerChat.onGuildMessageReceived(ListenerChat.java:68)
at net.dv8tion.jda.core.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:299)
at net.dv8tion.jda.core.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:64)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleDefaultMessage(MessageCreateHandler.java:97)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:47)
at net.dv8tion.jda.core.handle.SocketHandler.handle(SocketHandler.java:38)
at net.dv8tion.jda.core.requests.WebSocketClient.handleEvent(WebSocketClient.java:688)
at net.dv8tion.jda.core.requests.WebSocketClient.onTextMessage(WebSocketClient.java:437)
at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:262)
at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:240)
at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:965)
at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:748)
at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:110)
at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:66)

I've tried changing the maximum pool size, minimum idle and I've also enabled the leak detection (at 2s). None of these helped, except that I am getting a leak detection every time I execute a query, so maybe it's related to this.

This is my current configuration:

    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setJdbcUrl("jdbc:mysql://"+url+":3306/"+database+"?useSSL=false");
    hikariConfig.setUsername(username);
    hikariConfig.setPassword(password);
    hikariConfig.setMaximumPoolSize(10);
    hikariConfig.setMinimumIdle(3);
    hikariConfig.setLeakDetectionThreshold(2000);
    dataSource = new HikariDataSource(hikariConfig);

My query methods are structured as following:

        // inside a try/catch, after some checks that aren't related.
        PreparedStatement preparedStatement =
                dataSource.getConnection().prepareStatement(query);
        preparedStatement.setString(2, id);
        preparedStatement.setString(3, name);
        preparedStatement.setObject(1, value);
        preparedStatement.executeUpdate();
        preparedStatement.close();

Am I supposed to close the connection after this or something? The only thing I can imagine that might cause an error are memory leaks, and I don't think I have any. My CPU usage is fine, too, and so is my internet connection. The queries all work perfectly fine, except for the fact that that just start throwing this error after a few times.

Community
  • 1
  • 1
Arraying
  • 169
  • 2
  • 3
  • 10
  • Since it says `request timed out after 30000ms.` i would think it's a network related issue, or your db server has no more connections? – Redlab Feb 02 '17 at 15:37
  • @Redlab how would I confirm that it has no more connections? – Arraying Feb 02 '17 at 15:37

1 Answers1

12

"I am getting a leak detection every time I execute a query".

Of course you are. In your example, you get a Connection out of the DataSource, execute a PreparedStatement, close the PreparedStatement then don't close the Connection, so it's not returned to the pool and results in a leak.

Close your connections people! Only you can prevent fores...err, connection leaks.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Okay, thanks for letting me know, I assumed the CP handled opening and closing connections for me. I guess that's why you shouldn't assume... Gonna update my code now and see if it chanes anything... – Arraying Feb 02 '17 at 15:44
  • 7
    When you call `close()` the connection isn't really closed, it's returned to the pool (since `close()` is overridden by some `HikariConnection` class which implements some pooling logic). – Kayaman Feb 02 '17 at 15:45
  • I've executed 20+ queries, none of them have given me an error or a memory leak warning, hopefully closing the connection has fixed this. – Arraying Feb 02 '17 at 15:52
  • Oh I can guarantee it. Doesn't mean there aren't other problems though. My guarantee only applies to unclosed connections. – Kayaman Feb 02 '17 at 15:53