如何配置Wildfly 12或JBoss以适配MySQL Connector/J 8?
Hey there! Glad you got the connection working after digging through old docs—totally get that relief when something finally clicks. Since you’re wondering about optimal setups, here are some practical pointers I’ve picked up from production deployments:
Optimized Connection String Examples
The exact string depends on your database type, but here are common optimizations:
- MySQL/MariaDB:
jdbc:mysql://your-db-host:3306/your-database?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&serverTimezone=UTC&useSSL=true&verifyServerCertificate=true&allowPublicKeyRetrieval=falserewriteBatchedStatements=trueboosts bulk insert/update performanceuseSSL=trueensures secure connections in productionserverTimezone=UTCavoids timezone-related data inconsistencies
- PostgreSQL:
jdbc:postgresql://your-db-host:5432/your-database?sslmode=require&stringtype=unspecified&prepareThreshold=5sslmode=requireenforces TLS encryption for all connectionsprepareThreshold=5enables prepared statement caching after 5 repeated uses
WildFly Connection Pool Best Practices
Tweak your standalone.xml (or domain.xml) datasource config for better performance and reliability:
- Pool Sizing: Match to your application’s concurrent load—don’t overprovision (wastes resources) or underprovision (causes connection waits):
<datasource ...> <pool> <initial-size>5</initial-size> <max-pool-size>20</max-pool-size> <min-pool-size>5</min-pool-size> </pool> </datasource> - Connection Validation: Prevent stale, broken connections from being handed out to your app:
<validation> <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/> <background-validation>true</background-validation> <background-validation-millis>60000</background-validation-millis> </validation>- For PostgreSQL, use
org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionCheckerinstead
- For PostgreSQL, use
- Timeout & Leak Prevention: Clean up idle or abandoned connections to free up resources:
<pool> <idle-timeout-minutes>15</idle-timeout-minutes> <remove-abandoned>true</remove-abandoned> <remove-abandoned-timeout>60</remove-abandoned-timeout> </pool> - Statement Caching: Reduce overhead of repeated queries by caching prepared statements:
<statement> <prepared-statement-cache-size>100</prepared-statement-cache-size> <share-prepared-statements>true</share-prepared-statements> </statement>
Remember, these are starting points—monitor your WildFly admin console’s Datasource Metrics (active connections, wait counts, leak detections) to fine-tune based on your actual traffic. If you can share more details about your database type, transaction needs, or typical workload, I can refine these suggestions further!
内容的提问来源于stack exchange,提问作者pitchblack408

