Question

Somewhere i got to see a code snippet like this

// T1

for(i=0;i<n;i++) 
{
    EJBLocator.getAbcRemote().updateByIndex(i);
}

and i thought probably a code like following will do a better job

// T2

List<Integer> indexes = new XXXList<>();
for(i=0;i<n;i++) 
{
    indexes.add(i);
}
EJBLocator.getAbcRemote().updateByIndexes(indexes);

or even

// T3

AbcRemore remote = EJBLocator.getAbcRemote();    
for(i=0;i<n;i++) 
{
    remote.updateByIndex(i);
}

Just wanted to know, on a performance scale, where will these 3 approaches lies ad why.

Was it helpful?

Solution

Definitely use T2! Otherwise, each update is a separate transaction: it's slow, and it's got a risk to integrity: index numbers could have changed by some other request between iterations.

I would even take a look at whatever data reading you're performing earlier (like how are you generating n?), and moving that into the EJB method so it's all performed in the same transaction.

Generally, if you're performing database writes that are based on decisions made by an earlier database read, both the read and write should be performed in the same transaction.

This doesn't necessarily mean you need to handle both activities with the same EJB bean. The first EJB can call other EJB's, and it will all be in the same transaction (by default, you can change this however). However, your client (web application for instance) should only be calling one EJB method for a particular action.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top