Is it a bad practice to switch threads within a transaction?












-1















I wonder if switching threads within a transaction is simply asking for troubles?



Since following code is blocking one thread and doing things in other threads, it seems a waste, there must be better way than this, any advice or idea are welcome. Thanks in advance.



I am trying to load Something from database and save it after made some changes:



public Mono<SomeThing> updateSomething(int i) {
TransactionStatus tx = openTx();
return Mono.just(i)
.publishOn(workerThread)
.flatMap(this::loadSomethingFromDbById)
.map(Something::doSomeChange)
.flatMap(this::saveSomethingToDb)
.publishOn(Schedulers.immediate())
.doFinally(s -> closeTx(s));
}

public void blockingUpdateSomething() {
updateSomething(1).block();
}



  1. openTx/closeTx() are simple wrappers of PlatformTransactionManager, and .block() is used to keep this thread from other Transactions, can they work correctly?

  2. Mono<> is used to switch threads, which limits concurrent calls to the spring-data's JpaRepository, any better ways doing the limit?


Update:



Although the code could be more conventional if run in one thread synchronously, there might be a problem when there are lots of such threads.



Let's say 1000 threads do all jobs in their own thread, they might cause high cpu contentions.



Instead, if we offload jobs to limited threads, leaving those 1000 threads waiting for some result, the cpu contention should be less.



So, I might prefer the original thread dancing code, if transaction works well in that.










share|improve this question

























  • Feels like a very, very bad idea. I'd wonder why you're even asking.

    – duffymo
    Nov 16 '18 at 1:48











  • So, I guess I should never switch thread within transactions then? I'm asking because I wonder how to do transaction right, sorry if my code is too far away from that.

    – Slacker
    Nov 16 '18 at 2:19











  • Do it right? Don't switch threads.

    – duffymo
    Nov 16 '18 at 10:26
















-1















I wonder if switching threads within a transaction is simply asking for troubles?



Since following code is blocking one thread and doing things in other threads, it seems a waste, there must be better way than this, any advice or idea are welcome. Thanks in advance.



I am trying to load Something from database and save it after made some changes:



public Mono<SomeThing> updateSomething(int i) {
TransactionStatus tx = openTx();
return Mono.just(i)
.publishOn(workerThread)
.flatMap(this::loadSomethingFromDbById)
.map(Something::doSomeChange)
.flatMap(this::saveSomethingToDb)
.publishOn(Schedulers.immediate())
.doFinally(s -> closeTx(s));
}

public void blockingUpdateSomething() {
updateSomething(1).block();
}



  1. openTx/closeTx() are simple wrappers of PlatformTransactionManager, and .block() is used to keep this thread from other Transactions, can they work correctly?

  2. Mono<> is used to switch threads, which limits concurrent calls to the spring-data's JpaRepository, any better ways doing the limit?


Update:



Although the code could be more conventional if run in one thread synchronously, there might be a problem when there are lots of such threads.



Let's say 1000 threads do all jobs in their own thread, they might cause high cpu contentions.



Instead, if we offload jobs to limited threads, leaving those 1000 threads waiting for some result, the cpu contention should be less.



So, I might prefer the original thread dancing code, if transaction works well in that.










share|improve this question

























  • Feels like a very, very bad idea. I'd wonder why you're even asking.

    – duffymo
    Nov 16 '18 at 1:48











  • So, I guess I should never switch thread within transactions then? I'm asking because I wonder how to do transaction right, sorry if my code is too far away from that.

    – Slacker
    Nov 16 '18 at 2:19











  • Do it right? Don't switch threads.

    – duffymo
    Nov 16 '18 at 10:26














-1












-1








-1








I wonder if switching threads within a transaction is simply asking for troubles?



Since following code is blocking one thread and doing things in other threads, it seems a waste, there must be better way than this, any advice or idea are welcome. Thanks in advance.



I am trying to load Something from database and save it after made some changes:



