1 / 15

9.3 Data Access Objects

9.3 Data Access Objects. ATS Application Programming: Java Programming. Objectives. Define Data Access Object (DAO) Introduce the DAO classes in the HRS Application Introduce the methods of DAO Learn the steps in using DAO Use the transaction handling mechanism of DAO. Data Access Object.

Download Presentation

9.3 Data Access Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 9.3 Data Access Objects ATS Application Programming: Java Programming

  2. Objectives • Define Data Access Object (DAO) • Introduce the DAO classes in the HRS Application • Introduce the methods of DAO • Learn the steps in using DAO • Use the transaction handling mechanism of DAO

  3. Data Access Object The Data Access Object (also known simply as DAO) implements the access mechanism required to work with the data source. Regardless of what type of data source is used, the DAO always provides a uniform API to its clients. In this case, the data source used is a relational database management system (RDBMS) as the persistent store. The business component that needs data access uses the simpler interface exposed by the DAO for its clients. The DAO completely hides the data source implementation details from its clients. Because the interface exposed by the DAO to clients does not change when the underlying data source implementation changes, it allows you to change a DAO implementation without changing the DAO client's implementation. Essentially, the DAO acts as an adapter between the component and the data source.

  4. Data Access Object • There are 5 data access object classes that represent each table’s entities in the HRS application: • ProjectDAO • SkillDAO • SkillCategoryDAO • EmployeeDAO • EmpAccentureDetailsDAO • Each class implements methods from DataAccessObjectInterface • Input parameters of these methods are mostly in each corresponding bean class format that represents its data model (see com.jds.apps.beans package for the list of bean classes)

  5. Data Access Object Methods create method signature: public void create(Connection conn, Object object) throws DAOException update method signature: public boolean update(Connection conn, Object objSet, Object objWhere) throws DAOException remove method signature: public boolean remove(Connection conn, Object object) throws DAOException findByPK method signature: public Object findByPK(Object object) throws DAOException

  6. Data Access Object Methods find method signature: public RowSet find(Object object) throws DAOException findByAll method signature: public RowSet findByAll() throws DAOException

  7. Steps in Using Data Access Object • Import necessary packages (bean classes, DAO and db access) • Get an instance of the object • Get database connection when using data manipulation methods (create, remove and update) • Call the method • Additional steps for data manipulation methods: • Commit connection • Rollback transaction if exception occurred • Close the connection

  8. Data Access ObjectTransaction Handling • Transactional operations are handled by passing the database connection that will execute and commit the database update • An example of this operation is the, “create employee” • There are two table updates that are involved in creating an employee: • 1. Insert new entry for employee profile and • 2. Insert new entry for the corresponding Accenture details • All of these steps should have successful execution to create a new employee

  9. Data Access Object Sample CodeGeneral Steps //1.Import necessary packages. import com.jds.apps.Constants; import com.jds.apps.beans.*; import com.jds.architecture.service.dao.*; import com.jds.architecture.service.dbaccess.*; //2.Get an instance of the object. DataAccessObjectInterface empDao=(EmployeeDAO)DAOFactory.getFactory() .getDAOInstance(DAOConstants.DAO_EMP); //3.Get database connection when using data manipulation methods (create , remove and update) DBAccess dbAccess=DBAccess.getDBAccess(); Connection conn=dbAccess.getConnection();

  10. Data Access Object Sample CodeGeneral Steps //4.Call the method. //non data manipulation method id = EmployeeIdGenerator.getInstance().getNextId(); info.setEmpNo(String.valueOf(id)); empDao.create(conn, info); RowSet set = empDao.find(info); AccentureDetails details = info.getAccentureDetails(); details.setEmployeeNo(info.getEmpNo()); empAccDao.create(conn, details); OR //data manipulation method try{ try{ empDao.create(conn,info); //5.Commit connection. conn.commit(); }catch(DAOExceptione){

  11. Data Access Object Sample CodeGeneral Steps • //6.Rollback transaction if exception occurred. • dbAccess.rollbackConnection(conn); • if(e.isLogical())thrownewHRSLogicalException(e.getMessageKey()); • elsethrownewHRSSystemException(e.getMessageKey(),e.getCause()); • }catch(Exceptione){ • dbAccess.rollbackConnection(conn); • thrownewHRSSystemException("business.component.exception",e.getCause()); • }finally{ • //7.Close the connection. • dbAccess.closeConnection(conn); • }catch(DBAccessExceptione){ • thrownewHRSSystemException(e.getMessageKey(),e.getCause()); • }

  12. Data Access Object Sample CodeTransaction Handling • try{ • try{ • conn=dbAccess.getConnection(); • empDao.create(conn,info); • RowSet set = empDao.find(info); • AccentureDetailsdetails=info.getAccentureDetails(); • if(set.next()){ • details.setEmployeeNo(set.getString("empno")); • } • empAccDao.create(conn, details); • //commit employee and Accenture detail create entry • conn.commit(); • }catch(DAOExceptione){

  13. Data Access Object Sample CodeTransaction Handling • //rollback all operations on exception • dbAccess.rollbackConnection(conn); • //TODO checking for rollback • if(e.isLogical())thrownewHRSLogicalException(e.getMessageKey()); • elsethrownewHRSSystemException(e.getMessageKey(),e.getCause()); • }catch(Exceptione){ • //rollback all operations on exception • dbAccess.rollbackConnection(conn); • thrownewHRSSystemException("business.component.exception",e.getCause()); • }finally{ • dbAccess.closeConnection(conn); • } • }catch(DBAccessExceptione){ • thrownewHRSSystemException(e.getMessageKey(),e.getCause()); • }

  14. Key Points • DAO implements the access mechanism required to work with the data source, regardless of what type of data source is used • Each table entity in the HRS Application is represented by a DAO class • Methods in DAO used in HRS Application are as follows: • create(), update(), remove(), findByPK(), find(), findByAll() • DAO is also used to implement Transaction Management

  15. Questions and Comments ???

More Related