Tuesday, February 23, 2010

Use of Dynamic JNDI

Hi Guys

This is my first Post in 2010.I came across a very useful and unusual scenario where i have to select the JNDI at run time.
Here is the way how we do it.

Scenario-
We have two database schemas, India and US each containing Employee
table. If the employee's location is india, we
want to insert it into India schema, if he works in US, the insertion should be
done in the employee table of US Schema.
The credentials, and possibly even database host name, port number for the two schema can be different. Thus we have two different JNDI entries for
these two schemas – eis/DB/US and eis/DB/India. But, for inserting into these
different databases, we want to use a single Database adapter. Normally, the JNDI on
which the adapter is based is specified during design time. So we want to change the
JNDI value for the adapter at runtime, based on whether the employee is of US or India.

Please make sure that the two JNDIs (eis/DB/US and eis/DB/India)are created in the application server pointing to their respective databases.

After doing this , create a simple BPEL project which has a single invoke to the database.Make sure you have a switch activity which have a condition on employees location.Based on the employee's location assign its respective JNDI and then invoke the DB Adapter.

Now the question arrises how you will assign the JNDI?
You need to add the below xml fragment in your assign activity and assign it to the partner link(in our case it is DB adapter).

<EndpointReference
xmlns="http://schemas.xmlsoap.org/ws/2003/03/addressing">
<Address>eis/DB/India</Address>
</EndpointReference>


Once you complete it,just deploy the code and test it.
It works.

Cheers.