• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Connection object in BaseDAO class - Bad?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I had a feeling this would be real bad.

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

Thanks again,

Aaron
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Aaron Wilt
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic