Scott,

1) I hope the code you showed is sitting inside a DAO, not your Action.
2) It looks much messier because you are not using something like a
ServiceLocator Pattern. Look up and cache the reference to your DataSource
there. It's cleaner and way faster than doing it every time in your DAO.
3) Also, to improve performance, you should probably store your connection
in a private member variable in your DAO and have a base class for your DAO
with a getConnection() method. This way, any DAOs you implement that extend
from the BaseDAO will already know how to get the connection in a very
efficient and clean way.

HTH,
Yaakov.

-----Original Message-----
From: Scott Purcell [mailto:[EMAIL PROTECTED]
Sent: Friday, January 28, 2005 9:47 AM
To: user@struts.apache.org
Subject: Database Connection Workflow Question

Hello,

I am running Tomcat struts. I am beginning a new project using the struts
technologies and have a question in regards to handling connections.

First off, just to give you some background, older projects I worked on had
a singleton class that handed me database connections. So when I needed a
connection, I would just ask for a connection from a static class.

Now that I am starting into my struts project, I am beginning to wonder how
the best practice would be to handle connections. In tomcat I configured a
<ResourceParams name="jdbc/JNDITest" object to configure the data source.

Now in my java class files, I am finding myself doing the following:
    try {
      Context initCtx = new InitialContext();
      Context envCtx = (Context)initCtx.lookup("java:comp/env");
      DataSource ds = (DataSource)envCtx.lookup("jdbc/JNDITest");
      Connection con = ds.getConnection();
      PreparedStatement select = con.prepareStatement("Select * from
exchange");
      ResultSet rs = select.executeQuery();

      while (rs.next()) {
        System.out.println(rs.getString("rate"));
      }
      rs.close();
      select.close();
      con.close();

The problem is, this seems a lot messier than using a singleton and getting
handed connections. I may be mistaken, and if so I apologize, but it just
seems like a lot of duplication of code for the above.

Also I am using some taglibs in which I need to connect to the database.

Could anyone give me some suggestions, in "best" practices using these
technologies.

Thanks for your time,
Sincerely
Scott

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to