> 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
[quoted text clipped - 5 lines]
> 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.

Signature
Lew
koenxjans@gmail.com - 14 Apr 2008 15:15 GMT
> koenxj...@gmail.com wrote:
> > Hello all,
[quoted text clipped - 32 lines]
> --
> Lew
Hey Lew,
Thanks for your comment. To provide you with some (simplified) code:
The superclass looks like this:
(this can be abstract, although subclasses are singletons ;) )
-------------------------------------
package nl.nedcar.apollo.server.dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public abstract class AbstractDAO {
private DataSource dataSource;
private Connection connection;
private final static String JNDI_NAME = "yourJndiName";
protected AbstractDAO() {
InitialContext initial;
try {
initial = new InitialContext();
DataSource dataSource = (DataSource) initial.lookup(JNDI_NAME);
Logger.debug(this, "Obtained ref to DataSource " + JNDI_NAME);
this.dataSource = dataSource;
}
catch (NamingException e) {
e.printStackTrace();
}
}
private Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
protected void releaseConnection() throws SQLException {
if(connection != null) {
connection.close();
}
connection = null;
}
protected Statement getStatement() throws SQLException {
if(connection != null) {
RuntimeException rt = new
RuntimeException(this.getClass().getName() + ": Creating new statement
while previous query was not " + "properly closed. \nClosing previous
connection...");
releaseConnection();
throw rt;
}
connection = getConnection();
return connection.createStatement();
}
}
The singletons subclasses look like:
-------------------------------------
package nl.nedcar.apollo.server.dao;
import java.sql.ResultSet;
public class FirstDAO extends AbstractDAO {
private static FirstDAO theInstance;
public static synchronized FirstDAO getInstance() {
if(theInstance == null) {
theInstance = new FirstDAO();
}
return theInstance;
}
public String getSomethingFromDatabase() throws Exception {
try {
ResultSet s = getStatement().executeQuery("select something from
users");
if(s.next()) {
return s.getString("username");
}
return null;
}
finally {
releaseConnection();
}
}
}
-------------------------------------
calls are made like:
String somethingFromDb =
FirstDAO.getInstance().getSomethingFromDatabase();
So, the problem occurs when multiple clients are performing a call at
the moment:
client1 holds the connection, while client2 is attempting to use the
very same connection.
Lew - 15 Apr 2008 13:32 GMT
> package nl.nedcar.apollo.server.dao;
>
[quoted text clipped - 3 lines]
>
> private static FirstDAO theInstance;
Here's your trouble. You will also note than none of the methods or other
accesses to shared state are synchronized. This code was designed to fail.
> public static synchronized FirstDAO getInstance() {
> if(theInstance == null) {
[quoted text clipped - 7 lines]
> ResultSet s = getStatement().executeQuery("select something from
> users");
Note that
>> [a] ResultSet object is automatically closed when the Statement object that
>> generated it is closed, re-executed, or used to retrieve the next result from
>> a sequence of multiple results.
> if(s.next()) {
> return s.getString("username");
[quoted text clipped - 7 lines]
>
> }
Yep. Designed to fail.

Signature
Lew
koenxjans@gmail.com - 15 Apr 2008 15:29 GMT
> koenxj...@gmail.com wrote:
> > package nl.nedcar.apollo.server.dao;
[quoted text clipped - 41 lines]
> --
> Lew
Thanks for your reply.
One more question to clarify things for me..
In my opinion, this legacy code is indeed designed to fail.
Because the superclass holds an instance of the connection.
But, what if, like I mentionned in my first post, an instance
of the datasource is kept in the superclass, not the connection
itself.
With the superclass code like:
protected Statement getStatement() throws SQLException {
return dataSource.getConnection().getStatement();
}
protected void releaseResources(ResultSet resultSet) throws
SQLException {
...
}
If 2 threads would access the getSomethingFromDatabase() method
simultaneously,
would..
a) both threads use a difference connection?
So operations on the resultset of one thread, would not interfere
with
the resultSet of the other thread.
b) one thread still take the resultSet of the other thread, breaking
the design?
This would mean Singleton DAO's are always a no go.
Thanks!
Koen
koenxjans@gmail.com - 15 Apr 2008 15:35 GMT
On Apr 15, 4:29 pm, "koenxj...@gmail.com" <koenxj...@gmail.com> wrote:
> > koenxj...@gmail.com wrote:
> > > package nl.nedcar.apollo.server.dao;
[quoted text clipped - 78 lines]
> Thanks!
> Koen
Oh,
I think it's thread safe and the answer is a).
Because only local variables are used.
And every caller gets their own copy of local variables in a method.
Koen
On Mon, 14 Apr 2008 01:38:51 -0700 (PDT), "koenxjans@gmail.com"
<koenxjans@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>and implements all DAO's as singletons.
See http://mindprod.com/jgloss/designpatterns.html and follow the DAO
link to Sun's site. Something on that page may be useful.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com