Wednesday, December 30, 2015

Apply Dynamic JDBC in ADF Application (Step-by-Step)

In some situations the business require that ADF application can connect to different database users depending on the login user. For example if user1 login, the ADF application will connect to database user hr/hr. If user2 login, the ADF application will connect to database user hr2/hr2.
This is call Dynamic JDBC.

In this url you can see how to apply this dynamic JDBC, but in my post I will simplify this url by creating a sample ADF application and go step by step for how to apply dynamic JDBC to this ADF application.

I use JDeveloper

Note: From Step1 to Step5 it just steps for creating a simple ADF application. Applying the Dynamic JDBC to this simple application is starting from Step6.

Create a simple ADF Application connect to "hr" database, this application will contains only 1 ViewObject (Employees) and will contains 2 jspx pages (login.jspx and emp.jspx)

From faces-config.xml --> Navigation Rules define outcome to the two jspx pages.
login.jspx---> login
emp.jspx----> emp

- In the login.jspx page add two inputTexts (Username and Password) and one command button for login.
- Bind Username inputText and Password inputText to the back bean.
- Create action for the login command button to read the username and password then navigate to emp.jspx page.

In the emp.jspx drag EmployeesView1 to the page and add logout link to the page 

The back bean should be:

Run login.jspx page.

Now we have created a simple ADF application and we will now apply dynamic jdbc to this application.

Open AppModule and set "Connection Type" to JDBC URL for both AppModuleShared and AppModuleLocal

In the login button action we will store username/password it in session.
The login button action will be:

Create new Filter ( extends ADFBindingFilter.

Create Class (DynamicJDBCEnvInfoProvider) as

Create Class (DynamicJDBCHttpSessionCookieImpl) as

Create Class (DynamicJDBCSessionCookieFactory) as

In web.xml update adfBindings and make it point to DynamicJDBCBindingFilter

then add filter mapping to adfBindings filter

In AppModule update jbo.ampool.sessioncookiefactoryclass in AppModuleLocal and AppModuleShare to point to DynamicJDBCSessionCookieFactory


Now run login.jspx and login with database username/password = hr/hr you will successfully login and  get the data

Logout then login again with wrong password as hr/hr1 you will get "Invalid username/password".

You can download the sample application from here

1 comment:

  1. Its excellent example .I am facing one problem. When i will use task-flow , instead of direct page (emp.jspx) . I can see only blank page .Let me know any solution.