Re: About datasource
Hi, after some thinking, I have another problem, ^^ If I am using an Action (which extent the Struts' Action class), I can get the Datasource by calling getDataSource() But now I want to write my own data assess object which shouldn't extent Action, I don't know what way I can get the datasource from the pool. any help?? Regards - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: About datasource
Hello, How about retrieving it from JNDI? How about a plugIn (DAOFactory) that retrieves the DataSource via JNDI and then initializes each DAO with the DataSource? Regars marco -Original Message- From: Koon Yue Lam [mailto:[EMAIL PROTECTED] Sent: 16 July 2004 10:18 To: Struts Users Mailing List Subject: Re: About datasource Hi, after some thinking, I have another problem, ^^ If I am using an Action (which extent the Struts' Action class), I can get the Datasource by calling getDataSource() But now I want to write my own data assess object which shouldn't extent Action, I don't know what way I can get the datasource from the pool. any help?? Regards - 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]
RE: About datasource
Hi: I think this is the sort of thing you may be looking for: Context ctx = new InitialContext(); org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)ctx.lookup(java:comp/env/jdbc/yourDB)); Regards, Geeta -Original Message- From: Koon Yue Lam [mailto:[EMAIL PROTECTED] Sent: Friday, July 16, 2004 5:18 AM To: Struts Users Mailing List Subject: Re: About datasource Hi, after some thinking, I have another problem, ^^ If I am using an Action (which extent the Struts' Action class), I can get the Datasource by calling getDataSource() But now I want to write my own data assess object which shouldn't extent Action, I don't know what way I can get the datasource from the pool. any help?? Regards - 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]
Re: About datasource
Geeta Ramani wrote: Hi: I think this is the sort of thing you may be looking for: Context ctx = new InitialContext(); org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)ctx.lookup(java:comp/env/jdbc/yourDB)); That's definitely the sort of code to use when you want to pull things out of JNDI, but I would suggest one small change -- use the generic interface (javax.sql.DataSource) instead of hard coding the implementation class that the current version of Tomcat currently happens to use (o.a.c.d.BasicDataSource). This will protect you from future changes that Tomcat might make, or if you ever needed to move your app to a different server. Doing this, the above code would become: Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup(java:comp/env/jdbc/yourDB); For those using Tomcat, there's pretty extensive documentation on the Tomcat web site about how to set up JNDI resources (including the code snippet above :-): http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-resources-howto.html http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html Regards, Geeta Craig -Original Message- From: Koon Yue Lam [mailto:[EMAIL PROTECTED] Sent: Friday, July 16, 2004 5:18 AM To: Struts Users Mailing List Subject: Re: About datasource Hi, after some thinking, I have another problem, ^^ If I am using an Action (which extent the Struts' Action class), I can get the Datasource by calling getDataSource() But now I want to write my own data assess object which shouldn't extent Action, I don't know what way I can get the datasource from the pool. any help?? Regards - 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] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: About datasource
Sure Craig, I guess it's a good idea to look beyond the weekend..;) Thanks for the advice! Geeta -Original Message- From: Craig McClanahan [mailto:[EMAIL PROTECTED] Sent: Friday, July 16, 2004 12:16 PM To: Struts Users Mailing List Subject: Re: About datasource Geeta Ramani wrote: Hi: I think this is the sort of thing you may be looking for: Context ctx = new InitialContext(); org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)ctx.lookup(java:comp /env/jdbc/yourDB)); That's definitely the sort of code to use when you want to pull things out of JNDI, but I would suggest one small change -- use the generic interface (javax.sql.DataSource) instead of hard coding the implementation class that the current version of Tomcat currently happens to use (o.a.c.d.BasicDataSource). This will protect you from future changes that Tomcat might make, or if you ever needed to move your app to a different server. Doing this, the above code would become: Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup(java:comp/env/jdbc/yourDB); For those using Tomcat, there's pretty extensive documentation on the Tomcat web site about how to set up JNDI resources (including the code snippet above :-): http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-resources -howto.html http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasourc e-examples-howto.html Regards, Geeta Craig -Original Message- From: Koon Yue Lam [mailto:[EMAIL PROTECTED] Sent: Friday, July 16, 2004 5:18 AM To: Struts Users Mailing List Subject: Re: About datasource Hi, after some thinking, I have another problem, ^^ If I am using an Action (which extent the Struts' Action class), I can get the Datasource by calling getDataSource() But now I want to write my own data assess object which shouldn't extent Action, I don't know what way I can get the datasource from the pool. any help?? Regards - 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] - 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]
Re: About datasource
^^ thanks so much ! I am going to have a try and let see what will happen (maybe somemore question, ^^) Regards - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: About datasource
Koon Yue Lam wrote: Hi ! I have successfully use struts to connect MySql but I wonder do I using the database connection pool, or just a new connection for each request?? All I did is very simple stuffs and I follow strict to the database How-To section of struts doc. How can I know I am actually using a connection pool ?? If you are using an object whose class implements javax.sql.DataSource, you are indeed using a connection pool. The number of connections in the pool is determined by your configuration properties, and the getConnection() method will reuse an existing connection from the pool (if there is one), only creating new connections when necessary -- and when the total number of connections you specify has not yet been reached. A very important consideration when using connection pools is to ensure that you *always* return the connection to the pool, even when an exception is encountered. One pattern I like to follow that ensures this is to use a try ... finally block, something like this: Connection conn = null; DataSource ds = ...; // Get a reference to your data source try { conn = ds.getConnection(); // Allocate a connection from the pool ... use the connection as necessary ... } catch (SQLException e) { ... deal with exceptions as needed ... } finally { // If you got a connection from the pool, the close() method // does *not* close the physical connection; it returns this one // to the available pool if (conn != null) { conn.close(); } } The finally block is always executed (even if an exception occurs), so this design idiom ensures that there's no way for me to forget to return a connection when I am done with it. Regards Craig McClanahan - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]