Skip to content

Toro Cloud Dev Center


JDBC connections

Database connections created in Martini are maintained in a connection pool so they can be reused for future requests. Each pool keeps the maximum amount of open connections it can hold depending on its properties. In some cases, you may encounter this limit being reached (and even exceeded) causing unavailability of connections at runtime.

When a database connection is used in your services, Martini tries its best to put the connection back in its pool after using it. However, under some circumstances you may also find these connections leaking.

Martini offers a way to monitor JDBC connections by recording their stacktrace upon creation to help you decide if it is a connection leak or whether you need to reconfigure the database pool's properties. To enable this feature, set the logger level of io.toro.martini.database to DEBUG. You can then use the getDatabaseConnections operation from the REST API to query the connections' metadata.

Here is a sample response from this operation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
  "totalResults": 1,
  "totalPages": 1,
  "page": 0,
  "limit": 20,
  "items": [
    {
      "name": "store-2010329540",
      "createdAt": "2020-05-22 09:02:48",
      "stackTrace": [
        "GetCustomers.InvokeGloop(GetCustomers.gloop:12)",
        "\tat io.toro.martini.database.BitronixConnectionRegistry.onAcquire(BitronixConnectionRegistry.java:53)",
        "\tat bitronix.tm.resource.jdbc.PoolingDataSource.fireOnAcquire(PoolingDataSource.java:225)",
        "\tat bitronix.tm.resource.jdbc.JdbcPooledConnection.<init>(JdbcPooledConnection.java:118)",
        "\tat bitronix.tm.resource.jdbc.PoolingDataSource.createPooledConnection(PoolingDataSource.java:341)",
        "\tat bitronix.tm.resource.common.XAPool.createPooledObject(XAPool.java:283)",
        "\tat bitronix.tm.resource.common.XAPool.grow(XAPool.java:391)",
        "\tat bitronix.tm.resource.common.XAPool.getInPool(XAPool.java:371)"
      ]
    }
  ]
}

This response shows that an active connection named store-2010329540 exists from the store database pool. It was created and opened specifically from the GetCustomers Gloop service at line 12.

Note that connections are only recorded after configuring the log level so previously created connections are not returned by the API. If you need to track them immediately, restart the database pool to reset the connections. Remember to set the log level back after troubleshooting since recording such information may incur a performance overhead.