Jacob
Jacob

Reputation: 389

For logging information, want the url used for connection of JNDI

I create a data source using standard below:

private DataSource ds;      
Context envContext = (Context) initContext.lookup("java:/comp/env");
ds = (DataSource)envContext.lookup("jdbc/myDB");

I would want the connection url written to log file.

System.out.println(ds);

The above will not due. I thought about casting to BasicDataSource

(ds instanceof BasicDataSource);

to get information since BDS has getUrl() method. This goes against indirection, since not all connections will be BasicDataSource and should be avoided? How should one print the url used for a datasource, at least for debugging?

Upvotes: 1

Views: 639

Answers (1)

Susannah Potts
Susannah Potts

Reputation: 827

BasicDataSource and OracleDataSource (and probably other wrappers) are really the only ways to get URL information directly from the DataSource. But as gonzo described in the comments, you can get this information from your connection:

Connection conn = ds.getConnection();
String url = conn.getMetaData().getURL();

Upvotes: 2

Related Questions