Manisha Sathe wrote:
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.
You are welcome. Please note that there are many good patterns or idioms for this sort of thing -- check as many out as you can, including frameworks like iBatis, Hibernate, (as was suggested) etc. Oh, and don't forget JDO, because it looks like Sun is sticking with it -- maybe it's bigger for the EJB crowd. I'll say, frameworks are great, but ask, are you interested in being a specialist of vendor products, or a programmer? I suggest you should at least be a little of both . . . Me, I like to build stuff on my own because I always have the attitude that I can do it better (sometimes a costly attitude :) And yes, it takes years to learn all this stuff!
It certainly will help you to read Sun's JDBC tutorial, and the J2EE tutorial, which is getting better (I still think building it around the "deploy tool" -- the EJB part anyway -- is one of the worst ideas I've seen . . . how is that going to help you learn anything? The way to do it would be to pick three or four containers and deploy the same examples in each . . . It never taught me anything about the deployment descriptors doing it that way.). I did learn a *lot* from reading the Servlet specification, even though it's not a tutorial. Also, the Tomcat docs are pretty good -- especially the DataSource stuff. That "first webapp" section helped me a lot back in the early Tomcat 3 days. :) I suggest, even if you aren't using Tomcat in a professional capacity, that you learn to use it anyway. It will be worth the time.
Oh, and if you don't use Ant yet (you probably do even if it's under some IDE), you are in for a treat! Don't forget to check it out!
Erik
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.
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]