0
votes

Is there some way for a JavaEE application to get it's DataSource without knowing it's JNDI name if application has a single datasource?

I want to decouple application from the name of it's DataSource to avoid hardcoding JNDI name of the DataSource in the code (no matter in @Resource(name="") or in code that works with Contexts). Should i do this at all or it's normal practice to hardcode JNDI name of the datasource because coupling of DataSource with JDBC connection pool is described in web.xml and this gives all necessary flexibility?

Thanks in advance.

1

1 Answers

1
votes

You could use some sort of dependency injection framework to make the lookup completely transparent to the application, but yes, using JNDI instead of having the application connect to the database directly is supposed to already give you "the necessary flexibility". Note that you can put the JNDI name in a configuration file, too.