public Mono<SomeThing> updateSomething(int i) {
TransactionStatus tx = openTx();
return Mono.just(i)
.publishOn(workerThread)
.flatMap(this::loadSomethingFromDbById)
.map(Something::doSomeChange)
.flatMap(this::saveSomethingToDb)
.publishOn(Schedulers.immediate())
.doFinally(s -> closeTx(s));
}

public void blockingUpdateSomething() {
updateSomething(1).block();
}



  1. openTx/closeTx() are simple wrappers of PlatformTransactionManager, and .block() is used to keep this thread from other Transactions, can they work correctly?

  2. Mono<> is used to switch threads, which limits concurrent calls to the spring-data's JpaRepository, any better ways doing the limit?


Update:



Although the code could be more conventional if run in one thread synchronously, there might be a problem when there are lots of such threads.



Let's say 1000 threads do all jobs in their own thread, they might cause high cpu contentions.



Instead, if we offload jobs to limited threads, leaving those 1000 threads waiting for some result, the cpu contention should be less.



So, I might prefer the original thread dancing code, if transaction works well in that.










share|improve this question
















I wonder if switching threads within a transaction is simply asking for troubles?



Since following code is blocking one thread and doing things in other threads, it seems a waste, there must be better way than this, any advice or idea are welcome. Thanks in advance.



I am trying to load Something from database and save it after made some changes:



public Mono<SomeThing> updateSomething(int i) {
TransactionStatus tx = openTx();
return Mono.just(i)
.publishOn(workerThread)
.flatMap(this::loadSomethingFromDbById)
.map(Something::doSomeChange)
.flatMap(this::saveSomethingToDb)
.publishOn(Schedulers.immediate())
.doFinally(s -> closeTx(s));
}

public void blockingUpdateSomething() {
updateSomething(1).block();
}



  1. openTx/closeTx() are simple wrappers of PlatformTransactionManager, and .block() is used to keep this thread from other Transactions, can they work correctly?

  2. Mono<> is used to switch threads, which limits concurrent calls to the spring-data's JpaRepository, any better ways doing the limit?


Update:



Although the code could be more conventional if run in one thread synchronously, there might be a problem when there are lots of such threads.



Let's say 1000 threads do all jobs in their own thread, they might cause high cpu contentions.



Instead, if we offload jobs to limited threads, leaving those 1000 threads waiting for some result, the cpu contention should be less.



So, I might prefer the original thread dancing code, if transaction works well in that.







java spring-transactions project-reactor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 7:31







Slacker

















asked Nov 16 '18 at 1:20









SlackerSlacker

558




558













  • Feels like a very, very bad idea. I'd wonder why you're even asking.

    – duffymo
    Nov 16 '18 at 1:48











  • So, I guess I should never switch thread within transactions then? I'm asking because I wonder how to do transaction right, sorry if my code is too far away from that.

    – Slacker
    Nov 16 '18 at 2:19











  • Do it right? Don't switch threads.

    – duffymo
    Nov 16 '18 at 10:26



















  • Feels like a very, very bad idea. I'd wonder why you're even asking.

    – duffymo
    Nov 16 '18 at 1:48











  • So, I guess I should never switch thread within transactions then? I'm asking because I wonder how to do transaction right, sorry if my code is too far away from that.

    – Slacker
    Nov 16 '18 at 2:19











  • Do it right? Don't switch threads.

    – duffymo
    Nov 16 '18 at 10:26

















Feels like a very, very bad idea. I'd wonder why you're even asking.

– duffymo
Nov 16 '18 at 1:48





Feels like a very, very bad idea. I'd wonder why you're even asking.

– duffymo
Nov 16 '18 at 1:48













So, I guess I should never switch thread within transactions then? I'm asking because I wonder how to do transaction right, sorry if my code is too far away from that.

– Slacker
Nov 16 '18 at 2:19





So, I guess I should never switch thread within transactions then? I'm asking because I wonder how to do transaction right, sorry if my code is too far away from that.

– Slacker
Nov 16 '18 at 2:19













