Vert.x MySQL - PostgreSQL client

MySQL / PostgreSQL客户端为Vert.x应用提供了一个与MySQL / PostgreSQL数据库交互的接口.

它使用Mauricio Linhares开源驱动与MySQL / PostgreSQL数据库进行非阻塞交互.

Creating a the client

下面给出了几种创建方式:

Using default shared pool

在大多数情况下,我们需要在多个客户端实例中共享同一个连接池

例如你通过部署多个verticle实例的方式进行程序拓展,但是可以每个verticle可以共享同一个连接池.

  1. JsonObject mySQLClientConfig = new JsonObject().put("host", "mymysqldb.mycompany");
  2. AsyncSQLClient mySQLClient = MySQLClient.createShared(vertx, mySQLClientConfig);
  3. // To create a PostgreSQL client:
  4. JsonObject postgreSQLClientConfig = new JsonObject().put("host", "mypostgresqldb.mycompany");
  5. AsyncSQLClient postgreSQLClient = PostgreSQLClient.createShared(vertx, postgreSQLClientConfig);

MySQLClient.createShared或者PostgreSQLClient.createShared会根据指定的配置创建一个连接池. 随后再调用这俩个方式时会使用同一个连接池,同时新的配置不会被采用.

Specifying a pool name

你也可以像下面这样指定一个连接池的名字.

  1. JsonObject mySQLClientConfig = new JsonObject().put("host", "mymysqldb.mycompany");
  2. AsyncSQLClient mySQLClient = MySQLClient.createShared(vertx, mySQLClientConfig, "MySQLPool1");
  3. // To create a PostgreSQL client:
  4. JsonObject postgreSQLClientConfig = new JsonObject().put("host", "mypostgresqldb.mycompany");
  5. AsyncSQLClient postgreSQLClient = PostgreSQLClient.createShared(vertx, postgreSQLClientConfig, "PostgreSQLPool1");

如果不同的客户端使用相同的Vertx实例和相同的连接池名字,那么他们将使用同一个连接池.

使用这种创建方式你可以在不同的客户端上使用不同的datasource, 例如和不同的数据库进行交互.

Creating a client with a non shared data source

虽然在大部分情况下不同的客户端实例需要共享相同的data source,但是有时候也可能客户端需要一个非共享的data source, 你可以使用MySQLClient.createNonShared或者PostgreSQLClient.createNonShared方法.

  1. JsonObject mySQLClientConfig = new JsonObject().put("host", "mymysqldb.mycompany");
  2. AsyncSQLClient mySQLClient = MySQLClient.createNonShared(vertx, mySQLClientConfig);
  3. // To create a PostgreSQL client:
  4. JsonObject postgreSQLClientConfig = new JsonObject().put("host", "mypostgresqldb.mycompany");
  5. AsyncSQLClient postgreSQLClient = PostgreSQLClient.createNonShared(vertx, postgreSQLClientConfig);

这种方式和调用MySQLClient.createNonShared或者PostgreSQLClient.createNonShared时传递一个唯一的data source名字达到相同的效果.

Closing the client

你可以在整个verticle生命周期之内都持有客户端的引用,但是一旦你使用完该客户端你就应该调用close关闭它.

Getting a connection

当你成功创建出客户端之后,你可以使用getConnection方法来获得一个连接.

当连接池中有了可用连接之后,handler会获得一个可用连接

  1. client.getConnection(res -> {
  2. if (res.succeeded()) {
  3. SQLConnection connection = res.result();
  4. // Got a connection
  5. } else {
  6. // Failed to get connection - deal with it
  7. }
  8. });

连接是SQLConnection的一个实例, SQLConnection是一个被Sql客户端使用的公共接口.

需要注意的是datetimestamps类型. 无论何时从数据库中获取date时, 客户端会将它转换成ISO 8601形式的字符串(yyyy-MM-ddTHH:mm:ss.SSS).Mysql会忽略毫秒数.

Configuration

PostgreSqlMySql使用了下面相同的配置:

  1. {
  2. "host" : <your-host>,
  3. "port" : <your-port>,
  4. "maxPoolSize" : <maximum-number-of-open-connections>,
  5. "username" : <your-username>,
  6. "password" : <your-password>,
  7. "database" : <name-of-your-database>
  8. }
  • host : 数据库主机地址(默认是localhost)
  • port : 数据库端口(PostgreSQL默认是5432. MySQL默认是3306)
  • maxPoolSize : 连接池保持开启的最大数量,默认是10.
  • username : 连接数据库使用的用户名.(PostgreSQL的默认值是postgres, MySQL的默认值是root)
  • password : 连接数据库使用的密码(默认没有设置密码).
  • database : 连接的数据库名称.(默认值是test)