Re: DataSources
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Daniel, Daniel Blumenthal wrote: I'm looking into upgrading from 1.2.9 to 1.3.8, and I'm having a hard time figuring out how to get DataSources for my application. I understand that this was removed in 1.3, but it's unclear how to make the switch. It depends on your servlet container. You /can/ configure a JNDI datasource by hand in your own code, say, in a ServletContextInitListener, but it's often easier to have your container provide the datasource for you. For instance, Apache Tomcat makes this pretty darned easy. You can define a JNDI datasource at the server level, or per webapp. The configuration is (roughly) the same; it just goes in a different place in your config files. The documentation has a one-liner about using JNDI to get DataSources, but I can't seem to find any examples. Getting the datasource (once it exists) is very easy. Where you used to call: getDataSource(request, dataSourceId) you'll now need to do something like this: import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup(java:/comp/env/jdbc/your-datasource-name); return ds.getConnection(); Don't forget to handle null DataSources and NamingExceptions. I never use data sources in action code. I find this to be a bad practice, since I tend to think of my actions as being in the display logic layer, not in my data access layer. My data access layer is made up of a series of service classes which all inherit from a base class which defines a getConnection method which roughly does the above (and properly checks for null, NamingExceptions, etc.). Whatever do you, I would recommend against putting any JNDI-specific code into (all) your actions. If you have to keep data access in your actions, I would recommend putting a getConnection method in your actions' base class and then use that instead of Action.getDataSource(). - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFF+CeZ9CaO5/Lv0PARAibOAKCZ2ij442Vi5SFk/N8UvCcLwFrgegCeLiid H+2pTyelGuCOXP/fGdD45hg= =xQoE -END PGP SIGNATURE- - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: DataSources
Chris, Thanks for the reply - this sounds like just what I need. For instance, Apache Tomcat makes this pretty darned easy. You can define a JNDI datasource at the server level, or per webapp. The configuration is (roughly) the same; it just goes in a different place in your config files. I'm using Tomcat 5.5.x. The documentation for this looks pretty good, now that I know where to look for it. Thanks! I never use data sources in action code. I find this to be a bad practice, since I tend to think of my actions as being in the display logic layer, not in my data access layer. My data access layer is made up of a series of service classes which all inherit from a base class which defines a getConnection method which roughly does the above (and properly checks for null, NamingExceptions, etc.). This sounds very similar to what I have. The main difference seems to be that I pass the DataSource in from the action layer, instead of allocating it here. Whatever do you, I would recommend against putting any JNDI-specific code into (all) your actions. If you have to keep data access in your actions, I would recommend putting a getConnection method in your actions' base class and then use that instead of Action.getDataSource(). This is what I'm doing currently, although it would probably be worthwhile moving it down into the class mentioned above, as you suggest. Thanks again! Daniel - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: DataSources
I think this is where Spring really shines e.g. ApplicationContext.xml ?xml version=1.0 encoding=UTF-8? beans xmlns=http://www.springframework.org/schema/beans; xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance; xsi:schemaLocation=http://www.springframework.org/schema/beans/spring-beans-2.0.xsd; bean id=corporateEventDao class=com.example.JdbcCorporateEventDao property name=dataSource ref=dataSource/ /bean !-- the DataSource (parameterized for configuration via a PropertyPlaceHolderConfigurer) -- bean id=dataSource destroy-method=close class=org.apache.commons.dbcp.BasicDataSource property name=driverClassName value=${jdbc.driverClassName}/ property name=url value=${jdbc.url}/ property name=username value=${jdbc.username}/ property name=password value=${jdbc.password}/ /bean /beans and then to reference .. --- This e-mail message (including attachments, if any) is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, proprietary , confidential and exempt from disclosure. If you are not the intended recipient, you are notified that any dissemination, distribution or copying of this communication is strictly prohibited. --- Le présent message électronique (y compris les pièces qui y sont annexées, le cas échéant) s'adresse au destinataire indiqué et peut contenir des renseignements de caractère privé ou confidentiel. Si vous n'êtes pas le destinataire de ce document, nous vous signalons qu'il est strictement interdit de le diffuser, de le distribuer ou de le reproduire. - Original Message - From: Daniel Blumenthal [EMAIL PROTECTED] To: 'Struts Users Mailing List' user@struts.apache.org Sent: Wednesday, March 14, 2007 1:37 PM Subject: RE: DataSources Chris, Thanks for the reply - this sounds like just what I need. For instance, Apache Tomcat makes this pretty darned easy. You can define a JNDI datasource at the server level, or per webapp. The configuration is (roughly) the same; it just goes in a different place in your config files. I'm using Tomcat 5.5.x. The documentation for this looks pretty good, now that I know where to look for it. Thanks! I never use data sources in action code. I find this to be a bad practice, since I tend to think of my actions as being in the display logic layer, not in my data access layer. My data access layer is made up of a series of service classes which all inherit from a base class which defines a getConnection method which roughly does the above (and properly checks for null, NamingExceptions, etc.). This sounds very similar to what I have. The main difference seems to be that I pass the DataSource in from the action layer, instead of allocating it here. Whatever do you, I would recommend against putting any JNDI-specific code into (all) your actions. If you have to keep data access in your actions, I would recommend putting a getConnection method in your actions' base class and then use that instead of Action.getDataSource(). This is what I'm doing currently, although it would probably be worthwhile moving it down into the class mentioned above, as you suggest. Thanks again! Daniel - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: DataSources
Hi Dansorry..forgot the codepublic class JdbcCorporateEventDao implements CorporateEventDao { private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } // JDBC-backed implementations of the methods on the CorporateEventDao follow... }Personally I like the fact that this Threadsafe config is setup once and then (re)used over and over to multiple DAOsHTHM- This e-mail message (including attachments, if any) is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, proprietary , confidential and exempt from disclosure. If you are not the intended recipient, you are notified that any dissemination, distribution or copying of this communication is strictly prohibited. --- Le présent message électronique (y compris les pièces qui y sont annexées, le cas échéant) s'adresse au destinataire indiqué et peut contenir des renseignements de caractère privé ou confidentiel. Si vous n'êtes pas le destinataire de ce document, nous vous signalons qu'il est strictement interdit de le diffuser, de le distribuer ou de le reproduire. - Original Message - From: Daniel Blumenthal [EMAIL PROTECTED] To: 'Struts Users Mailing List' user@struts.apache.org Sent: Wednesday, March 14, 2007 1:37 PM Subject: RE: DataSources Chris, Thanks for the reply - this sounds like just what I need. For instance, Apache Tomcat makes this pretty darned easy. You can define a JNDI datasource at the server level, or per webapp. The configuration is (roughly) the same; it just goes in a different place in your config files. I'm using Tomcat 5.5.x. The documentation for this looks pretty good, now that I know where to look for it. Thanks! I never use data sources in action code. I find this to be a bad practice, since I tend to think of my actions as being in the display logic layer, not in my data access layer. My data access layer is made up of a series of service classes which all inherit from a base class which defines a getConnection method which roughly does the above (and properly checks for null, NamingExceptions, etc.). This sounds very similar to what I have. The main difference seems to be that I pass the DataSource in from the action layer, instead of allocating it here. Whatever do you, I would recommend against putting any JNDI-specific code into (all) your actions. If you have to keep data access in your actions, I would recommend putting a getConnection method in your actions' base class and then use that instead of Action.getDataSource(). This is what I'm doing currently, although it would probably be worthwhile moving it down into the class mentioned above, as you suggest. Thanks again! Daniel - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: DataSources
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Daniel, Daniel Blumenthal wrote: I'm using Tomcat 5.5.x. The documentation for this looks pretty good, now that I know where to look for it. Thanks! Feel free to cruise on over to the Tomcat list if you have any questions about configuration. There's lots of stuff in the archives if something isn't working for you. This sounds very similar to what I have. The main difference seems to be that I pass the DataSource in from the action layer, instead of allocating it here. Aah, I see. Since Struts was providing your DataSource, you simply used that interface. If you have a ton of code, you have the option of writing some wrappers that can replace the expected DataSource from Struts with another one from JNDI. You could also implement your own getDataSource method in a base action to help with the transition. Or, you could change the interface of every data service that you have ;) - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFF+FH79CaO5/Lv0PARAhkEAJ9C3rIcglGM48JNn6CWrhD68uR94wCggXMA Ny7N++jUbMVdT1DghA5CIpw= =qYDc -END PGP SIGNATURE- - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Datasources for Struts 1.2.x
Hi all, Forget this last posting. I've found the class! :) There was a problem on my archive. C.F. Scheidecker Antunes wrote: Hello all, On the Struts: How to Access a Database document the way to set up a Datasource is shown in the MySQL example. However the DataSource class type that is used is the org.apache.commons.dbcp.BasicDataSource. I've downloaded commons dbcp version 1.2.1 and there is no BasicDataSource class. Hence I wonder what to do in this case. I have placed commons-dbcp-1.2.1.jar under my lib directory along with mysql-connector-java-3.1.10-bin.jar. The MySQL connector is seen and works, however the commons-dbcp jar does not include the BasicDataSource class. What am I missing here? Thanks, C.F. - 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]