Is “with_connection” necessary with “exec_query”?











up vote
4
down vote

favorite












I'm occasionally getting PG::TRDeadlockDetected on a Rails with PostgreSQL app (served via Passenger/Nginx) and traced it down to places in the code which use ActiveRecord::Base.connection.exececute.



In order to fix this, I'm replacing .execute with .exec_query or .exec_update as per what the docs recommend. However, it's not really made clear whether ActiveRecord::Base.connection.exec_query gets the connection from the connection pool or not.




  • When using ActiveRecord::Base.connection.exec_query in a Rails controller, does it have to be wrapped with ActiveRecord::Base.connection_pool.with_connection? (my guess: yes)

  • When using ActiveRecord::Base.connection.exec_query outside of the context of a request (say a Rake tasks executed as cronjob), does it have to be wrapped with ActiveRecord::Base.connection_pool.with_connection? (my guess: yes)


And if wrapping is necessary, are there shorter alternatives compared to:



ActiveRecord::Base.connection_pool.with_connection do |connection|
connection.exec_update "REINDEX INDEX my_complex_index"
end


(my guess: no)



Thanks for your hints!










share|improve this question
























  • Interesting, got any source for that? I'm experiencing the same issue with the same stack. Was not too sure about what causes it and just realized it started occuring after I added model methods that call raw sql with .execute.
    – mkrl
    Nov 17 at 11:23










  • Seems threading related to me
    – Dorian
    Nov 17 at 11:34










  • @mkrl The execute vs exec_query is in fact unrelated to the deadlock but should assure proper cleanup.
    – svoop
    Nov 17 at 15:02










  • @mkrl As for the connection pool, I've plowed through the AR source, but I'm not 100% sure I got things right (and therefore ask here). My guess is, the deadlock occurs when different threads pick up the same connection and create circular locks. The pooling of connections should prevent this.
    – svoop
    Nov 17 at 15:07










  • @svoop yes, I was thinking of that. Not sure about your case, but I'm using the open-source version. As far as I know, only passenger enterprise is able to spawn additional threads.
    – mkrl
    Nov 17 at 15:12















up vote
4
down vote

favorite












I'm occasionally getting PG::TRDeadlockDetected on a Rails with PostgreSQL app (served via Passenger/Nginx) and traced it down to places in the code which use ActiveRecord::Base.connection.exececute.



In order to fix this, I'm replacing .execute with .exec_query or .exec_update as per what the docs recommend. However, it's not really made clear whether ActiveRecord::Base.connection.exec_query gets the connection from the connection pool or not.




  • When using ActiveRecord::Base.connection.exec_query in a Rails controller, does it have to be wrapped with ActiveRecord::Base.connection_pool.with_connection? (my guess: yes)

  • When using ActiveRecord::Base.connection.exec_query outside of the context of a request (say a Rake tasks executed as cronjob), does it have to be wrapped with ActiveRecord::Base.connection_pool.with_connection? (my guess: yes)


And if wrapping is necessary, are there shorter alternatives compared to:



ActiveRecord::Base.connection_pool.with_connection do |connection|
connection.exec_update "REINDEX INDEX my_complex_index"
end


(my guess: no)



Thanks for your hints!










share|improve this question
























  • Interesting, got any source for that? I'm experiencing the same issue with the same stack. Was not too sure about what causes it and just realized it started occuring after I added model methods that call raw sql with .execute.
    – mkrl
    Nov 17 at 11:23










  • Seems threading related to me
    – Dorian
    Nov 17 at 11:34










  • @mkrl The execute vs exec_query is in fact unrelated to the deadlock but should assure proper cleanup.
    – svoop
    Nov 17 at 15:02










  • @mkrl As for the connection pool, I've plowed through the AR source, but I'm not 100% sure I got things right (and therefore ask here). My guess is, the deadlock occurs when different threads pick up the same connection and create circular locks. The pooling of connections should prevent this.
    – svoop
    Nov 17 at 15:07










  • @svoop yes, I was thinking of that. Not sure about your case, but I'm using the open-source version. As far as I know, only passenger enterprise is able to spawn additional threads.
    – mkrl
    Nov 17 at 15:12













up vote
4
down vote

favorite









up vote
4
down vote

favorite











I'm occasionally getting PG::TRDeadlockDetected on a Rails with PostgreSQL app (served via Passenger/Nginx) and traced it down to places in the code which use ActiveRecord::Base.connection.exececute.



In order to fix this, I'm replacing .execute with .exec_query or .exec_update as per what the docs recommend. However, it's not really made clear whether ActiveRecord::Base.connection.exec_query gets the connection from the connection pool or not.




  • When using ActiveRecord::Base.connection.exec_query in a Rails controller, does it have to be wrapped with ActiveRecord::Base.connection_pool.with_connection? (my guess: yes)

  • When using ActiveRecord::Base.connection.exec_query outside of the context of a request (say a Rake tasks executed as cronjob), does it have to be wrapped with ActiveRecord::Base.connection_pool.with_connection? (my guess: yes)


And if wrapping is necessary, are there shorter alternatives compared to:



