JDBC.NET icon indicating copy to clipboard operation
JDBC.NET copied to clipboard

connection pooling?

Open funkysandman opened this issue 2 years ago • 1 comments

Any advice on how to achieve connection pooling with a jdbc.net connection? I'm using this for a web api and looking to reduce overhead of opening connections

funkysandman avatar Jul 19 '23 15:07 funkysandman

Use the Factory Pattern to provide connections that you pool. Wrap the JdbcConnections in a class that can hold a reference to the pool, that which the pool will recycle it when you finish using it in a using statement (disposable).

In the factory keep a list of wrapped-connections private JdbcConnectionWrapper[] Connections = new JdbcConnectionWrapper[POOL_SIZE];. and a list of locks private int[] Locks = new int[POOL_SIZE];. locks[i] is 0 for free or 1 for in use. when requesting a connection loop over connections pool and check for unused connections. Interlocked.CompareExchange(ref Locks[i], 1, 0) == 0 is your friend (atomic / locking). I'd post all of my code, but I'd need approval from my boss.

also, take a moment to see my pull request for making JDBC.NET web api compatible: https://github.com/chequer-io/JDBC.NET/pull/29

tcables avatar Jul 19 '23 19:07 tcables