PostgreSQL(五)JDBC连接串常用参数

2023-05-16

目录

    • 1.单机 PostgreSQL 连接串
    • 2.集群PostgreSQL 连接串

  • PostgreSQL JDBC 官方驱动下载地址: https://jdbc.postgresql.org/download/

  • PostgreSQL JDBC 官方参数说明文档: https://jdbc.postgresql.org/documentation/use/

  • 驱动类: driver-class-name=org.postgresql.Driver

1.单机 PostgreSQL 连接串

单机 PostgreSQL,连接串如下:

url: jdbc:postgresql://10.20.1.231:5432/postgres?
binaryTransfer=false&forceBinary=false&reWriteBatchedInserts=true
  • binaryTransfer=false:控制是否使用二进制协议传输数据,false 表示不适用,默认为 true

  • forceBinary=false:控制是否将非 ASCII 字符串强制转换为二进制格式,false 表示不强制转换,默认为 true.

  • reWriteBatchedInserts=true:控制是否将批量插入语句转换成更高效的形式,true 表示转换,默认为 false

    例如:

    insert into foo (col1, col2, col3) values(1,2,3);
    insert into foo (col1, col2, col3) values(4,5,6);
    

    会转换成:

    insert into foo (col1, col2, col3) values(1,2,3), (4,5,6);
    

    如果使用正确,reWriteBatchedInserts 会提升批量 insert 性能 2-3 倍。

2.集群PostgreSQL 连接串

集群PostgreSQL,连接串如下:

url: jdbc:postgresql://10.20.1.231:5432/postgres?
binaryTransfer=false&forceBinary=false&reWriteBatchedInserts=true&targetServerType=master&loadBalanceHosts=true
  • 单机 PostgreSQL 连接串的所有参数。
  • targetServerType=master:只允许连接到具有所需状态的服务器,可选值有:
    • any:默认,表示连接到任何一个可用的数据库服务器,不区分主从数据库;
    • master:表示连接到主数据库,可读写;
    • slave:表示连接到从数据库,可读,不可写;
    • 其他不常用值:primary, master, slave, secondary, preferSlave, preferSecondary and preferPrimary。
  • loadBalanceHosts=true:控制是否启用主从模式下的负载均衡,true 表示启用,开启后依序选择一个 ip1:port 进行连接,默认为 false

整理完毕,完结撒花~ 🌻





参考地址:

1.使用JDBC时,加速批量insert,https://blog.csdn.net/yanzongshuai/article/details/112084578

2.PostgreSQL reWriteBatchedInserts configuration property,https://vladmihalcea.com/postgresql-multi-row-insert-rewritebatchedinserts-property/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PostgreSQL(五)JDBC连接串常用参数 的相关文章

随机推荐