简体   繁体   中英

Setting connection options for PostgreSQL JDBC connection from Java

My application was using Mongo DB earlier. Now, I'm shifting to 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. In some way, I want to set these options with JDBC for PostgreSQL also.

I've tried and searched the same functions in JDBC DriverManager class. But didn't find any.

MongoDB connection options used are added below, How can I set these options for JDBC client for PostgreSQL?

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.

For PostgreSQL JDBC refer to the section Connecting to the Database

For almost any serious usage of JDBC, you should not use DriverManager directly as it will create a new physical connection for each request. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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