You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Katilie, John" <Jo...@softwareagusa.com> on 2008/03/03 22:08:06 UTC

Getting the URL of a DataSource

All, this may be a dumb question but I've exhausted my research and just
wondering what other users out there are doing.

I would like to get the URL for a DataSource to display for debugging
and/or
Informational reasons. I know I can get the URL after I get the
DataSource and get a connection from the connection metadata. My problem
is what if the
Connection fails? It seems without the connection I can not get the URL
for the DataSource. 

This is an application running under Tomcat 6.0.14. I guess I could get
the URL from the JMX DataSource objects created by Tomcat (I can see
them with jconsole). But is there another way? Am I missing something? I
hope this question makes sense...

Thanks for any and all comments.

Regards, John.  

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Getting the URL of a DataSource

Posted by "Katilie, John" <Jo...@softwareagusa.com>.
Phil, Thanks.. I'll take a look.

jfk

-----Original Message-----
From: Phil Steitz [mailto:phil.steitz@gmail.com] 
Sent: Tuesday, March 04, 2008 10:41 PM
To: Tomcat Users List
Subject: Re: Getting the URL of a DataSource

On Mon, Mar 3, 2008 at 2:08 PM, Katilie, John
<Jo...@softwareagusa.com> wrote:
> All, this may be a dumb question but I've exhausted my research and
just
>  wondering what other users out there are doing.
>
>  I would like to get the URL for a DataSource to display for debugging
>  and/or
>  Informational reasons. I know I can get the URL after I get the
>  DataSource and get a connection from the connection metadata. My
problem
>  is what if the
>  Connection fails? It seems without the connection I can not get the
URL
>  for the DataSource.
>
>  This is an application running under Tomcat 6.0.14. I guess I could
get
>  the URL from the JMX DataSource objects created by Tomcat (I can see
>  them with jconsole). But is there another way? Am I missing
something? I
>  hope this question makes sense...
>
I may get flamed for condoning hacking here, but there is a
too-fragile-to-depend-on way to do this for debugging / testing.

If the DataSource implementation that you are using exposes a getUrl
method, and you really want to get this (or any other extended
property) in application code, you can get the URL by specializing the
cast on your JNDI lookup and then invoking that method.  For example,
if you are using the tomcat-provided DBCP DataSource,
org.apache.tomcat.dbcp.dbcp.BasicDataSource ds  =
(org.apache.tomcat.dbcp.dbcp.BasicDataSource)
initContext.lookup("java:/comp/env/jdbc/yourDB");
String urlString = ds.getUrl();
That obiously only works if you are using BasicDataSource bundled with
Tomcat or if whatever datasource you use supports something like this
(If you are using Commons DBCP directly, just change "tomcat.dbcp" to
"commons").  It is not something you want to depend on, though, as all
Tomcat or your DS provider really promises you is a
javax.sql.DataSource.  In particular, there is no guarantee that code
such as the above will not start generating ClassCastExceptions or
ClassNotFound when the container (Tomcat) or datasource provider
release new versions.

Phil


>  Thanks for any and all comments.
>
>  Regards, John.
>
>  ---------------------------------------------------------------------
>  To start a new topic, e-mail: users@tomcat.apache.org
>  To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>  For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Getting the URL of a DataSource

Posted by "Katilie, John" <Jo...@softwareagusa.com>.
Phil, I switched over to using BasicDataSource instead of DataSource and
I'm able to get everything I need. I've tested it against 10 different
Data Bases and everything seems to work wonderfully. 

Thank you for your response and help. I hope some day I can also help
you!

Regards, jfk. 

-----Original Message-----
From: Phil Steitz [mailto:phil.steitz@gmail.com] 
Sent: Tuesday, March 04, 2008 10:41 PM
To: Tomcat Users List
Subject: Re: Getting the URL of a DataSource