ActiveRecord::Base.connection_pool.with_connection do |connection|
connection.exec_update "REINDEX INDEX my_complex_index"
end


(my guess: no)



Thanks for your hints!










share|improve this question















I'm occasionally getting PG::TRDeadlockDetected on a Rails with PostgreSQL app (served via Passenger/Nginx) and traced it down to places in the code which use ActiveRecord::Base.connection.exececute.



In order to fix this, I'm replacing .execute with .exec_query or .exec_update as per what the docs recommend. However, it's not really made clear whether ActiveRecord::Base.connection.exec_query gets the connection from the connection pool or not.




  • When using ActiveRecord::Base.connection.exec_query in a Rails controller, does it have to be wrapped with ActiveRecord::Base.connection_pool.with_connection? (my guess: yes)

  • When using ActiveRecord::Base.connection.exec_query outside of the context of a request (say a Rake tasks executed as cronjob), does it have to be wrapped with ActiveRecord::Base.connection_pool.with_connection? (my guess: yes)


And if wrapping is necessary, are there shorter alternatives compared to:



ActiveRecord::Base.connection_pool.with_connection do |connection|
connection.exec_update "REINDEX INDEX my_complex_index"
end


(my guess: no)



Thanks for your hints!







ruby-on-rails ruby postgresql activerecord






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 10:50

























asked Nov 17 at 9:50









svoop

2,0241528




2,0241528












  • Interesting, got any source for that? I'm experiencing the same issue with the same stack. Was not too sure about what causes it and just realized it started occuring after I added model methods that call raw sql with .execute.
    – mkrl
    Nov 17 at 11:23










  • Seems threading related to me
    – Dorian
    Nov 17 at 11:34










  • @mkrl The execute vs exec_query is in fact unrelated to the deadlock but should assure proper cleanup.
    – svoop
    Nov 17 at 15:02










  • @mkrl As for the connection pool, I've plowed through the AR source, but I'm not 100% sure I got things right (and therefore ask here). My guess is, the deadlock occurs when different threads pick up the same connection and create circular locks. The pooling of connections should prevent this.
    – svoop
    Nov 17 at 15:07










  • @svoop yes, I was thinking of that. Not sure about your case, but I'm using the open-source version. As far as I know, only passenger enterprise is able to spawn additional threads.
    – mkrl
    Nov 17 at 15:12


















  • Interesting, got any source for that? I'm experiencing the same issue with the same stack. Was not too sure about what causes it and just realized it started occuring after I added model methods that call raw sql with .execute.
    – mkrl
    Nov 17 at 11:23










  • Seems threading related to me
    – Dorian
    Nov 17 at 11:34










  • @mkrl The execute vs exec_query is in fact unrelated to the deadlock but should assure proper cleanup.
    – svoop
    Nov 17 at 15:02










  • @mkrl As for the connection pool, I've plowed through the AR source, but I'm not 100% sure I got things right (and therefore ask here). My guess is, the deadlock occurs when different threads pick up the same connection and create circular locks. The pooling of connections should prevent this.
    – svoop
    Nov 17 at 15:07










  • @svoop yes, I was thinking of that. Not sure about your case, but I'm using the open-source version. As far as I know, only passenger enterprise is able to spawn additional threads.
    – mkrl
    Nov 17 at 15:12
















Interesting, got any source for that? I'm experiencing the same issue with the same stack. Was not too sure about what causes it and just realized it started occuring after I added model methods that call raw sql with .execute.
– mkrl
Nov 17 at 11:23




Interesting, got any source for that? I'm experiencing the same issue with the same stack. Was not too sure about what causes it and just realized it started occuring after I added model methods that call raw sql with .execute.
– mkrl
Nov 17 at 11:23












Seems threading related to me
– Dorian
Nov 17 at 11:34




Seems threading related to me
– Dorian
Nov 17 at 11:34












@mkrl The execute vs exec_query is in fact unrelated to the deadlock but should assure proper cleanup.
– svoop
Nov 17 at 15:02




@mkrl The execute vs exec_query is in fact unrelated to the deadlock but should assure proper cleanup.
– svoop
Nov 17 at 15:02












@mkrl As for the connection pool, I've plowed through the AR source, but I'm not 100% sure I got things right (and therefore ask here). My guess is, the deadlock occurs when different threads pick up the same connection and create circular locks. The pooling of connections should prevent this.
– svoop
Nov 17 at 15:07




@mkrl As for the connection pool, I've plowed through the AR source, but I'm not 100% sure I got things right (and therefore ask here). My guess is, the deadlock occurs when different threads pick up the same connection and create circular locks. The pooling of connections should prevent this.
– svoop
Nov 17 at 15:07












@svoop yes, I was thinking of that. Not sure about your case, but I'm using the open-source version. As far as I know, only passenger enterprise is able to spawn additional threads.
– mkrl
Nov 17 at 15:12




@svoop yes, I was thinking of that. Not sure about your case, but I'm using the open-source version. As far as I know, only passenger enterprise is able to spawn additional threads.
– mkrl
Nov 17 at 15:12

















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',
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%2f53350039%2fis-with-connection-necessary-with-exec-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350039%2fis-with-connection-necessary-with-exec-query%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

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]