Wildfly 11中EJB/JPA无法获取JDBC连接问题咨询(附数据源配置)
Let's walk through the most likely causes and fixes for your "unable to obtain JDBC connection" problem, using the partial datasource config you provided as a starting point.
1. Fix Incomplete/Incorrect Datasource Configuration
Your provided config is truncated (<...>), so let's cover the critical gaps first:
Validate the Connection URL
Your URLjdbc:oracle:thin:@*.*.*.*:1521:*uses placeholder values that need to be replaced with actual database details:- For Oracle SID: Use
jdbc:oracle:thin:@<DB_HOST>:1521:<SID> - For Oracle Service Name: Use
jdbc:oracle:thin:@//<DB_HOST>:1521/<SERVICE_NAME>
Leaving the*placeholders will prevent WildFly from resolving the target database entirely.
- For Oracle SID: Use
Add Missing Security Credentials
Your config doesn't include the required<security>block for database authentication. Without this, WildFly can't log into the database:<security> <user-name>your_database_username</user-name> <password>your_database_password</password> </security>
2. Verify Oracle Driver Deployment
WildFly 11 requires the Oracle JDBC driver to be properly configured:
- First, confirm the
<driver>oracle</driver>entry in your datasource matches the driver definition in yourstandalone.xml/domain.xmlunder the<drivers>section. It should look something like this:<driver name="oracle" module="com.oracle"> <driver-class>oracle.jdbc.OracleDriver</driver-class> <xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class> </driver> - Ensure the Oracle driver JAR is deployed as a module in WildFly (typically in
jboss/modules/system/layers/base/com/oracle/main) with a validmodule.xmlpointing to the JAR file.
3. Tune Connection Pool Parameters & Validate Connections
Your pool settings have a few areas to check and adjust:
- Initial vs Min Pool Size: You set
<initial-pool-size>5</initial-pool-size>and<min-pool-size>10</min-pool-size>. WildFly will automatically fill the pool tomin-pool-sizeon startup, which is fine—but make sure your database can support at least 10 concurrent connections. - Max Pool Size vs Database Limits: Your
<max-pool-size>500</max-pool-size>is quite large. Check your Oracle database'sprocessesandsessionsparameters—if the database's maximum allowed connections are less than 500, WildFly will fail to create new connections once the limit is hit. - Add Connection Validation: To avoid stale connections in the pool, add validation settings to automatically detect and remove bad connections:
<validation> <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/> <validate-on-match>true</validate-on-match> <background-validation>true</background-validation> <background-validation-millis>60000</background-validation-millis> </validation>
4. Check for Connection Leaks in EJB/JPA Code
If connections are being acquired but never released, your pool will eventually exhaust:
- Use container-managed Entity Managers: If you're using JPA, inject the Entity Manager with
@PersistenceContextinstead of manually creating it—this lets WildFly handle connection cleanup automatically. - Enable Connection Pool Statistics: Add
<statistics enabled="true"/>to your datasource config, then use the WildFly Management Console to monitorActiveCountandAvailableCount. IfActiveCountstays atmax-pool-sizewithout decreasing, you have a connection leak. - Audit code for unclosed resources: Ensure any manual JDBC connections or Entity Managers are closed in
finallyblocks to avoid leaks.
5. Verify Network & Database Availability
Rule out basic infrastructure issues:
- Test direct database connectivity: Use
sqlplusor another client to connect to the database with the same credentials and URL from the WildFly server—if this fails, the problem is with network access, database status, or user permissions. - Confirm database listener is running: Ensure the Oracle TNS listener is active on port 1521, and the WildFly server can reach the database host (use
pingortelnet <DB_HOST> 1521to test).
内容的提问来源于stack exchange,提问作者user1454294

