Lock repository to let write only one process












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?










share|improve this question


















  • 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 putsynchronized 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
















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?










share|improve this question


















  • 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 putsynchronized 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














0












0








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?










share|improve this question














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?







hibernate spring-boot jpa






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 12:32









asdasasdasdasasd

237




237








  • 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 putsynchronized 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














  • 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 putsynchronized 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








1




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





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 putsynchronized to both generateReference and vreateBook, it works?

– asdasasd
Nov 15 '18 at 12:55





@M.Deinum so if i putsynchronized 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





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







@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





You could but that would severely impact your performance. As you will lock everything down.

– M. Deinum
Nov 15 '18 at 16:25












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319595%2flock-repository-to-let-write-only-one-process%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319595%2flock-repository-to-let-write-only-one-process%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine