2

I've an Azure function app which has a timer trigger function written in Java. I need to connect to PostgreSQL which is deployed on one of the Azure VMs(not using Managed Postgres here).

My code :

import java.sql.*;

public class MyFunction {

    public static final String DB_URL              = "jdbc:postgresql://<host>:<port>/<dbName>";
    public static final String DB_USER             = "<dbUser>";
    public static final String DB_PASSWORD         = "<dbPassword>";

    @FunctionName("timerTrigger")
    public void timerTrigger(@TimerTrigger(name = "timerTriggerFunc", schedule = "0 */30 * * * *")
                                         String timerInfo, ExecutionContext context) {

        Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
        connection.setAutoCommit(false);
    }
}

When I run this function, its throwing the following exception :

[11/29/2019 10:42:24] java.sql.SQLException: No suitable driver found for jdbc:postgresql://<host>:<port>/<dbName>
[11/29/2019 10:42:24]   at java.sql.DriverManager.getConnection(DriverManager.java:689)
[11/29/2019 10:42:24]   at java.sql.DriverManager.getConnection(DriverManager.java:247)

Please help resolving this. I went through other questions in stack overflow and browsed, but didn't get my use case elsewhere.

4
  • 1
    You apparently don't have the Postgres JDBC driver in your classpath Commented Nov 29, 2019 at 11:59
  • @a_horse_with_no_name That, or the driver isn't on the initial classpath but on a context classpath and needs to be loaded explicitly. Commented Nov 29, 2019 at 16:56
  • @StanleyGong I'm not the person that asked the question. Commented Dec 4, 2019 at 8:24
  • Hi @mypeople , has your issue been solved ? Commented Dec 4, 2019 at 8:48

1 Answer 1

1

Try to import the postgresql driver. I use the code below to connect to my database successfully:

import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import java.sql.*;
/**
 * Azure Functions with HTTP Trigger.
 */
public class Function {

    @FunctionName("HttpTrigger-Java")
    public HttpResponseMessage run(
            @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}) HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) throws ClassNotFoundException {

        Connection c = null;

        try {
           Class.forName("org.postgresql.Driver");
           c = DriverManager
                   .getConnection("jdbc:postgresql://<DB server>:5432/<dbname>",
                           "<username>", "<password>");
        } catch (SQLException e) {

           return request.createResponseBuilder(HttpStatus.OK).body(e.getMessage()).build();
        }


       return request.createResponseBuilder(HttpStatus.OK).body("Opened database successfully").build();

    }
}

Adding latest postgresql Driver in maven dependencies:

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.2.8.jre7</version>
</dependency>

Result on Azure Java function:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, is it helpful for you ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.