英文:
Connecting Postgres to Ballerina, can't find bre/lib folder
问题
BALLERINA版本
jBallerina 1.2.6
语言规范 2020R1
更新工具 0.8.8
我已经安装了Ballerina,一切都运行得很好,但现在我正在尝试在我的本地安装中使用Postgres,但我无法使其工作。
阅读文档时,它说要将Postgres JDBC驱动程序复制到目录BALLERINA_HOME/bre/lib... 但是并不存在这样的目录。
首先,我执行了"which ballerina"来找到它指向/usr/bin/ballerina。
当我来到这里时,它只是一个指向ballerina可执行文件的链接,而不是一个目录,可执行文件位于/usr/lib/ballerina/bin中。
所以我将驱动程序复制到了usr/lib/ballerina/lib,希望它能够工作,但实际上没有成功。
然后我了解到要创建一个项目并在Ballerina.toml中进行配置(使用命令行生成了项目和模块)。
所以这是我的TOML文件:
[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]
当我运行模块(不是构建,而是运行)时,我仍然得到相同的缺少驱动程序错误。
我尝试在/user/lib/ballerina下创建bre/lib目录,但似乎也不起作用。
我得到的错误如下:
警告:发现不兼容的JRE版本'11.0.7'。此Ballerina程序支持在JRE版本'1.8.*'上运行
错误:{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)
这是我正在尝试运行的模块代码:
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);
}
}
英文:
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);
}
}
答案1
得分: 1
以下是翻译好的内容:
不需要将Postgresql的jar文件复制到Ballerina安装位置。编译期间,编译器只需要能够访问该jar文件。你可能还在其他地方弄错了一些东西,因为你会得到Incompatible JRE version '11.0.7' found.
的警告。Ballerina已经包含了正确的Java版本:
$ 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)
这是一个可用的Ballerina 1.2.2 + Postgresql项目中的Ballerina.toml
文件:
[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"
注意,postgresql-42.2.10.jar
的路径是相对于Ballerina.toml
的位置的,两个文件都在版本控制下。
还要确保已经正确地提及了所有使用Postgresql jar的Ballerina模块。在这个例子中,我只有一个名为main
的模块。
主模块中的数据库设置代码与你的示例相同:
import ballerinax/java.jdbc;
jdbc:Client db = new ({
url: "jdbc:postgresql://localhost:5432/testdb",
username: "testdb",
password: "testdb"
});
英文:
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"
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论