Re: Java DAO pattern: singleton and threadsafe?



koenxjans@xxxxxxxxx wrote:
Hello all,

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

That the DAOs are singletons and that there is one abstract superclass are orthogonal.

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

It's a lot easier to make an instance variable thread safe than a static variable. The problem isn't that there's an instance variable, the problem is that you're using a singleton. Stop using the class as a singleton.

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.

I suggest that you provide an SSCCE - a simple, self-contained compilable example that illustrates the points you're making. All this vague hand-waving doesn't admit of any useful comment. The example should elucidate the "singleton-ness" of the approach.

--
Lew
.



Relevant Pages

  • Re: Java DAO pattern: singleton and threadsafe?
    ... That the DAOs are singletons and that there is one abstract superclass are ... DataSource dataSource = initial.lookup; ...
    (comp.lang.java.programmer)
  • Re: Singletons in Session...?
    ... l> It is a good practice to insert singletons in Session? ... l> session state is a good idee:| ... l> public class DAO ... l> public static DAO Instance ...
    (microsoft.public.dotnet.framework.aspnet)
  • Singletons in Session...?
    ... It is a good practice to insert singletons in Session? ... session state is a good idee:| ... public class DAO ... public static DAO Instance ...
    (microsoft.public.dotnet.framework.aspnet)
  • DAOs and Connection sharing
    ... I have been using a DAO -based design to implement my applications ... business logic classes and DAOs ... Action gets a Connection from a DataSource; ...
    (comp.lang.java.programmer)
  • Re: subclass with static constructor
    ... singletons. ... instance (containing the db-specific connection string). ... there's no such thing as a static subclass - at all. ... it means that static is not what you need - insisting on ...
    (microsoft.public.dotnet.languages.csharp)