cancel
Showing results for 
Search instead for 
Did you mean: 

Entity bean - getting Duplicate Key Exception on ejbCreate

Former Member
0 Kudos

Hello Guys,

In one of the webdynpro applications, we had to migrate large amount of data from the other source and so we had over 5000 records in the one of the table. After the data is migrated and application is deployed, I thought it was all ok, but it was the data in between, but not at the end as you would expect. And when it could no longer insert in between, it started throwing the “Duplicate Key Exception” on ejbCreate method.

The table are created using dictionary DC. We are using SQL Server as the DB and the particular table has the primary key as big int.

Do I have to set the "Interval for Generated Primary keys' option in the ejb-j2ee-engine.xml?

Have you seen like this before? Am I missing something here? Any help would be much appreciated.

Regards,

Muthaiah

Edited by: Muthaiah Ramanathan on Jan 7, 2008 12:34 AM

Accepted Solutions (1)

Accepted Solutions (1)

Vlado
Advisor
Advisor
0 Kudos

Hi Muthaiah,

DuplicateKeyException indicates that you are trying to insert a record into a DB table for which another record with the same PK already exists. This is caused by application misbehavior, so check your application logic. You have to avoid calling the entity bean's create method more than once for the same PK.

HTH!

\-- Vladimir

Former Member
0 Kudos

Thanks Vladimir. I'm using a unknown primary key here. So I don't know the primary key when calling ejbCreate method.

Regards,

Muthaiah

Edited by: Muthaiah Ramanathan on Jan 10, 2008 5:16 AM

Former Member
0 Kudos

Hi,

It looks like you have auto generate key field in your DB. And DB generates primary keys for you.

You told you had a database migration. Please check the starting number in your auto generate key field in the new database. I think it overlaps with the current record IDs.

Regards

Pavel

Former Member
0 Kudos

Yes, Pavel I had to do the Data migration. But it seems the primary key counter is initialized only during the application is started for the first time.

Before migrating the data I did a test to check if everything was ok and that set the primary key counter in the ejb engine I assume. Then I did the data migration and had few thousand records migrated. But the primary key counter was never refreshed.

Regards,

Muthaiah

Vlado
Advisor
Advisor
0 Kudos

For the record (as this issue has been processed also through the regular SAP support channel):

Indeed, the primary key counter is initialized when create() method is invoked for the first time. Its initial value is (max value of the corresponding column in the DB) + 1. From this point on the EJB Container requires exclusive access for updates to the entity bean's DB table. However, in this case it turned out you've already created some entries through the entity bean and then did the data migration, which resulted in DuplicateKeyException at the next call to ejbCreate().

Regards,

\-- Vladimir

Answers (0)