Two Laptop Bag*
The moose likes Tomcat and the fly likes Accessing Tomcat DB Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Accessing Tomcat DB Pool" Watch "Accessing Tomcat DB Pool" New topic
Author

Accessing Tomcat DB Pool

Oliver Moore
Ranch Hand

Joined: Mar 04, 2003
Posts: 44
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 ]

Regards,<p>Ols
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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'
 
 
subject: Accessing Tomcat DB Pool
 
Similar Threads
Datasource question
Tomcat, MySQL,JSP/Servlets -> Connection Pooling
Connection Pooling samples
Data Access Code
Reading data from multiple tables. Any better way than this?