Hi all, I'm looking at implementing a connection pool using Tomcat to replace a connection pool that uses the struts GenericDataSource. With this method, I would call an external class to return the datasource object for the connection. However, with most of the Tomcat examples I've seen, the method for getting the connection is to include something like the following in each of your classes needing DB access:
Is it necessary to place this code in each object requiring db access? Is it simpler to place this code in a seperate class file and call it each time you require it, or does this create large performance penalties? Apologies if I'm missing something really simple, I just wanted to check I was taking the right route... [ March 10, 2004: Message edited by: Oliver Moore ]
Dispersing the *how* of obtaining a connection throughout your classes would then tie you to that exact way of obtaining connections... until you re-write and recompile.
So perhaps as a general 'rule', it's better to write any classes that require a connection to be designed in such a way as to be 'given' a connection by some "other object". Either directly, through methods such as "setConnection", or any method that needs connections would have, as a parameter, the Connection to us. Or perhaps if you don't want to pass the Connection directly, you could design it in such a way as to obtain the Connection from an Interface. That "other object" or "interface" might be that 'separate class' you spoke of.
That way, you centralize the *how*, and need change only one class. Or better, provide an alternate implementation of some interface (that you design).
On the other hand, this is starting to sound an awful lot like 'DataSource'