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
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();