Alex Merced
Alex Merced

Reputation: 150

Connecting Postgres to Ballerina, can't find bre/lib folder

BALLERINA VERSION

jBallerina 1.2.6
Language specification 2020R1
Update Tool 0.8.8

I've installed Ballerina and everything works great but now I'm trying to work with my local install on postgres and I can't get it to work.

When reading the documentation it said to copy the postgres JDBC driver to a director BALLERINA_HOME/bre/lib.. no such directory exists.

So first I did a "which ballerina" to find it's pointing to /usr/bin/ballerina

When I made my way here it was just a link to the ballerina executable not a directory, the executable being in /usr/lib/ballerina/bin

So I coping the driver to usr/lib/ballerina/lib in hopes it would work, it did not.

Then I read about creating a project and configuring it in the Ballerina.toml (used the command line to generate the project and module)

So here is my TOML file

[project]
org-name= "alexmerced"
version= "0.1.0"

[platform]
target = "java8"

    [[platform.libraries]]
    artafactId = "postgresql"
    version = "42.2.14"
    path = "/usr/lib/ballerina/bre/lib/postgresql-42.2.14.jar"
    groupId = "org.postgresql"
    modules = ["samplemodule"]

[dependencies]

when I run the module (not build, run) I still get the same missing driver error.

I tried creating the bre/lib director in /user/lib/ballerina but that doesnt' seem to work either.

The error I'm getting is the following:


WARNING: Incompatible JRE version '11.0.7' found. This ballerina program supports running on JRE version '1.8.*'
error: {ballerinax/java.jdbc}ApplicationError message=error in sql connector configuration: Failed to get driver instance for jdbcUrl=jdbc:postgresql://localhost:5432/test?useSSL=false:No suitable driver
        at ballerinax.java_jdbc:createClient(client.bal:137)
           ballerinax.java_jdbc.Client:__init(client.bal:27)
           ballerinax.java_jdbc.Client:$__init$(client.bal:21)

here is module code I'm trying to run

import ballerina/io;
// import ballerina/jsonutils;
import ballerina/time;
import ballerinax/java.jdbc;

jdbc:Client testDB = new ({
    url: "jdbc:postgresql://localhost:5432/test?useSSL=false",
    username: "test",
    password: "test",
    dbOptions: {useSSL: false}
});

type Bird record {
    int id;
    int age;
    string name;
    time:Time insertedTime;
};


public function main() {

 //RENAME PRINT   
 var print = io:println;

 var ret = testDB->update("CREATE TABLE bird(id INT AUTO_INCREMENT, " +
        "age INT, name VARCHAR(255), insertedTime TIMESTAMP DEFAULT " +
        "CURRENT_TIMESTAMP, PRIMARY KEY (id))");
    handleUpdate(ret, "Create student table");


}

function handleUpdate(jdbc:UpdateResult|jdbc:Error returned, string message) {
    if (returned is jdbc:UpdateResult) {
        io:println(message, " status: ", returned.updatedRowCount);
    } else {
        io:println(message, " failed: ", <string>returned.detail()?.message);
    }
}

Upvotes: 0

Views: 198

Answers (1)

user272735
user272735

Reputation: 10648

It is unnecessary to copy the Postgresql jar to Ballerina installation location. The jar simply needs to be accessible by the compiler during the build. You might also have messed something else there as you get Incompatible JRE version '11.0.7' found. warning. Ballerina comes with the correct Java version included:

$ ballerina home
/usr/lib/ballerina/distributions/jballerina-1.2.2
$ ls -d /usr/lib/ballerina/dependencies/jdk8u202-b08-jre/
/usr/lib/ballerina/dependencies/jdk8u202-b08-jre/
$ /usr/lib/ballerina/dependencies/jdk8u202-b08-jre/bin/java -version
openjdk version "1.8.0_202"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_202-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.202-b08, mixed mode)

This is a Ballerina.toml file from a working Ballerina 1.2.2 + Postgresql project:

[project]
org-name= "test"
version= "0.1.0"

[dependencies]

[platform]
target = "java8"
    [[platform.libraries]]
    module = "main"
    path = "../lib/postgresql-42.2.10.jar"
    groupId = "org.postgresql"
    artafactId = "postgresql"
    version = "42.2.10"

Note that path of postgresql-42.2.10.jar is relative to the location of Ballerina.toml and the both files are under version control.

Also make sure that you have correctly mentioned all Ballerina modules that are using the Postgresql jar. In this example I have only a single module named main.

The database setup code in the main-module is identical to your example:

import ballerinax/java.jdbc;

jdbc:Client db = new ({
    url: "jdbc:postgresql://localhost:5432/testdb",
    username: "testdb",
    password: "testdb"
});

Upvotes: 1

Related Questions