Hi,
How about using a Vector in place of an ArrayList?

regards
Jayant

-----Original Message-----
From: Kesavanarayanan, Ramesh (Cognizant)
[mailto:[EMAIL PROTECTED]]
Sent: Friday, December 20, 2002 10:26 AM
To: [EMAIL PROTECTED]
Subject: static initialization


Hi all,

 I have a Helper class which has several methods. Each method willspeak
overto the DB and get the values as ArrayList. These values will be loaded
in the HTML pages as option values of the select boxes.


Here is the sample code snippet i have used . I have made the OWNER_LIST as
private static.
          private   static  ArrayList distinctOwner(String tableName)  {
                Connection conn = null;
                PreparedStatement pstmt = null;
                DBUtilities DBUtil = null;
               if(OWNER_LIST == null) {
                try
                  {
                         OWNER_LIST = new ArrayList();
                         DBUtil = new DBUtilities();
                         conn.setAutoCommit(false);;
                             String sql = "select distinct owner_name from "
+ tableName + " order by owner_name";
                             ResultSet rs = DBUtil.doSelect(sql);
                             while (rs != null && rs.next())
                             {
                                OWNER_LIST.add(rs.getString(1));
                             }
                            rs.close();
                  } catch (Exception e) {
                               System.err.println("Problem getting distinct
owner from " + tableName + " order by owner name");
                    }
                    finally {
                        try { pstmt.close(); } catch (Exception e) { }
                        try {
                               DBUtil.closeConnection();
                        } catch (Exception e) { }
                   }
                 }
                  return OWNER_LIST;
            }
I have made all these ArrayList  as static so that they will be populated
only once in the server. Typically I amtrying to catch the records.

Some transaction during the application may change the DB records.

 In such scenario i have to call a Refresh such that this makes all the
values of the Helper class again to null so that when next time somebody
clicks on some item Helper class has to fetch the new values from the DB.

 My question is supposing that when a user clicks on the Refresh buton and
at the same time some other user is looping thru the ArrayList in their jsp
pages, it will surely thro an error.

 How to prevent this.

I thought of putting the ArrayList initialization as synchronized but in my
Helper class i have many methods (25). That means i will be having 25
Synchronized blocks which might become too overhead for the webserver.

 Any nice ideas and suggestions to resolve this ishighly appreciated.

 Regards

 Ramesh Kesavanarayanan
 [EMAIL PROTECTED]
 +91-44- 811 3063 ext 2232 VNet- 42425

==========================================================================To 
unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to