Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Databases > Caching of Data...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 3708 of 3747
Post > Topic >>

Caching of Database Access Objects possible?

by "palak123i@[EMAIL PROTECTED] " <palak123i@[EMAIL PROTECTED] > Apr 19, 2008 at 07:47 PM

I am working on a web application. I am using Struts for the
presentation layer. Also I am using DAO pattern for Database layer.

My doubt is about creating instance of DAO's. I am created a DAO
Factory which can create instances of DAO's. Whether I can go for
caching of DAO or Do I have to create a new DAO for everytime? Would
caching done as shown in the code below create Concurrency issues? or
please suggest the right way of going about it.

My Struts Action code is as follows:


public class WelcomeAction extends Action {
	static Category log =
Category.getInstance(WelcomeAction.class.getName());

	public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws
Exception {

		try{
			UserDAO userDAO =
(UserDAO)DAOFactory.getInstance("com.share.dao.UserDAO");
			ResultObj obj = (ResultObj)userDAO.getUserList();
			List userList = (ArrayList)obj.getResults();
			request.setAttribute("userList", userList);
		}catch(Exception e){
			e.printStackTrace();
			return mapping.findForward("errorPage");
		}



		return mapping.findForward("success");
	}



UserDAO  code is as follows:


public abstract class UserDAO extends BaseDAO {

	public Object getUserList() throws DBException{
		Connection conn = null;
		PreparedStatement stmt = null;
		Object obj = null;

		try{
			conn = DBConnectionManager.getConnection();
			stmt = conn.prepareStatement("Select * from USER_INFO where USER_ID
like ?");
			stmt.setMaxRows(3);
			stmt.setString(1, "%");
			stmt.executeQuery();
			obj = processGetUserList(stmt);

		}catch(SQLException e){
			throw new DBException(e.getMessage());
		}finally{
			DBConnectionManager.close(stmt);
			DBConnectionManager.closeConnection(conn);
		}

		return obj;
	}


And the DAO Factory is as follows:



package com.share.dao;

import java.util.HashMap;

public class DAOFactory
{
	private static HashMap hash = new HashMap();

    public DAOFactory()
    {
    }

    public static BaseDAO getInstance(String daoName)
    {
        BaseDAO dao = null;
        try
        {
            dao = (BaseDAO)hash.get(daoName);
            if(dao == null)
                dao = createNewInstance(daoName);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return dao;
    }

    private static synchronized BaseDAO createNewInstance(String
daoName)
    {
        BaseDAO dao = null;
        try
        {
            dao = (BaseDAO)hash.get(daoName);
            if(dao != null)
                return dao;
            Class daoClass = Class.forName(daoName + "Impl");
            dao = (BaseDAO)daoClass.newInstance();
            hash.put(daoName, dao);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return dao;
    }


}




 4 Posts in Topic:
Caching of Database Access Objects possible?
"palak123i@[EMAIL PR  2008-04-19 19:47:20 
Re: Caching of Database Access Objects possible?
"palak123i@[EMAIL PR  2008-04-21 22:43:43 
Re: Caching of Database Access Objects possible?
Sabine Dinis Blochberger   2008-04-22 09:28:17 
Re: Caching of Database Access Objects possible?
Lew <lew@[EMAIL PROTEC  2008-04-22 07:05:17 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue May 13 18:40:46 CDT 2008.