8.2. Eclipse Vert.x JDBC 客户端的更改
从 Eclipse Vert.x 4 中,JDBC 客户端支持 SQL 客户端。SQL common 模块也已合并到 JDBC 客户端中,即 io.vertx:vertx-sql-common 合并于 io.vertx:vertx-client 模块。您必须删除 io.vertx:vertx-sql-common 依赖项文件,因为 io.vertx:vertx-jdbc-client 将包含它。通过合并 SQL 通用客户端,所有数据库 API 都已合并到 JDBC 客户端中。
在 Eclipse Vert.x 4 中,SQL 客户端已被更新,使其包含以下客户端:
- 重新主动 PostgreSQL 客户端。在较早版本的中,它包括了被动 PostgreSQL 客户端。
- 重新主动 MySQL 客户端
- 重新主动 DB2 客户端
- 继续包括被动 PostgreSQL 客户端。这个客户端也包括在 Eclipse Vert.x 3.x 版本中。
- 现有的 JDBC 客户端现在同时包含 JDBC 客户端 API 和 SQL 客户端 API
被动实施使用数据库网络协议。这使得资源效率更高。
对数据库的 JDBC 调用将阻止调用。JDBC 客户端使用 worker 线程来使这些调用非阻塞。
以下部分介绍了 Eclipse Vert.x JDBC 客户端中的更改。
8.2.1. 创建池 复制链接链接已复制到粘贴板!
在 Eclipse Vert.x 4 中,您可以使用 JDBC 客户端 API 创建池。在早期版本中,您只能创建客户端。您不能创建池。
以下示例演示了如何在 Eclipse Vert.x 3.x 中创建客户端。
// 3.x
SQLClient client = JDBCClient.create(vertx, jsonConfig);
以下示例演示了如何在 Eclipse Vert.x 4 中创建池。
// 4.x
JDBCPool pool = JDBCPool.pool(vertx, jsonConfig);
虽然 Eclipse Vert.x 3.x API 在 Eclipse Vert.x 4 中被支持,但建议您使用应用程序中的新的 JDBC 客户端 API。
池允许您执行简单的查询。您不需要管理简单查询的连接。但是,对于复杂的查询或多个查询,您必须管理您的连接。
以下示例演示了如何管理 Eclipse Vert.x 3.x 中的查询的连接。
// 3.x
client.getConnection(res -> {
if (res.succeeded()) {
SQLConnection connection = res.result();
// Important, do not forget to return the connection
connection.close();
} else {
// Failed to get connection
}
});
以下示例演示了如何管理 Eclipse Vert.x 4 中的查询连接。
// 4.x
pool
.getConnection()
.onFailure(e -> {
// Failed to get a connection
})
.onSuccess(conn -> {
// Important, do not forget to return the connection
conn.close();
});
8.2.2. 支持 Typsesafe 配置 复制链接链接已复制到粘贴板!
您可以使用 jsonConfig 进行配置。但是,使用 jsonConfig 有时可能会导致错误。为避免这些错误,JDBC 客户端引入了 Typesafe Config。
以下示例显示了 Typesafe Config 的基本结构。
// 4.x ONLY!!!
JDBCPool pool = JDBCPool.pool(
vertx,
// configure the connection
new JDBCConnectOptions()
// H2 connection string
.setJdbcUrl("jdbc:h2:~/test")
// username
.setUser("sa")
// password
.setPassword(""),
// configure the pool
new PoolOptions()
.setMaxSize(16)
);
要使用 Typesafe Config,您必须在项目中包含 agroal 连接池。该池不公开许多配置选项,并使配置易于使用。
8.2.3. 运行 SQL 查询 复制链接链接已复制到粘贴板!
本节演示了如何在 JDBC 客户端中运行查询。
8.2.3.1. 运行一个 shot 查询 复制链接链接已复制到粘贴板!
以下示例演示了如何在 Eclipse Vert.x 3.x 中在不管理连接的情况下运行查询。
// 3.x
client.query("SELECT * FROM user WHERE emp_id > ?", new JsonArray().add(1000), res -> {
if (res.succeeded()) {
ResultSet rs = res2.result();
// You can use these results in your application
}
});
以下示例演示了如何在 Eclipse Vert.x 4 中运行查询,而不管理连接。
// 4.x
pool
.preparedQuery("SELECT * FROM user WHERE emp_id > ?")
// the emp id to look up
.execute(Tuple.of(1000))
.onSuccess(rows -> {
for (Row row : rows) {
System.out.println(row.getString("FIRST_NAME"));
}
});
8.2.3.2. 在受管连接上运行查询 复制链接链接已复制到粘贴板!
以下示例演示了如何在 Eclipse Vert.x 4 中对受管连接运行查询。
pool
.getConnection()
.onFailure(e -> {
// Failed to get a connection
})
.onSuccess(conn -> {
conn
.query("SELECT * FROM user")
.execute()
.onFailure(e -> {
// Handle the failure
// Important, do not forget to return the connection
conn.close();
})
.onSuccess(rows -> {
for (Row row : rows) {
System.out.println(row.getString("FIRST_NAME"));
}
// Important, do not forget to return the connection
conn.close();
});
});
8.2.4. 支持存储的步骤 复制链接链接已复制到粘贴板!
存储的步骤在 JDBC 客户端中受到支持。
以下示例演示了如何在 Eclipse Vert.x 3.x 中传递 IN 参数。
// 3.x
connection.callWithParams(
"{ call new_customer(?, ?) }",
new JsonArray().add("John").add("Doe"),
null,
res -> {
if (res.succeeded()) {
// Success!
} else {
// Failed!
}
});
以下示例演示了如何在 Eclipse Vert.x 4 中传递 IN 参数。
// 4.x
client
.preparedQuery("{call new_customer(?, ?)}")
.execute(Tuple.of("Paulo", "Lopes"))
.onSuccess(rows -> {
...
});
在 Eclipse Vert.x 3.x 中,由于可用类型,将 IN 和 OUT 参数结合使用的支持会非常有限。在 Eclipse Vert.x 4 中,池是安全的,可以处理 IN 和 OUT 参数的组合。您还可以在应用程序中使用 INOUT 参数。
以下示例显示了在 Eclipse Vert.x 3.x 中处理参数。
// 3.x
connection.callWithParams(
"{ call customer_lastname(?, ?) }",
new JsonArray().add("John"),
new JsonArray().addNull().add("VARCHAR"),
res -> {
if (res.succeeded()) {
ResultSet result = res.result();
} else {
// Failed!
}
});
以下示例显示了在 Eclipse Vert.x 4 中处理参数。
// 4.x
client
.preparedQuery("{call customer_lastname(?, ?)}")
.execute(Tuple.of("John", SqlOutParam.OUT(JDBCType.VARCHAR)))
.onSuccess(rows -> {
...
});
在 JDBC 客户端中,数据类型已更新。
-
对于类型为
OUT的参数,您可以指定其返回类型。在示例中,OUT参数指定为 类型VARCHAR,这是 JDBC 常量。 - 这类类型不受 JSON 限制的限制。现在,您可以在类型名称中使用数据库特定类型而不是文本常量。