On Mon, Mar 3, 2008 at 2:08 PM, Katilie, John
<Jo...@softwareagusa.com> wrote:
> All, this may be a dumb question but I've exhausted my research and
just
>  wondering what other users out there are doing.
>
>  I would like to get the URL for a DataSource to display for debugging
>  and/or
>  Informational reasons. I know I can get the URL after I get the
>  DataSource and get a connection from the connection metadata. My
problem
>  is what if the
>  Connection fails? It seems without the connection I can not get the
URL
>  for the DataSource.
>
>  This is an application running under Tomcat 6.0.14. I guess I could
get
>  the URL from the JMX DataSource objects created by Tomcat (I can see
>  them with jconsole). But is there another way? Am I missing
something? I
>  hope this question makes sense...
>
I may get flamed for condoning hacking here, but there is a
too-fragile-to-depend-on way to do this for debugging / testing.

If the DataSource implementation that you are using exposes a getUrl
method, and you really want to get this (or any other extended
property) in application code, you can get the URL by specializing the
cast on your JNDI lookup and then invoking that method.  For example,
if you are using the tomcat-provided DBCP DataSource,
org.apache.tomcat.dbcp.dbcp.BasicDataSource ds  =
(org.apache.tomcat.dbcp.dbcp.BasicDataSource)
initContext.lookup("java:/comp/env/jdbc/yourDB");
String urlString = ds.getUrl();
That obiously only works if you are using BasicDataSource bundled with
Tomcat or if whatever datasource you use supports something like this
(If you are using Commons DBCP directly, just change "tomcat.dbcp" to
"commons").  It is not something you want to depend on, though, as all
Tomcat or your DS provider really promises you is a
javax.sql.DataSource.  In particular, there is no guarantee that code
such as the above will not start generating ClassCastExceptions or
ClassNotFound when the container (Tomcat) or datasource provider
release new versions.

Phil


>  Thanks for any and all comments.
>
>  Regards, John.
>
>  ---------------------------------------------------------------------
>  To start a new topic, e-mail: users@tomcat.apache.org
>  To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>  For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Getting the URL of a DataSource

Posted by Phil Steitz <ph...@gmail.com>.
On Mon, Mar 3, 2008 at 2:08 PM, Katilie, John
<Jo...@softwareagusa.com> wrote:
> All, this may be a dumb question but I've exhausted my research and just
>  wondering what other users out there are doing.
>
>  I would like to get the URL for a DataSource to display for debugging
>  and/or
>  Informational reasons. I know I can get the URL after I get the
>  DataSource and get a connection from the connection metadata. My problem
>  is what if the
>  Connection fails? It seems without the connection I can not get the URL
>  for the DataSource.
>
>  This is an application running under Tomcat 6.0.14. I guess I could get
>  the URL from the JMX DataSource objects created by Tomcat (I can see
>  them with jconsole). But is there another way? Am I missing something? I
>  hope this question makes sense...
>
I may get flamed for condoning hacking here, but there is a
too-fragile-to-depend-on way to do this for debugging / testing.

If the DataSource implementation that you are using exposes a getUrl
method, and you really want to get this (or any other extended
property) in application code, you can get the URL by specializing the
cast on your JNDI lookup and then invoking that method.  For example,
if you are using the tomcat-provided DBCP DataSource,
org.apache.tomcat.dbcp.dbcp.BasicDataSource ds  =
(org.apache.tomcat.dbcp.dbcp.BasicDataSource)
initContext.lookup("java:/comp/env/jdbc/yourDB");
String urlString = ds.getUrl();
That obiously only works if you are using BasicDataSource bundled with
Tomcat or if whatever datasource you use supports something like this
(If you are using Commons DBCP directly, just change "tomcat.dbcp" to
"commons").  It is not something you want to depend on, though, as all
Tomcat or your DS provider really promises you is a
javax.sql.DataSource.  In particular, there is no guarantee that code
such as the above will not start generating ClassCastExceptions or
ClassNotFound when the container (Tomcat) or datasource provider
release new versions.

Phil


>  Thanks for any and all comments.
>
>  Regards, John.
>
>  ---------------------------------------------------------------------
>  To start a new topic, e-mail: users@tomcat.apache.org
>  To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>  For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org