Hi all, This is to give big thanks to all. Erik - i will go thr it - i think i need some some time to grasp it. Regards and Have a nice day, Manisha
Erik Weber <[EMAIL PROTECTED]> wrote: Manisha Sathe wrote: >I am still new to struts and even servlets. Last time when i developed >servlets, i created one common class method to get the connection (with >regular JDBC call). All database related parameters i stored in a constant >variable file. > > That's the way I do it, with or without Struts, with or without a DataSource that pools Connections. Make a ConnectionManager class with either a static "getConnection" method, or an instance method if you are using multiple DataSources and a singleton per DataSource. Then all the rest of your app needs to know is ConnectionManager.getConnection(), or ConnectionManager.getConnectionManager("config_name").getConnection(). Put the config data in a constant file or a properties file. Include both the information needed to obtain a DataSource from the naming server and (possibly) the information needed to obtain a Connection directly from DriverManager. Write your ConnectionManager so that if it can't find the DataSource or get a Connection from it, it will log an alert and (possibly) get one from the DriverManager. If it finds the DataSource and can get a Connection from it, it should cache the DataSource reference so that it doesn't have to repeat the lookup: protected DataSource dataSource; protected void init() { try { InitialContext ic = new InitialContext(); //use JBoss' naming server Object ds = ic.lookup("java:/MyDataSourceName"); //use Tomcat's naming server //Object ds = ic.lookup("java:/comp/env/jdbc/MyDataSourceName"); dataSource = (DataSource) PortableRemoteObject.narrow(ds, DataSource.class); } catch (Exception e) { //problem! } } public Connection getConnection() { try { return dataSource.getConnection(); } catch (Exception e) { //do something about it return null; } } Also consider including cleanup methods. Your DAOs in any type of Servlet application can use this ConnectionManager class, which has no knowledge of Struts (why should it?). With that said, your DataSource implementation is much more important than where in your code you configure your DataSource and distribute your Connections (I have had problems with DBCP and leaks which may or not be my fault; there are other open source DataSource implementations). You don't gain anything that I know of by configuring Struts to know about your DataSource. Typically in Struts I do something like this: Action.execute --> Manager.get or Manager.put --> DAO.query or DAO.insert/update (using ConnectionManager). Also, it's important to clean up connections properly, if you are using a DataSource that pools connections. Use this idiom or similar: Connection c = null; PreparedStatement p = null; ResultSet r = null; ArrayList items = new ArrayList(); try { c = ConnectionManager.getConnection(); if (c == null) throw new SystemException("no database connection available"); String sql = SQLManager.getSQL(SQLManager.GUMMY_BEAR_FLAVORS_SELECT); p = c.prepareStatement(sql); r = p.executeQuery(); while (r.hasNext()) { //add gummy bear flavors to list } p.close(); //also closes r p = null; c.close(); c = null; return items; } //possibly catch and handle SQLExceptions, etc., here, //throwing system Exceptions to your managers finally { //if an Exception was thrown during JDBC operations //in the try clause, then the PreparedStatement and //Connection possibly were instantiated but not closed, //so this clause will close them; //if not, this clause will do nothing; //note: consider refactoring these lines into a //ConnectionManager.cleanup method to eliminate //duplicate code try { if (p != null) p.close(); } catch (Exception e) { //this would be a problem that should be handled } try { if (c != null) c.close(); } catch (Exception e) { //this would be a problem that should be handled } } See your J2EE server's documentation for how to configure the naming server to instantiate a DataSource (it's well documented both for JBoss and Tomcat). The advantage of using a DataSource that pools connections is that your application doesn't have to suffer the overhead of establishing a Socket connection, handshaking, authentication, etc., every time your application needs to use a Connection, as many Connections are prepared and "held" waiting at DataSource initialization time. Erik > >Now in struts i found out that we can specify the datasource inside >struts-config.xml. > >I want to know which methodology is better ? Can i continue with my existing >method ? or struts database method is better ? > >regards >Manisha > >__________________________________________________ >Do You Yahoo!? >Tired of spam? Yahoo! Mail has the best spam protection around >http://mail.yahoo.com > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------- Do you Yahoo!? Read only the mail you want - Yahoo! Mail SpamGuard.