0

When i try to save via swagger,It gives those errors:

INTERNAL_SERVER_ERROR(PERSISTENCE_DATAACCESS_EXCEPTION) - DataIntegrityViolationException : could not execute statement; SQL [n/a]; constraint [UK_8q03as5by8j5kvtroc1dyl0lo]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute

My class is this:

public class Book{
    @Id
    private long id;

    @NotNull
    @Column(unique = true)
    @Size(min = 7, max = 7)

    private String referenceCode;;//this field makes the error because of not unique value creation

That referenceCode is also unique. I am keeping the value in another table. And it has another service to save and increment:

 @Override
    public String generateReference(String productCode) {

        ReferenceGenerator referenceGenerator = referenceGeneratorRepository.findByMnemonic(productCode);

        if (referenceGenerator != null) {
            referenceGenerator.setValue(incrementReference(referenceGenerator.getValue()));
            referenceGeneratorRepository.save(referenceGenerator);
        }
        else {
            referenceGenerator = referenceGeneratorRepository.save(new ReferenceGenerator(PREAPP, START_VALUE_FOR_REFERENCE_CODE));
        }

        return PREFIX_OF_REFERENCE_CODE + referenceGenerator.getValue();
    }

    private String incrementReference(String value) {

        return Long.toString( Long.parseLong(value, 36) + 1, 36).toUpperCase();
    }
}

In my main service, while saving, it uses this:

    public class Bookservice{    

public void createBook(){
book.setReferenceCode(referenceGeneratorService.generateReference("BOOK));
                BookApplication savedBook = bookRepository.save(book);

On swagger, when i sent lots of requests to save, it can sometimes fail to save. Because of duplication and uniqueness. Concurrency

How can i prevent this?

Lock or something? I dont want to lose data also.

I can use pessimist lock but maybe there are other ways? Also i can change the method that makes it unique but i want to keep it.

Like this https://stackoverflow.com/a/53259441/10309977

i put synchronized to the method but i can again get the errors.

@Override
    public synchronized String generateReference(String productCode) {

I put here.

Maybe i should put to BookService.java class?

asdasasd
  • 81
  • 1
  • 9
  • 1
    This is the reason you generally don't want to use table based increments. To solve this you would need to lock the table for the duration of the transaction. This will however impact your throughput as you now can only serve 1 transaction at a time. Adding `synchronize` won't help as you would need synchronize the whole `createBook` method. – M. Deinum Nov 15 '18 at 12:40
  • @M.Deinum so if i put`synchronized` to both `generateReference` and `vreateBook`, it works? – asdasasd Nov 15 '18 at 12:55
  • No you only need to synchronize `createBook` however that will severely impact your performance and work with a single instance of your application. As soon as you deploy 2 instances it won't work anymore. – M. Deinum Nov 15 '18 at 13:59
  • @M.Deinum so, for 2 instances work, what can i do? Any thing like `pessimist lock`? Why cant i lock the database, so another instance can not write to it? – asdasasd Nov 15 '18 at 14:14
  • You could but that would severely impact your performance. As you will lock everything down. – M. Deinum Nov 15 '18 at 16:25
  • @M.Deinum what is your suggestion – asdasasd Nov 15 '18 at 22:30
  • Use proper transactions for that or stop using table driven increments and let the database decide using a sequence for instance. – M. Deinum Nov 15 '18 at 23:04
  • @M.Deinum but it is non-id. Need custom sequence . – asdasasd Nov 16 '18 at 19:40
  • And what has given you the impression that you can only use a sequence for id fields? – M. Deinum Nov 17 '18 at 15:31
  • @M.Deinum i also need prefix + unique alphanumeric. So, i searched and could not see way https://stackoverflow.com/a/53259441/10309977 example – asdasasd Nov 18 '18 at 14:49
  • See http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#mapping-database-generated-value and https://discourse.hibernate.org/t/using-a-oracle-sequence-on-non-primary-key/541/8 . For generating values on non-id fields. You could even create your own annotation using this for specifing the prefix. – M. Deinum Nov 19 '18 at 07:25
  • https://stackoverflow.com/a/536102/10646656 `It seems that Hibernate/JPA isn't able to automatically create a value for your non-id-properties. The @GeneratedValue annotation is only used in conjunction with @Id to create auto-numbers.` So, what you posted are useless for my situation unfortunately. I need to do something in the URL i provided. But it did not work for me. – asdasasd Nov 19 '18 at 13:47
  • Read, please read instead of just judging. One of the hibernate maintainers answered the thread on discourse WITHOUT an id field. So yes the `@GeneratedValue` only works on an `@Id` field, the answer I linked you to is a solution WITHOUT `@GeneratedValue`. – M. Deinum Nov 19 '18 at 14:12
  • @M.Deinum sorry. I did not see. I think you mean `unique` creation of date field. With `@ValueGenerationType(generatedBy = FunctionCreationValueGeneration.class) @Retention(RetentionPolicy.RUNTIME)` i first time saw this. I will try this. – asdasasd Nov 19 '18 at 14:35

0 Answers0