Use Server’s Data Source In AM Instead Of Application Connection DataSource

Using Data Source instead of normal JDBC connection has lot of advantages. Major one is Data Source behaves dynamically. One can change the Data Source’s Configurations also. While the deployed application will not be affected if Data Base Objects do not get altered.

But one problem I have found in industry is that how to configure different data sources in application module(Root Application Module) .

Normally for testing purpose we do not use Data Sources we use application modules jdbc connection.

To configure data sources we can choose two types of Data Sources(Based on Authentication)

1. Application
2. Container

As we are discussing here about using server’s Data Source I will be going with Container based one.

To configure this I suppose I will be using “HRDataSource” as Data Source Name.

First I shall edit the Application Module Configuration to refer HRDataSource.

To do that I should follow these steps:

1. Right Click On AppliationModule.xml(MSService.xml in picture).
2. Click Configurations – It will open Manage Configurations wizard.
3. Now click on edit – and edit the DataSource as java:comp/env/jdbc/DataSourceName

Click save all

Now we have added the DataSource in AM. But if we deploy the application in server we may still get errors though jdbc/HRDataSource exists in server.
We have to configure the authentication to be of Container.
To do that we should edit the web.xml of View Controller project.
We should one Data Source Reference there of Authentication Container.
The configuration should be like below image. Data Source is of javax.sql.DataSource type.
Now if we deploy the application it should take HRDataSource for Data Base connection.
So If you need to modify the DataSource name , just modify bc4j.xcfg located in common folder and web.xml located within WEB-INF folder.
Here the Data Source is found out using context lookup.
as like below.

Context envCtx = (Context) initCtx.lookup(“java:comp/env”);

// Look up our data source
DataSource ds = (DataSource)envCtx.lookup(“jdbc/HRDataSource”);

// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
… use this connection to access the database …
conn.close();

——————————————————————————————————-
The Data Source can be configured by directly and only in bc4j.xcfg file only. Without mentioning any reference in web.xml
just put the jndi name of the Servers Data Source in ‘Custom JDBCDataSource=”jdbc/HRDataSource” />’ in bc4j.xcfg file deploy it in server.
It will run as expected.
——————————————————————————————————–