0

I have a Model class which is part of my self-crafted ORM. It has all kind of methods like save(), create() and so on. Now, the thing is that all these methods require a connection object to act properly. And I have no clue on what's the best approach to feed a Model object with a connection object.

What I though of so far:

  • provide a connection object in a Model's __init__(); this will work, by setting an instance variable and use it throughout the methods, but it will kind of break the API; users shouldn't always feed a connection object when they create a Model object;
  • create the connection object separately, store it somewhere (where?) and on Model's __init__() get the connection from where it has been stored and put it in an instance variable (this is what I thought to be the best approach, but have no idea of the best spot to store that connection object);
  • create a connection pool which will be fed with the connection object, then on Model's __init__() fetch the connection from the connection pool (how do I know which connection to fetch from the pool?).

If there are any other approached, please do tell. Also, I would like to know which is the proper way to this.

1 Answer 1

1

Here's how I would do:

  • Use a connection pool with a queue interface. You don't have to choose a connection object, you just pick the next on the line. This can be done whenever you need transaction, and put back afterwards.

  • Unless you have some very specific needs, I would use a Singleton class for the database connection. No need to pass parameters on the constructor every time.

  • For testing, you just put a mocked database connection on the Singleton class.

Edit:

About the connection pool questions (I could be wrong here, but it would be my first try):

  • Keep all connections open. Pop when you need, put when you don't need it anymore, just like a regular queue. This queue could be exposed from the Singleton.

  • You start with a fixed, default number of connections (like 20). You could override the pop method, so when the queue is empty you block (wait for another to free if the program is multi-threaded) or create a new connection on the fly.

  • Destroying connections is more subtle. You need to keep track of how many connections the program is using, and how likely it is you have too many connections. Take care, because destroying a connection that will be needed later slows the program down. In the end, it's a n heuristic problem that changes the performance characteristics.

Sign up to request clarification or add additional context in comments.

7 Comments

So I pop a connection from the pool when I need to do something. Then, how/when should I put it back? Also, connections in the pool should be already opened when they are popped or they should be opened after popping? When do I have to close one connection? Sorry for the multitude of questions, but connection pooling isn't quite familiar to me.
Editing the question to add those answers.
It sounds way too complicated for my requirements. I expect to open a connection before using my models, do the necessary operations on them that require a connection, then close the connection. I think I need some hints on where to store the connection object rather how to deal with it in my application...hence I am looking for something simple to fulfill my basic needs.
No problem, just ignore the "connection pool" advice. Keep one connection open and available through static means and be happy. If it ever becomes a bottleneck, you can start adding more complex connection controls.
Alright then. The last thing is, how should I store the connection object? An attribute in a connections module, aside with the Connection class? In some sort of container that would be accessed by the Model class? What's the recommended way?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.