Skip to main content

@GeneratedValue(strategy = GenerationType.AUTO) throws exception

10 replies [Last post]
server_user
Offline
Joined: 2007-08-06

I have used @GeneratedValue(strategy = GenerationType.AUTO) to generate primary key id (netbeans default type - 'long'). Sometimes, I am getting the following exception:

EJB5071: Some remote or transactional roll back exception occurred
Local Exception Stack:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2006.8 (Build 060830)): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:
Duplicate entry '1473486' for key 1Error Code: 1062

I have also tried TableGenerator. But getting same exception after a while for one or two transactions.

@TableGenerator(name="test_table", table="ID_GEN", pkColumnName="ID_NAME", valueColumnName="ID_VAL", pkColumnValue="TEST_TABLE_ID", allocationSize=1)

Database: MYSQL 5.0.22
OS: Ubuntu server 6.01
App server: Sun Java System Application Server Platform Edition 9.0_01

Couldn't trace what's causing this exception as it doesn't happen all the time or for all transactions. Anyone facing this problem? any workaround?

THANKS in advance.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mvatkina
Offline
Joined: 2005-04-04

What is the type of the id field? How many instances are you trying to create?

thanks,
-marina

server_user
Offline
Joined: 2007-08-06

Well, the id type is "long" (bigint(20) in database). I am running a loop to create the instances (lets say, 10 to 20 instances).

for(int i=0; i<20; i++) {
createing new object;
em.persist();
}

[and then deleting records as well]
em.createNativeQuery ("DELETE FROM table WHERE column2 > 100").executeUpdate();

mvatkina
Offline
Joined: 2005-04-04

Can you try to see if a) calling em.remove() on them or b) using bulk delete of JPQL instead makes a difference? Are you closing em after each portion? If not, you are adding and adding instances to the em's cache. And you are for sure increasing the contents of the emf's cache - executing a native query makes em unaware of the changes.

If the above changes solve the problem, it can be a TopLink Essentials bug, if not, I suggest to try a jdbc test to simulate your situation to check if it's not a bug in MySQL.

thanks,
-marina

jessevanhouten
Offline
Joined: 2008-04-02

Hi,

We've had a similar problem (duplicate keys under low stress test). It was solved by switching all tables to InnoDB engine.

Our problems occured when using pure MyIsam (default on *nix) or combinations of MyIsam and InnoDB. The issue was only resolved when using pure InnoDB.

Obviously InnoDB and MyIsam have other advantages/weaknesses. But because of this, we now feel forced to only use InnoDB.

Hope this helps.

Regards,
Jesse van Houten

mvatkina
Offline
Joined: 2005-04-04

Can it be a MySQL error? Some time back they had poor transactional support. Can you try on some other database and see if you can reproduce the problem?

thanks,
-marina

server_user
Offline
Joined: 2007-08-06

just wondering how does GenerationType.AUTO algorithm works. Shouldn't it take care of duplicate entry? who generates the ID in this case - the database or the app server (EJB3)?

In my case, database tables are created by the app server when I deploy the application. I have checked the database table structure manually and there is no attribute defined to auto increment ID value.

Thanks.

mvatkina
Offline
Joined: 2005-04-04

GenerationType.AUTO means the persistence provider is free to choose the strategy. If you are using TopLink Essentials which is the default in GlassFish, this strategy maps to GenerationType.TABLE (there is a way to make it a different type by setting some parameters).

Id doesn't matter which GenerationType you choose, the end result should be a unique key value.

Regards,
-marina

server_user
Offline
Joined: 2007-08-06

Thanks for your reply. Well, That's what i believe too. But how come it's creating duplicate id's. It's throwing exception on a load test (lets say max 5 to 10 users). However, not all the time but sometimes (approx. 5%).

Thanks.

Error msg:

Some remote or transactional roll back exception occurred
Local Exception Stack:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2006.8 (Build 060830)): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:
Duplicate entry '3815473' for key 1Error Code: 1062

gyorke
Offline
Joined: 2005-06-21

What is your code doing? Is it only persisting new objects or are there other operations like delete? Is it possible the error is occurring from other code?
--Gordon

server_user
Offline
Joined: 2007-08-06

My code is performing insert and delete operations but one after another and this shouldn't cause any problem. The exception is a "duplicate key entry exception" and my code doesn't create primary key (id) manually. I tried AUTO and TABLE mode but still having same problem.

Thanks.