Friday, 21 January 2011

Application Module - Optimistic Lock vs Pessimistic Lock

In Application Module the locking is by default pessimistic lock.

jbo.locking.mode is pessimistic by default.

But the practice should be optimistic locking.

How to change the locking mode.

We can easily change the locking mode by opening
"EDIT BUSINESS COMPONENT CONFIGURATION".



change the value to optimistic.

The pessimistic locking holds lock on database but optimistic does not hold lock on database.

So in case concurrent update

1> pessimistic locking - The second user for concurrent data update will stuck with error until first user saves or undo data change.
The second user will be forced to wait until first user will Save or Undo his changes in order to proceed with data commit. Another option for the second user - Undo his change in the current row and proceed with other rows editing. When the first user will close his transaction, the second one will be able to save his changes for previously locked record as well.

2> optimistic locking - The first user will not hold database lock and second user will be able to continue his work. At the same time, first user will be able to press Save button second time, and his data will be committed.

So for general optimistic locking is better to use.

By the way

There are three types of locking:
1. normal
2.optimistic
3.pessimistic



 

Thursday, 20 January 2011

Handling Stale Data

While using ADF one internal feature of ADF model is Data Caching. So for neseted application modules sometimes it makes problems like RowInconsistentException.

To remove that we can use setClearCacheOnCommit.
By default its value is false.

But if we make it true it will clear the previous cache on every commit. So the data updated by other users always get updated.

It can be done by two ways.

1. Open the ApplicationModule.xml in source mode. and set the ClearCacheOnCommit="true"


or
2. If AppModuleImpl.java is created use the following code there.

 this.getDBTransaction().setClearCacheOnCommit(true); in constructor.

This should solve the problems like Stale Data.

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.


--------------------------------------------------------------------------------------------------------