简体   繁体   English

从Java设置PostgreSQL JDBC连接的连接选项

[英]Setting connection options for PostgreSQL JDBC connection from Java

My application was using Mongo DB earlier. 我的应用程序之前使用的是Mongo DB。 Now, I'm shifting to PostgreSQL. 现在,我要转向PostgreSQL。 For that, I've been migrating queries and all. 为此,我一直在迁移查询和所有内容。 But, I was being blocked by issue. 但是,我被问题所困扰。 In MongoDB connection, we've some MongoClientOptions used to improve the performance of the application. 在MongoDB连接中,我们使用了一些MongoClientOptions来提高应用程序的性能。 In some way, I want to set these options with JDBC for PostgreSQL also. 以某种方式,我也想通过JDBC for PostgreSQL设置这些选项。

I've tried and searched the same functions in JDBC DriverManager class. 我已经尝试并在JDBC DriverManager类中搜索了相同的函数。 But didn't find any. 但是没找到。

MongoDB connection options used are added below, How can I set these options for JDBC client for PostgreSQL? 下面添加了使用的MongoDB连接选项,如何为PostgreSQL的JDBC客户端设置这些选项?

MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.threadsAllowedToBlockForConnectionMultiplier(1000);
builder.maxConnectionIdleTime(60* 1000 * 5);
builder.connectionsPerHost(100000);
MongoClientOptions options = builder.build();
mongoClient = new MongoClient(hostname, options);

In JDBC you pass a Properties object with some JDBC-standard properties ("user" and "password") and driver-specific properties, or pass the properties as part of the JDBC-url (with driver-specific properties and driver-specific syntax), or you configure things using a DataSource and its getters and setters. 在JDBC中,您传递带有某些JDBC标准属性(“用户”和“密码”)和特定于驱动程序的属性的Properties对象,或者将这些属性作为JDBC-url的一部分传递(具有特定于驱动程序的属性和特定于驱动程序的语法) ),或使用DataSource及其获取器和设置器配置事物。

For PostgreSQL JDBC refer to the section Connecting to the Database 对于PostgreSQL JDBC,请参考“ 连接到数据库 ”部分

For almost any serious usage of JDBC, you should not use DriverManager directly as it will create a new physical connection for each request. 对于几乎所有严重使用JDBC的用户,都不应直接使用DriverManager ,因为它将为每个请求创建一个新的物理连接。 Instead use a javax.sql.DataSource implementation that provides connection pooling, either provided by your driver (those usually aren't very good though), a third-party library like HikariCP, or one built into your JavaEE application server. 而是使用提供连接池的javax.sql.DataSource实现,该实现可以由驱动程序(通常不是很好)提供,也可以由HikariCP之类的第三方库提供,也可以由JavaEE应用程序服务器内置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM