1

Where must be placed Connection object in DAO design pattern?

Do I need to create separate class (e.g. DaoManager) with 2 methods: open() and close()? And in business logic do like:

DaoManager.open();
PersonDao personDao = DaoFactory.getPersonDao();
Person person = personDao.getById(personId);
.... //more DAO operations
DaoManager.close()
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286

2 Answers2

2

The connection should be a local variable of the PersonDao.getById() method (and of the other DAO methods). It should get the connection from a DataSource, use it, and close it in a finally block.

If you told a bit more about your environment (Spring, EJB container?), I could give more explanations.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks. I use WAS 7 (EJB container). How to deal with DataSource in WAS i know..But in real I don't work with JDBC, I work with another framework which have it custom repository. And it also have Connection objects etc. So I want to keep my code abstract as possible for easy changing internal implementations to use JDBC/JPA. – WelcomeTo Dec 27 '12 at 09:15
  • Well, how could we answer then? We don't know anything about this other framework. – JB Nizet Dec 27 '12 at 09:19
  • So in case of DataSource, how should look my DAO classes (I think my framework in implementations also uses DataSource, so code should be same)? I need to have DAOFactory, and in constructor of DAOFactory I need to place `Connection` object? And DAOFactory will initialize `PersonDAO` putting to constructor Connection instance variable (getPersonDao(mConnection))? – WelcomeTo Dec 27 '12 at 09:23
  • No. The DAO should have a field of type DataSource, and every method of the DAO should get a connection from the DataSource field, use it, and close it in a finally block. Which version of the JEE spec does WAS 7 support? If JEE 6, your DAOs should be CDI beans injected into service EJBs. If JEE5, your DAOs should be EJBs injected into service EJBs. You shouldn't need any DAOFactory. – JB Nizet Dec 27 '12 at 09:33
  • Hmm. ok, thanks. Now I understand it much clearly. WAS 7 support only JEE 5. What about using Google Guice for injecting DAO classes? In following post @Pascal Thivent suggest don't to use EJB's as DAO. http://stackoverflow.com/questions/3223112/javaee6-dao-should-it-be-stateless-or-applicationscoped – WelcomeTo Dec 27 '12 at 09:38
  • I agree with him, if CDI is available. But you don't have CDI. If you want to be able to inject DAOs into EJBs and the DataSource into DAOs without CDI being available, EJBs are the only thing you have, AFAIK. I've never tried to setup Guice inside an EJB environment, but since EJB instances can't be created by Guice, but must be created by WAS, injecting DAOs into them will be painful, if not impossible. – JB Nizet Dec 27 '12 at 09:44
  • Ok. Again thank you for big help:). And last question: Does my DAO classes need to be Singletons? – WelcomeTo Dec 27 '12 at 10:01
  • 1
    It doesn't matter much, since they should be thread-safe and stateless (except for the DataSource reference). I make them singletons, but objects are cheap in Java nowadays. – JB Nizet Dec 27 '12 at 10:05
1

In a large application you would typically hold the connections in a connection pool so that they can be reused for several purposes. One does this because opening a new DB connection can be somewhat expensive.

The DAO gets a reference to the pool and takes out a connection when it is needed. After the operation it would give it back to the pool.

There are several open source implementations for connection pools available, you may want to reuse one of these.

Henry
  • 42,982
  • 7
  • 68
  • 84