Provide one datasource with two different JNDI names in JBoss

While testing your JBoss service it can be very handy to use DefaultDS as your data source. However you will probably not want to hardcode this JNDI name into your code, jboss-server.xml, web.xml or persistence.xml (depending on service you write). There you want to use your own "MyAppDS" JNDI name. So wouldn't it be sweet to be able to bind two different JNDI names to the same datasource ?

It is possible and quite simple however I could not really find any good example while Googling the problem. So here is a short description how I solved my problem.

The problem

In a SLSB I'm using JPA to access the database. My persistance.xml has the following defined:


So how do I use the already defined DefaultDS without having to change the persistance.xml


The solution is to define a LinkRef from one JNDI name to another. In JBoss this can be done using NamingAlias.

Create your own custome my-ds.xml and place it in the deploy directory.

<?xml version="1.0" encoding="UTF-8"?>


and your done.