DAO design was revised repeatedly recent days,I am glad of getting a perfect DAO solution
finally.
The sticking point in DAO design is where a DB connection is opened and when
to close it. Sometimes,one action executes only one DAO method,the connection
should be closed immediately follow the method. Sometimes, However, one action
may contain many DAOs or a series of methods in a DAO, (in other words, called transaction).
A same connection is used in DAOs,so closing connection after method execution is incorrect.
Therefore,I wrote two DAO's constructors for above two scenarios.For the first scenario,
opening a connection inside DAO,and close it at once after a method is invoked.For the second
scenario,a connection is created outside DAO, when instantiating a new DAO class,the connection
is passed from outside.We cope with this case (a transaction) in a service,the service invokes more
than one DAOs.DB connection is opened when service starting,and closed at the end of service.