05-16-2012 09:21 AM
Hello,
In the attached image, I do a database query with one query string. It opens the connection, makes the query, and closes the connection. The output array is scanned for certain conditions later.
If I have 10 queries I could simply make an array of queries and make an array of replies. Then I could decompose the output array in a for loop and run my evaluations.
In OOP however, I find myself wanting to make each query as an object, to encapsulate it. That is a problem though, because the database resides in a server in another building across town, and the connection to it is slow. So multiple queries take too long.
How do I approach this in OOP?
05-16-2012 09:26 AM
In your class you would have an open method, some query methods, and a close method. Outside of a loop you call the open method. Wire the class to the left side of the loop and make it a shift register. Do your queries in the loop, and after the loop call the close method. You might find that it is faster since you are not opening and closing the connection for each query.
05-16-2012 09:49 AM
For something like this I would definately consider an architexture simmilar to the Actor framework.
You are going to want to only open the connection once as described above, but you want those requests to occur in a different thread than your parsing. I realize that your parsing time may be negligable but because of your slow connection I would still offload that processing to keep the database busy at all times.
Essentially you'll be making a loop which responds to transaction requests and sends results back to the requester as they come in.
05-16-2012 10:17 AM
Or yet another approach...
Use "Composition" pattern to say the connection is a part of the query and implement the connection class using the Singlton pattern.
The Connection method that crates the connection would only make the connection when an open is invoked and the connection does not alreday exist.
So your queries are an array like what seems natural (is that "Cohesion").
Ben
05-16-2012 10:28 AM
Wow. Lots to think about. First I'd heard of the Actor Framework.