How to make hibernate commit fast and efficient?












0















I have 1000 threads and there task is to increase the value by 1 update to the database . So for eg :
Initally my database has the value 0, first thread come it increases the value to 1 , then 2nd thread come it increases that value to 2 , then 3rd come it increases the value to 3 and so on till 1000 threads , so at the end when 1000th thread come it changes the value to 1000 from 999.



I have written the code and the code is working perfectly but the problem is it is taking more than a minute to update those values ,so how to minimize that time so that 1000 threads can update it in least time .



public String increaseAndUpdateNumber() {
Session session = getSession();
try {
synchronized (this) {
int number = dao.getNumberdao().getNumberCount(session);
Number numberObj = dao.getNumberdao().getByNumber(session,number);

//begin of transaction
Transaction numberTx = session.beginTransaction();
number+=1;
System.out.println("number is :"+number+" numberObj :"+numberObj);
numberObj.setNumber(number);
dao.getNumberdao().update(session, numberObj);
numberTx.commit();
//end of transaction
}
return "success";
}catch(Exception e){
e.printStackTrace();
}finally {
session.close();
}
return null;
}


Act these 1000 threads in the least time possible.










share|improve this question

























  • The problem is that the database updates must happen one after the other to reflect serial increment of the number. This will be a choking point for the 1000 threads. If speed is so important, then you may have to create a custom data layer that replicates the relevant database tables in memory as objects and only persists changes back to the database periodically.

    – Shadow
    Dec 30 '18 at 8:10
















0















I have 1000 threads and there task is to increase the value by 1 update to the database . So for eg :
Initally my database has the value 0, first thread come it increases the value to 1 , then 2nd thread come it increases that value to 2 , then 3rd come it increases the value to 3 and so on till 1000 threads , so at the end when 1000th thread come it changes the value to 1000 from 999.



I have written the code and the code is working perfectly but the problem is it is taking more than a minute to update those values ,so how to minimize that time so that 1000 threads can update it in least time .



public String increaseAndUpdateNumber() {
Session session = getSession();
try {
synchronized (this) {
int number = dao.getNumberdao().getNumberCount(session);
Number numberObj = dao.getNumberdao().getByNumber(session,number);

//begin of transaction
Transaction numberTx = session.beginTransaction();
number+=1;
System.out.println("number is :"+number+" numberObj :"+numberObj);
numberObj.setNumber(number);
dao.getNumberdao().update(session, numberObj);
numberTx.commit();
//end of transaction
}
return "success";
}catch(Exception e){
e.printStackTrace();
}finally {
session.close();
}
return null;
}


Act these 1000 threads in the least time possible.










share|improve this question

























  • The problem is that the database updates must happen one after the other to reflect serial increment of the number. This will be a choking point for the 1000 threads. If speed is so important, then you may have to create a custom data layer that replicates the relevant database tables in memory as objects and only persists changes back to the database periodically.

    – Shadow
    Dec 30 '18 at 8:10














0












0








0








I have 1000 threads and there task is to increase the value by 1 update to the database . So for eg :
Initally my database has the value 0, first thread come it increases the value to 1 , then 2nd thread come it increases that value to 2 , then 3rd come it increases the value to 3 and so on till 1000 threads , so at the end when 1000th thread come it changes the value to 1000 from 999.



I have written the code and the code is working perfectly but the problem is it is taking more than a minute to update those values ,so how to minimize that time so that 1000 threads can update it in least time .



public String increaseAndUpdateNumber() {
Session session = getSession();
try {
synchronized (this) {
int number = dao.getNumberdao().getNumberCount(session);
Number numberObj = dao.getNumberdao().getByNumber(session,number);

//begin of transaction
Transaction numberTx = session.beginTransaction();
number+=1;
System.out.println("number is :"+number+" numberObj :"+numberObj);
numberObj.setNumber(number);
dao.getNumberdao().update(session, numberObj);
numberTx.commit();
//end of transaction
}
return "success";
}catch(Exception e){
e.printStackTrace();
}finally {
session.close();
}
return null;
}


Act these 1000 threads in the least time possible.










share|improve this question
















I have 1000 threads and there task is to increase the value by 1 update to the database . So for eg :
Initally my database has the value 0, first thread come it increases the value to 1 , then 2nd thread come it increases that value to 2 , then 3rd come it increases the value to 3 and so on till 1000 threads , so at the end when 1000th thread come it changes the value to 1000 from 999.



I have written the code and the code is working perfectly but the problem is it is taking more than a minute to update those values ,so how to minimize that time so that 1000 threads can update it in least time .



public String increaseAndUpdateNumber() {
Session session = getSession();
try {
synchronized (this) {
int number = dao.getNumberdao().getNumberCount(session);
Number numberObj = dao.getNumberdao().getByNumber(session,number);

//begin of transaction
Transaction numberTx = session.beginTransaction();
number+=1;
System.out.println("number is :"+number+" numberObj :"+numberObj);
numberObj.setNumber(number);
dao.getNumberdao().update(session, numberObj);
numberTx.commit();
//end of transaction
}
return "success";
}catch(Exception e){
e.printStackTrace();
}finally {
session.close();
}
return null;
}


Act these 1000 threads in the least time possible.







java multithreading hibernate spring-mvc synchronization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 6:10







Neeraj

















asked Dec 30 '18 at 5:00









NeerajNeeraj

83




83













  • The problem is that the database updates must happen one after the other to reflect serial increment of the number. This will be a choking point for the 1000 threads. If speed is so important, then you may have to create a custom data layer that replicates the relevant database tables in memory as objects and only persists changes back to the database periodically.

    – Shadow
    Dec 30 '18 at 8:10



















  • The problem is that the database updates must happen one after the other to reflect serial increment of the number. This will be a choking point for the 1000 threads. If speed is so important, then you may have to create a custom data layer that replicates the relevant database tables in memory as objects and only persists changes back to the database periodically.

    – Shadow
    Dec 30 '18 at 8:10

















The problem is that the database updates must happen one after the other to reflect serial increment of the number. This will be a choking point for the 1000 threads. If speed is so important, then you may have to create a custom data layer that replicates the relevant database tables in memory as objects and only persists changes back to the database periodically.

– Shadow
Dec 30 '18 at 8:10





The problem is that the database updates must happen one after the other to reflect serial increment of the number. This will be a choking point for the 1000 threads. If speed is so important, then you may have to create a custom data layer that replicates the relevant database tables in memory as objects and only persists changes back to the database periodically.

– Shadow
Dec 30 '18 at 8:10












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%2f53975381%2fhow-to-make-hibernate-commit-fast-and-efficient%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%2f53975381%2fhow-to-make-hibernate-commit-fast-and-efficient%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas