-----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]