Yes, This is what I want to do. I am just getting underway with this new project, and am looking for a solution to handle this. I have read the full O'Reilly struts book, and I will keep the Action classes clean of business logic.
If this is not asking too much, is the basic workflow for this to create a singleton (possibly servlet set to load at startup) have it start up singleton class, and be able to hand back connections? I am not familar with the ServiceLocator pattern, and I am also new to using the JNDI type of database pooling objects. If anyone has examples, links, articles for me to review, or comment, please let me know, Sincerely Scott -----Original Message----- From: Chaikin, Yaakov Y. [mailto:[EMAIL PROTECTED] Sent: Friday, January 28, 2005 10:10 AM To: 'Struts Users Mailing List' Subject: RE: Database Connection Workflow Question 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]