Meaningless Drivel is fun!*
The moose likes EJB and other Java EE Technologies and the fly likes  Java DAO pattern: singleton and threadsafe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark " Java DAO pattern: singleton and threadsafe?" Watch " Java DAO pattern: singleton and threadsafe?" New topic
Author

Java DAO pattern: singleton and threadsafe?

koen ja
Greenhorn

Joined: Oct 15, 2007
Posts: 10
Hello all,

Here's my problem:
I'm working on a legacy J2EE application that uses the DAO pattern
and implements all DAO's as singletons. There's one abstract DAO
superclass.

But this class holds the connection to the database as an instance
variable.
Which, in my opinion, is not thread safe.

If multiple clients would make a call simultaneously, the very same
connection
would be used for the different calls, resulting in an exception on
the app server.

I think a different connection should be gotten from the datasource
connection
pool on everycall? And that the datasource itself should be an
instance variable.

All opinons are appreciated!

Thanks,
Koen
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

Originally posted by koen ja:
I think a different connection should be gotten from the datasource
connection
pool on everycall? And that the datasource itself should be an
instance variable.

I completely agree with this!


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java DAO pattern: singleton and threadsafe?
 
Similar Threads
ServeltContextListener for DataSource
JNDI design question
Transaction EJB session bean
Implement Datasource
how should the optimized DAOs used in Stateless Session Bean look like?