Does anyone know of any techniques that would allow a Database Access Object (DAO) to be used from the various actions that make up a Struts application?

I have a DAO which, when instantiated, acquires a data source from the servlet container (in this case, Apache Tomcat). Thereafter, I can execute the various methods provided by the object which use the data source to execute SQL queries/statements, and return usable data in the form of Strings, Maps, Vectors etc, as appropriate.

Instantiating the DAO in the execute() method of each of my action's however, seems a little inefficient. Does Struts provide any way to instantiate the object once and then make it available for the lifetime of the container (in a way that my Actions can access it)?

Note please that i'm well aware that this is a somewhat non-standard approach to database interaction. I am intending to investigate Hibernate (and like) at some point, but for the time being I want to avoid such.

This is the skeleton of the DAO I have in mind...

----------------------------------------------------------------------
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import javax.servlet.*;

public class MyDataAccessObject {

   private DataSource dataSource;
   private String SQL;

   public MyDataAccessObject() throws Exception {

       try {
           Context init = new InitialContext();
           Context ctx = (Context) init.lookup("java:comp/env");
           dataSource = (DataSource) ctx.lookup("jdbc/mysqldb");
       } catch (NamingException ne) {
throw new Exception("Cannot retrieve java:comp/env/jdbc/mysqldb", ne);
       }

       SQL = null;
   }

   public String getValue(String name) {

       Connection dbcon = null;
       PreparedStatement pstmt = null;
       ResultSet rs = null;

       String value = null;

       try {
           dbcon = dataSource.getConnection();

           SQL = "SELECT value FROM table WHERE name = ?";

           pstmt = dbcon.prepareStatement(SQL);
           pstmt.setString(1, name);

           rs = pstmt.executeQuery();
           while (rs.next()) {
               value = rs.getString("value");
           }

           rs.close();
           rs = null;

           pstmt.close();
           pstmt = null;

           dbcon.close();
           dbcon = null;
       } catch (SQLException ex) {
           log(ex);
       } catch (Exception ex) {
           log(ex);
       } finally {
           if (rs != null) {
               try { rs.close(); } catch (Exception fe1) { log(fe1); }
               rs = null;
           }

           if (pstmt != null) {
               try { pstmt.close(); } catch (Exception fe2) { log(fe2); }
               pstmt = null;
           }

           if (dbcon != null) {
               try { dbcon.close(); } catch (Exception fe3) { log(fe3); }
               dbcon = null;
           }
       }

       SQL = null;
       return value;
   }

   private void log(...) {
       // Handle logging
   }
}
----------------------------------------------------------------------

Thanks,

Jazz




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to