Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sachin-handiekar/aabfbb952b5ef19d0e87fdff00539896 to your computer and use it in GitHub Desktop.
Save sachin-handiekar/aabfbb952b5ef19d0e87fdff00539896 to your computer and use it in GitHub Desktop.
what's the best setting for basicdatasource in production environment
ChatGPT
When configuring BasicDataSource (from Apache Commons DBCP) for a production environment, it's crucial to focus on settings that ensure stability, performance, and efficient resource utilization. Here are some recommended configurations:
Essential Configuration Parameters
Driver Class Name and Connection URL
java
Copy code
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setUrl("jdbc:mysql://hostname:port/database");
ds.setUsername("yourUsername");
ds.setPassword("yourPassword");
Initial Size
Initial number of connections created when the pool is started.
java
Copy code
ds.setInitialSize(10);
Maximum Active Connections (maxTotal)
Maximum number of active connections that can be allocated from this pool at the same time.
java
Copy code
ds.setMaxTotal(100);
Maximum Idle Connections (maxIdle)
Maximum number of connections that can sit idle in the pool.
java
Copy code
ds.setMaxIdle(30);
Minimum Idle Connections (minIdle)
Minimum number of connections that can sit idle in the pool without extra ones being created.
java
Copy code
ds.setMinIdle(10);
Max Wait Time for Connection (maxWaitMillis)
Maximum time to wait for a connection to be returned before throwing an exception.
java
Copy code
ds.setMaxWaitMillis(10000); // 10 seconds
Performance and Reliability Enhancements
Connection Validation
Ensure that the connections returned from the pool are valid.
java
Copy code
ds.setValidationQuery("SELECT 1");
ds.setTestOnBorrow(true);
ds.setTestOnReturn(false);
ds.setTestWhileIdle(true);
Idle Connection Validation Interval
How frequently to validate idle connections in the pool.
java
Copy code
ds.setTimeBetweenEvictionRunsMillis(5000); // 5 seconds
Minimum Time a Connection May Sit Idle Before It Is Eligible for Eviction (minEvictableIdleTimeMillis)
java
Copy code
ds.setMinEvictableIdleTimeMillis(60000); // 1 minute
Log Abandoned Connections
Log a stack trace of the code that abandoned a connection to assist with debugging.
java
Copy code
ds.setLogAbandoned(true);
ds.setRemoveAbandonedOnBorrow(true);
ds.setRemoveAbandonedOnMaintenance(true);
ds.setRemoveAbandonedTimeout(300); // 5 minutes
Example Configuration
Here is an example of how you might configure a BasicDataSource:
java
Copy code
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setUrl("jdbc:mysql://hostname:port/database");
ds.setUsername("yourUsername");
ds.setPassword("yourPassword");
ds.setInitialSize(10);
ds.setMaxTotal(100);
ds.setMaxIdle(30);
ds.setMinIdle(10);
ds.setMaxWaitMillis(10000);
ds.setValidationQuery("SELECT 1");
ds.setTestOnBorrow(true);
ds.setTestOnReturn(false);
ds.setTestWhileIdle(true);
ds.setTimeBetweenEvictionRunsMillis(5000);
ds.setMinEvictableIdleTimeMillis(60000);
ds.setLogAbandoned(true);
ds.setRemoveAbandonedOnBorrow(true);
ds.setRemoveAbandonedOnMaintenance(true);
ds.setRemoveAbandonedTimeout(300);
Additional Considerations
Monitoring: Integrate with monitoring tools to keep an eye on the performance and behavior of your connection pool.
Timeouts: Set appropriate timeouts to avoid potential hanging connections.
Security: Ensure that credentials are managed securely and that your database is properly secured against unauthorized access.
Scalability: Regularly review and adjust the connection pool settings based on the load and usage patterns.
Testing and Optimization
Before deploying to production, thoroughly test your configuration under load to ensure it meets your application's requirements. Monitor its performance and make adjustments as necessary based on real-world usage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment