views:

74

answers:

1

I guess, DAO is thread safe, does not use any class members.

So can it be used without any problem as a private field of a Servlet ? We need only one copy, and

multiple threads can access it simultaneously, so why bother creating a local variable, right?

+1  A: 

"DAO" is just a general term for database abstraction classes. Whether they are threadsafe or not depends on the specific implementation.

This bad example could be called a DAO, but it would get you into trouble if multiple threads call the insert method at the same time.

class MyDAO {
     private Connection connection = null;

     public boolean insertSomething(Something o) throws Exception {
          try {
              connection = getConnection()
              //do insert on connection.
          } finally {
              if (connection != null) {
                  connection.close();
              }
          }
     }
}

So the answer is: if your DAO handles connections and transactions right, it should work.

deadsven
All right, thank you.
EugeneP