Do it right? Don't switch threads.

– duffymo
Nov 16 '18 at 10:26





Do it right? Don't switch threads.

– duffymo
Nov 16 '18 at 10:26












1 Answer
1






active

oldest

votes


















0














Answering your question, yes it is a bad practice to switch threads within a transaction. However your code doesn't seem to be doing that, it's just using a non-conventional way to lock a shared resource.






share|improve this answer
























  • I think if I recode it like: @Transactional public SomeThing updateSomething(int i) { Something st = loadSomethingFromDbById(i); st.doSomeChange(); return saveSomethingToDb(st); } It's more conventional, but could not do things parallel, e.g. make two http request same time. Is that correct? Sorry for the formatting, can't get it right.

    – Slacker
    Nov 16 '18 at 6:24













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%2f53330136%2fis-it-a-bad-practice-to-switch-threads-within-a-transaction%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Answering your question, yes it is a bad practice to switch threads within a transaction. However your code doesn't seem to be doing that, it's just using a non-conventional way to lock a shared resource.






share|improve this answer
























  • I think if I recode it like: @Transactional public SomeThing updateSomething(int i) { Something st = loadSomethingFromDbById(i); st.doSomeChange(); return saveSomethingToDb(st); } It's more conventional, but could not do things parallel, e.g. make two http request same time. Is that correct? Sorry for the formatting, can't get it right.

    – Slacker
    Nov 16 '18 at 6:24


















0














Answering your question, yes it is a bad practice to switch threads within a transaction. However your code doesn't seem to be doing that, it's just using a non-conventional way to lock a shared resource.






share|improve this answer
























  • I think if I recode it like: @Transactional public SomeThing updateSomething(int i) { Something st = loadSomethingFromDbById(i); st.doSomeChange(); return saveSomethingToDb(st); } It's more conventional, but could not do things parallel, e.g. make two http request same time. Is that correct? Sorry for the formatting, can't get it right.

    – Slacker
    Nov 16 '18 at 6:24
















0












0








0







Answering your question, yes it is a bad practice to switch threads within a transaction. However your code doesn't seem to be doing that, it's just using a non-conventional way to lock a shared resource.






share|improve this answer













Answering your question, yes it is a bad practice to switch threads within a transaction. However your code doesn't seem to be doing that, it's just using a non-conventional way to lock a shared resource.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 2:53









Perdi EstaquelPerdi Estaquel

6691520




6691520













  • I think if I recode it like: @Transactional public SomeThing updateSomething(int i) { Something st = loadSomethingFromDbById(i); st.doSomeChange(); return saveSomethingToDb(st); } It's more conventional, but could not do things parallel, e.g. make two http request same time. Is that correct? Sorry for the formatting, can't get it right.

    – Slacker
    Nov 16 '18 at 6:24





















  • I think if I recode it like: @Transactional public SomeThing updateSomething(int i) { Something st = loadSomethingFromDbById(i); st.doSomeChange(); return saveSomethingToDb(st); } It's more conventional, but could not do things parallel, e.g. make two http request same time. Is that correct? Sorry for the formatting, can't get it right.

    – Slacker
    Nov 16 '18 at 6:24



















I think if I recode it like: @Transactional public SomeThing updateSomething(int i) { Something st = loadSomethingFromDbById(i); st.doSomeChange(); return saveSomethingToDb(st); } It's more conventional, but could not do things parallel, e.g. make two http request same time. Is that correct? Sorry for the formatting, can't get it right.

– Slacker
Nov 16 '18 at 6:24







I think if I recode it like: @Transactional public SomeThing updateSomething(int i) { Something st = loadSomethingFromDbById(i); st.doSomeChange(); return saveSomethingToDb(st); } It's more conventional, but could not do things parallel, e.g. make two http request same time. Is that correct? Sorry for the formatting, can't get it right.

– Slacker
Nov 16 '18 at 6:24






















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%2f53330136%2fis-it-a-bad-practice-to-switch-threads-within-a-transaction%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

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values