Results 1 to 2 of 2
  1. #1
    JavaAl2 is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default Struts framework. Is this thread safe?

    Hi,
    In the FOOAction.execute method, there are two local variables (ExcelHelperBean and rData ) used for datacollection. The rData is a reference to the FOODAO class. In the FOODAO.getReportData method, it initializes a Collection (via ArrayList), executes a select statement, and returns the Collection of FooBean(s). Because both of the methods have local variables instead of Class level variables, they are thread safe. Correct?
    I should not have to synchronize, correct?


    public class FOOAction extends Action implements ReportDefinitions
    {

    static Logger log = Logger.getLogger(FOOAction.class.getName());

    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request,HttpServletResponse response) throws Exception
    {
    ActionMessages errMsgs = new ActionMessages();
    HttpSession session = request.getSession();
    Fields fields = new Fields();
    boolean error = false;
    IncidentTimeDateForm fooForm = (FOOForm) form;
    String strName = fooForm.getname();

    try
    {
    //Helper Bean per Actionclass
    ExcelHelperBean excelHelperBean = new ExcelHelperBean();
    //Init DAO
    ReportQuery rData = new FOODAO();
    Map htUser = new HashMap();

    htUser.put(ReportDefinitions.NAME,strName);
    //Place results into a Collection for iterating through on jsp
    //
    excelHelperBean.setColExcelData(rData.getReportDat a(htUser));

    request.setAttribute(ReportDefinitions.EXCELHELPER CLASS,excelHelperBean);


    }
    catch (Exception e)
    {
    e.printStackTrace();
    return mapping.findForward(ReportDefinitions.FAIL);
    }
    return mapping.findForward(ReportDefinitions.SUCCESS);

    }



    }
    public class FOODAO extends BaseDAO
    {
    private final static String FASQL = "select distinct per.LASTNAME,i.USERID,pro.PROVIDER,"+
    " it.DESCRIPTION,TO_CHAR(i.INCIDENTDATETIME,'MM/DD/YYYY') as incidentdate,"+
    " TO_CHAR(i.INCIDENTDATETIME,'DAY') incidentDay, "+
    " TO_CHAR(i.INCIDENTDATETIME,'HH24:MI') incidentTime,location.description locDescription "+
    " from incident i,incident_incidenttype iit, incident_type it,provider pro,source, incident_participant ip,"+
    " participant part, person per,location where i.name='";

    public Collection getReportData(Map mValues ) throws Exception
    {
    Statement stmt = null;
    ResultSet rs = null;
    Collection calFirstAid = new ArrayList();

    String sName =mValues.get(ReportDefinitions.NAME).toString();


    StringBuffer sbSQL = new StringBuffer(FASQL);
    try
    {

    sbSQL.append(sName).append("'");
    //GetConnection from BaseClass
    openConnection();
    stmt = conn.createStatement();

    rs = stmt.executeQuery(sbSQL.toString());
    FooBean faBean = null;
    System.out.println("SQL "+sbSQL);
    while (rs.next())
    {
    faBean = new FooBean();
    faBean.setLastName(rs.getString("LASTNAME"));
    faBean.setIncidentTrackingno(rs.getString("USERID" ));
    faBean.setProviderName(rs.getString("PROVIDER"));
    faBean.setTypeDescription(rs.getString("DESCRIPTIO N"));
    faBean.setIncidentDate(rs.getString("INCIDENTDATE" ));
    faBean.setIncidentDay(rs.getString("INCIDENTDAY")) ;
    faBean.setIncidentTime(rs.getString("INCIDENTTIME" ));
    faBean.setLocDescription(rs.getString("locDescript ion"));

    calFirstAid.add(faBean);
    }

    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    finally
    {
    closeStatement(stmt);
    closeConnection();
    }

    return calFirstAid;

    }

    }

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    10

    Default

    Welcome to the forum JavaAl2,

    What you said in first paragraph is right, as long as you don't have shared resources (including static/class level variables) you don't need to worry about thread safety.
    dont worry newbie, we got you covered.

Similar Threads

  1. Are Local variables thread safe ?
    By samson in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-21-2010, 03:34 PM
  2. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:31 PM
  3. Replies: 0
    Last Post: 04-07-2008, 07:54 PM
  4. Struts Validator Framework
    By Albert in forum Web Frameworks
    Replies: 2
    Last Post: 02-15-2008, 12:01 PM
  5. Local Variables for a static method - thread safe?
    By mikeg1z in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 02:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •