Open Source API Gateway

500 Internal Server Error: Could Not Connect to Database

Membrane API Gateway returns 500 Internal Server Error with the detail Could not connect to database when the statisticsProvider plugin cannot get a connection from its JDBC data source. The database or the data source configuration is to blame.

The Error Response

The response contains the status 500 Internal Server Error, the title Internal server error., the detail Could not connect to database, the problem type https://membrane-api.io/problems/internal and a link to this page. In development mode, the message of the database exception is included as well. The type internal means that the gateway could not complete the processing of the exchange. In production mode, the title is replaced by Internal Server Error and the details are left out.

What the Statistics Provider Does

The statisticsProvider plugin reads the call statistics that Membrane stored in a database and returns them as JSON. It uses the JDBC data source bean named in dataSourceBeanId. If no connection can be obtained from that data source, the plugin answers with this error.

Common Causes

The Database Is Down or Unreachable

The database server is not running, or the gateway host cannot reach it because of DNS, firewall or port settings.

The Data Source Is Misconfigured

The data source bean has a wrong JDBC URL, user name or password, or its JDBC driver is not on the classpath of the gateway.

The Connection Pool Is Exhausted

All connections of the data source's pool are in use, or the connection attempt timed out.

An Unexpected Exception in the Plugin

Any other exception that the statisticsProvider plugin does not turn into an error response itself is reported under the same URL by Membrane's generic error handler. The detail then reads Aborting! Exception caused in … flow. and names the plugin, the request URI and the flow. Unless the exception points to your configuration or to the message, this usually indicates a bug in Membrane.

How to Fix It

  1. Check that the database is running and reachable from the gateway host.
  2. Test the JDBC URL and the credentials of the bean named in dataSourceBeanId, and make sure the JDBC driver is available.
  3. Check the pool size and the timeouts of the data source.
  4. Look at the database exception in the response (development mode) or in the log.

Hide Details in Production

By default, Membrane runs in development mode and adds details to error responses that help you find the problem. These details may reveal information about your setup. In production, turn them off:

configuration:
production: true
configuration:
  production: true

Add the configuration document to your apis.yaml and separate it from the APIs with ---. The title then reads Internal Server Error, and instead of the details the response only contains a log key. Search the gateway log for this key to find the exception.

If the plugin is nested inside a request, response or abort element and throws an unexpected exception, the exception message, the stack trace and the link to this page are currently sent even in production mode. Fix the failing plugin rather than relying on production mode to hide them.

Related Documentation

Configuration reference:

← All Membrane error messages

Still stuck? See the troubleshooting guide or ask on GitHub Issues. For commercial support, contact info@predic8.de.