aspose file tools*
The moose likes JDBC and the fly likes Static Connection object in BaseDAO class - Bad? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Static Connection object in BaseDAO class - Bad?" Watch "Static Connection object in BaseDAO class - Bad?" New topic
Author

Static Connection object in BaseDAO class - Bad?

Aaron Wilt
Ranch Hand

Joined: Sep 26, 2001
Posts: 49
Hi all,

I'm not real well versed in data access logic and practices but I came across this and I'm wondering if this is real bad.

This is the Base Data Access class for all our DAOs, it stores a static Connection object for all the DAOs to use and it only snags the connection on first use.

Our web app that doesn't have real heavy db transactions but could eventually be used by 50-100 people concurrently pulling or inserting a few records at a time.



Thanks,

Aaron

[ August 30, 2006: Message edited by: Aaron Wilt ]
[ August 30, 2006: Message edited by: Aaron Wilt ]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

It looks like you've just combined all of the drawbacks with using database connections with all of the drawbacks of maintaining your own database connections.

You really should just be taking from the pool when you need a connection. That's what the pool is there for.

Here's a link that provides a whole bunch of information about what a connection pooling service is really doing for you.

http://www.technicalfacilitation.com/get.php?link=pooling

Yes, the code scares me.
Aaron Wilt
Ranch Hand

Joined: Sep 26, 2001
Posts: 49
Thanks, I had a feeling this would be real bad.

I'll check out that link and get it changed.

Thanks again,

Aaron
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

You can still have a base DAO class that all DAO's extend from that provide database access, but it should call a connection pool that the server can manage. You will need a way to connect the code to the pool (by use of JNDI usually) but at least then you can change the database information later without changing code.

You could also load a properties file that stores the JNDI if there is a chance you want to change the JNDI later although this can be overkill.


My Blog: Down Home Country Coding with Scott Selikoff
Aaron Wilt
Ranch Hand

Joined: Sep 26, 2001
Posts: 49
Ok here's how I changed the code. I read in the link above that the connections won't return to the pool unless they are closed. I added a cleanup() method for all subclasses to call in their finally blocks after using a connection.

Does this look better? Anything else I should change wrt connection pooling or other standard DAO practices?



Also, we're using Tomcat 5 for our App Server. Here's the datasource info in server.xml. Any tweaks I should be making here?


Thanks again for everyone's time and consideration.

Aaron

[ August 31, 2006: Message edited by: Aaron Wilt ]
[ August 31, 2006: Message edited by: Aaron Wilt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Connection object in BaseDAO class - Bad?
 
Similar Threads
Connection class related Query
ServeltContextListener for DataSource
Unable to create Data Source in Tomcat
Static Method for Database Connection
mysql connectivity with jboss