Krunal,
 
1) you can try "select for update" when you get your max() + 1 . That will lock the record until your transaction commits.
The plus is you won't have duplicate values. The minus is your transactions will be serialized.
 
2) i assume you are not using entity beans otherwise I'd suggest a sequence blocks pattern described chapter 5 of "EJB Design Patterns" by Floyd Marinescu (available free in PDF format of www.the serverside.com) so create another session bean, inside the bean start another transaction, retrieve/update the key within that transaction. It will be be very short all it should is read and up[date the key value and return it.
If you have just one server and not a cluster, the easiest solution would be to read the index on start up and bind it inside a class into JNDI tree where the class would look like this
public class IndexCounter{
    private int index;
    ...
    public int getIndex(){
        return index++;
    }
}
 
You even don't have to synchronize the getter for it's impossible to get a duplicate in this case.
 
----- Original Message -----
To: JDJList
Sent: Thursday, May 30, 2002 4:07 AM
Subject: [jdjlist] HELP REQUIRED : COMPLEX TRANSACTIONS

Dear All,
 
I am using ejb (statelss session bean), and using bean managed transaction processing.
I have one method which includes inserts in one table (min. 8 rows) and then select
statements and one select includes max() +1 value of one column from table, using that
value makes insert into one table say 'A'. finally at the end of method, commiting the transaction.
 
The database data for table 'A' is becoming inconsistent as many users are firing the same method
and 2 or 3 users get same max+1 value as insert is done for that value.
 
I can not create a sequence due to some business function limitation.
 
One solution that we are thinking of is :
On server, create one class, having sync. get and set methods using one flag  and in ejb
check that flag and then get max+1 value in order to control max+1 value conflict.
 
Is this solution right ?
 
Please mail me the solutions that u all can provide me as soon as possible on JDJ List.

Thanks & Regards,
Krunal Shimpi.
Passenger Solutions Group,
Airlines division,
Kale Consultants Ltd.

Tel : 8259580 - 89  Extn : 322

http://www.kaleconsultants.com

Work is worship.

To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to