Home Duplicate key value violates unique constraint "hibernate_sequences_pkey" exception by Postgres
Reply: 1

Duplicate key value violates unique constraint "hibernate_sequences_pkey" exception by Postgres

Adam Boczek
1#
Adam Boczek Published in 2017-12-06 20:32:35Z

My Spring Data (JPA/Hibernate) based application raises on increased load when persisting new entities in the Postgres the following exception: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "hibernate_sequences_pkey"

The strategy for id is defined as follows:

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "ID")
public Long getId() {
    return id;
}

I know that this strategy is not the perfect solution for Postgres, however is it the reason for the exception above?

The load is generated using Apache Camel route with splitter running inserts in parallel on multiple threads.

Adam Boczek
2#
Adam Boczek Reply to 2017-12-07 08:30:06Z

After some tests it seems indeed that the problem is caused by the strategy = GenerationType.TABLE. With the change to strategy = GenerationType.SEQUENCE the problems described above with duplicated keys were gone.

@Id
@SequenceGenerator(name = Consts.VEHICLE_ATTR_ID_SEQ,
        sequenceName = Consts.VEHICLE_ATTR_ID_SEQ,
        allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
        generator = Consts.VEHICLE_ATTR_ID_SEQ)
@Column(name = "ID", updatable = false)
public Long getId() {
    return id;
} 
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305566 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO