[Ballerina] Database connection pool exhausted?

Manuri Amaya Perera
Ballerina Swan Lake Tech Blog
2 min readOct 27, 2018

--

You just started playing around with Ballerina, the new kid in town, and thought let’s see how good she is with databases. Somewhere in the middle you figured out that your connection pool gets exhausted. I’m going to list down a some reasons that could have caused this issue!

First, I’m going to assume you have, Connection pool size of the database itself and the ballerina database are set to meet the needs. This article is only about how you could have caused connection leaks due to the lack of best practices in the ballerina code itself.

First, the culprit is almost always a table returned by a select action or a call action! It can never be an update action.

  1. Do you not fully iterate the table?

2. Are you having a break statement in the middle of an iteration?

2. Are you returning from a function in the middle of an iteration?

These are possible scenarios that could have caused the connection pool exhaustion. Let me explain why.

When you obtain a table from a select/call operation, the complete result set will not be loaded to memory. The table just keeps a pointer to the database result set. Records will be retrieved on demand. This is because ballerina treats streaming behavior as a first class citizen. Therefore, connection will be closed when the table is fully iterated or “close” function is called on the table explicitly!

So, in each of the above mentioned scenarios, table is neither fully iterated or explicitly closed, causing a connection leak.

Now I guess its obvious to you how to fix this.

You have to either,

  1. Fully iterate the table

Or,

2. Close the table explicitly before you return in the middle of an iteration, break etc.

--

--

Manuri Amaya Perera
Ballerina Swan Lake Tech Blog

I am an Engineer at WSO2. Currently working in the Ballerina team. Mainly contributing to Ballerina data client area. My GitHub URL: https://github.com/manuri