So, when you want to deploy a new web app in your server, you usually do the following steps:
- Declare a new Datasource (or XA-Datasource) in the server wich points to the database
- Configure the web app to lookup the JNDI entry in which you declare the Datasource
Cool. But you may find yourself in some situations when this may not be the best solution. I´m thinking in two scenarios:
- Multiple web apps access to the same database
- One web app wishes to switch between databases in the same environment
The solution is to use JNDI aliases. This allows you to use one JNDI entry which, in turns, returns the value of another JNDI, just like a proxy.
As JBoss doc states (https://docs.jboss.org/author/display/AS71/JNDI+Reference), three different types of bindings are supported:
- Simple - A primitive or string.
- ObjectFactory - This allows to specify the javax.naming.spi.ObjectFactory that is used to create the looked up value.
- Lookup - This allows to create JNDI aliases. When this entry is looked up, it will lookup the target and return the result.
This is an example of use of the lookup binding type:
/subsystem=naming/binding="java:/alias/datasource":add(binding-type=lookup, lookup="java:jboss/datasources/pruebas") /subsystem=naming/binding="java:/alias/datasource":read-attribute(name=lookup) (...) /subsystem=naming/binding="java:/alias/datasource":write-attribute(name=lookup,value="java:jboss/datasources/pruebas2") :reload
Here I assume that there are two datasources previously created, accesibles in java:jboss/datasources/pruebas2 and java:jboss/datasources/pruebas2. First I created a new JNDI which points to the first one and then update its value to the second one.
Now I will analyze more in deep the two scenarios presented.
Multiples web apps access to the same database
Suppose you have have multiple web apps who need to access to the same database (yes, this is not a good architecture, but imagine that for some legacy reasons they have to). In the future each app would access to its own database, so you configure them to use a different datasource.What you would do is to create multiple datasources in your server, all of them pointing to the same database. But if the database location changes, for example the IT department decides to move them to a new server, you must to reconfigure all datasources to point to this new location.
By using multiple JNDI entries with binding lookup type you can configure just one datasource and reference it from multiple JNDI keys.
One web app wishes to switch between databases in the same environment
The other scenario could be when you have a single web app and multiple database environments. For example, you have two databases in your Test environment and you want to switch between then in order to make different tests. Again, the best solution is to have multiple full environments and deploy your app in each one, but this is not a ideal world.
What you would do is to create multiple datasources in your server, each one pointing to one different database. But you also need to reconfigure your web app each time you want to switch between them.
By using a single JNDI entry with binding lookup type you can configure your app to pick the datasource from it, and internally reference the desired datasource.