chequer-io / JDBC.NET

It is a wrapper that allows you to use JDBC drivers in ADO.NET
MIT License
34 stars 10 forks source link

connection pooling? #31

Open funkysandman opened 1 year ago

funkysandman commented 1 year ago

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

tcables commented 1 year ago

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