質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Q&A

解決済

1回答

1451閲覧

herokuのpostgresqlのデータベーステーブルに何度かクエリを実行すると503のエラーが発生する。

mussi425

総合スコア11

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

0グッド

0クリップ

投稿2020/04/14 01:38

編集2020/04/14 02:49

お世話になっております。
herokuとherokuのデータベースを使い、データベースのテーブルにアクセスするクエリを作成しました。
また、そのクエリを実行し、jsonにして返すGETメソッドを作成いたしました。
GETメソッドを叩いているとき、最初はjsonを返してくれるのですが、繰り返し叩いていると10回目ぐらいで503エラーが出て叩けなくなってしまいます。
そしてしばらくしてからGETメソッドを叩くと再びjsonを返してくれるようになるのですが、繰り返すとエラーになります。

最初はうまくいくこと、herokuを無料で利用していることからプラン的なもので影響が出ているのかと思ったのですが、これで何か分かる方がいらっしゃれば、原因とできうる解決策などをお教えいただけると幸いです。

nodejs

1const express = require('express') 2const path = require('path') 3const pg = require('pg') 4const PORT = process.env.PORT || 5000 5 6var pool = pg.Pool ({ 7 host: process.env.ENV_HOST, 8 database: process.env.ENV_DATABASE, 9 user: process.env.ENV_USER, 10 port: 5432, 11 password: process.env.ENV_PASSWORD, 12}); 13 14 15 16express() 17 .use(express.static(path.join(__dirname, 'public'))) 18 .get('/get', function(req,res) { 19 // 作成したGETメソッド 20 pool.connect(function(err, client, done) { 21 if (err) { 22 console.log(err); 23 } else { 24 // ここが実行しているクエリ 25 client.query('SELECT * FROM 【テーブル名】', function (err, result) { 26 res.json(result.rows) 27 }); 28 } 29 }) 30 }) 31 .listen(PORT, () => console.log(`Listening on ${ PORT }`))

追記:以下がログになります。

2020-04-14T01:54:20.802927+00:00 heroku[web.1]: Unidling 2020-04-14T01:54:20.806472+00:00 heroku[web.1]: State changed from down to starting 2020-04-14T01:54:25.884577+00:00 heroku[web.1]: State changed from starting to up 2020-04-14T01:54:25.807353+00:00 app[web.1]: (node:4) DeprecationWarning: Constructing a pg.Pool without new is deprecated and will stop working in pg 8. 2020-04-14T01:54:25.808187+00:00 app[web.1]: Listening on 50352 2020-04-14T01:54:26.916949+00:00 heroku[router]: at=info method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=9a164e07-f0a6-48e1-bea2-0c5a7c086a81 fwd="219.126.219.133" dyno=web.1 connect=1ms service=62ms status=200 bytes=470 protocol=https 2020-04-14T02:07:12.478573+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=higasumi52-20200410.herokuapp.com request_id=680eed99-735f-4c83-8bbc-849c0d24253a fwd="219.126.219.133,64.233.172.48" dyno=web.1 connect=1ms service=8ms status=404 bytes=394 protocol=https 2020-04-14T02:23:53.316293+00:00 heroku[router]: at=info method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=39c84980-a4a1-44b0-8b05-917fe53dcc25 fwd="219.126.219.133" dyno=web.1 connect=1ms service=16ms status=200 bytes=470 protocol=https 2020-04-14T02:23:53.797646+00:00 heroku[router]: at=info method=GET path="/" host=higasumi52-20200410.herokuapp.com request_id=626e4f41-a74e-4071-9ed5-10ef47d67b0c fwd="185.2.196.196" dyno=web.1 connect=1ms service=5ms status=200 bytes=583 protocol=http 2020-04-14T02:39:33.379450+00:00 heroku[router]: at=info method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=2485337e-ba4f-463e-8e72-8c6aef140494 fwd="219.126.219.133" dyno=web.1 connect=1ms service=16ms status=200 bytes=470 protocol=https 2020-04-14T02:40:14.066370+00:00 heroku[router]: at=info method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=378b3a03-8dad-47fe-b9ef-59b6d2b89ad5 fwd="219.126.219.133" dyno=web.1 connect=1ms service=18ms status=304 bytes=151 protocol=https 2020-04-14T02:40:14.942105+00:00 heroku[router]: at=info method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=f6519f8f-668f-4867-9ee4-b0e8deeef592 fwd="219.126.219.133" dyno=web.1 connect=1ms service=12ms status=304 bytes=151 protocol=https 2020-04-14T02:40:15.722869+00:00 heroku[router]: at=info method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=0250e54e-3547-4795-9682-11170435b78b fwd="219.126.219.133" dyno=web.1 connect=1ms service=14ms status=304 bytes=151 protocol=https 2020-04-14T02:40:16.525570+00:00 heroku[router]: at=info method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=269ee2bd-1ce5-4d20-91f8-d0956d789426 fwd="219.126.219.133" dyno=web.1 connect=1ms service=16ms status=304 bytes=151 protocol=https 2020-04-14T02:40:17.310146+00:00 heroku[router]: at=info method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=03298f63-786a-49ed-97be-a0ca560ed98c fwd="219.126.219.133" dyno=web.1 connect=1ms service=10ms status=304 bytes=151 protocol=https 2020-04-14T02:40:18.003926+00:00 heroku[router]: at=info method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=7e64327f-4e80-429e-afd4-98977866a010 fwd="219.126.219.133" dyno=web.1 connect=1ms service=15ms status=304 bytes=151 protocol=https 2020-04-14T02:40:18.744060+00:00 heroku[router]: at=info method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=ceeb7c19-464b-4aa7-a4eb-0a8558788188 fwd="219.126.219.133" dyno=web.1 connect=1ms service=12ms status=304 bytes=151 protocol=https 2020-04-14T02:40:49.482583+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/get" host=higasumi52-20200410.herokuapp.com request_id=a1e6b9a0-105e-47e0-b76d-a5df71e52dc5 fwd="219.126.219.133" dyno=web.1 connect=1ms service=30009ms status=503 bytes=0 protocol=https

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mussi425

2020/04/14 07:03

申し訳ございません。 ログを追記いたしました。 よろしくお願いいたします。
guest

回答1

0

ベストアンサー

コネクションを使い終わったら done を呼んであげないといけないのでは。

pg.Pool | node-postgres の pool.connect の注意書きより、

You must call the releaseCallback or client.release (which points to the releaseCallback) when you are finished with a client. If you forget to release the client then your application will quickly exhaust available, idle clients in the pool and all further calls to pool.connect will timeout with an error or hang indefinitely if you have connectionTimeoutMills configured to 0.

diff

1 // ここが実行しているクエリ 2 client.query('SELECT * FROM 【テーブル名】', function (err, result) { 3+ done(); 4 res.json(result.rows) 5 });

投稿2020/04/14 12:51

hoshi-takanori

総合スコア7895

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mussi425

2020/04/14 13:06

done();を入れておかないと接続したままになり、そのままconnectionが増えていくことでいずれはエラーになってしまうということで理解いたしました。 無事、解決いたしました。 ありがとうございます!
hoshi-takanori

2020/04/14 13:16 編集

コネクションプールなので、デフォルトでは最大 10 個の接続を使い回すわけですが、done (releaseCallback) を呼ばないと、ずっと使用中だと思って再利用できなくて、11 回目のリクエストですべてのコネクションが使用中のままタイムアウトしてしまうのでしょう。 https://wa3.i-3-i.info/word12762.html
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問