How to specify Connection Properties In The Datasource Configuration for JBoss EAP
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 6
- 7
Issue
- How to specify connection properties (i.e. vendor driver specific properties) in the datasource configuration for JBoss EAP 6 / 7
- In EAP 5 we used to set them like this on the datasource:
<connection-property name"vendorProperty">true</connection-property>
- How can Oracle connection parameters be passed to the datasource?
Resolution
Connection properties must be handled differently depending on which of the following is used to establish connections to the database:
- Content from docs.oracle.com is not included.java.sql.Driver
- Content from docs.oracle.com is not included.javax.sql.DataSource or Content from docs.oracle.com is not included.javax.sql.XADataSource
When not defining an XA pool, the simplest alternative (recommended in most cases) is to leverage the vendor supplied java.sql.Driver implementation because the java.sql.Driver API provides a Content from docs.oracle.com is not included.standard mechanism to specify arbitrary properties as name value pairs. This approach will be used by default (for non-XA pools) if no datasource-class is explicitly added to the configuration. If the JDBC driver JAR does not provide a suitable default using the META-INF/services/java.sql.Driver file, a driver-class may be specified explicitly.
Configuration when using implementations of java.sql.Driver
When using a driver class (the default if no datasource-class or xa-datasource-class is specified in the configuration), EAP will pass connection-property entries as name-value pairs in a properties object supplied as the second parameter supported by the Content from docs.oracle.com is not included.java.sql.Driver.connect(String url, Properties properties) API.
Connection properties can be set in the datasource configuration section in the server configuration file (standalone.xml or domain.xml):
<subsystem xmlns="urn:jboss:domain:datasources:1.1">
<datasources>
<datasource jndi-name="java:/MySqlDS" pool-name="MySqlDS_Pool" enabled="true">
<connection-url>jdbc:mysql://localhost:3306/jboss</connection-url>
<connection-property name="vendorProperty">someValue</connection-property>
<connection-property name="char.encoding">UTF-8</connection-property>
...
</subsystem>
Using CLI:
/subsystem=datasources/data-source=MySqlDS_Pool/connection-properties=vendorProperty:add(value=someValue)
Note that char.encoding in the above configuration will fail if not using a vendor provided implemention of java.sql.Driver.
Configuration when using implementations of javax.sql.DataSource or javax.sql.XADataSource
Neither the javax.sql.DataSource nor javax.sql.XADataSource interface provides a standard interface for setting connection properties. As a means of working around this limitation, when these interfaces are used, JBoss will Content from github.com is not included.attempt to find 'set' methods which correspond to the named properties specified. This will work only if all properties have named setters in the underlying datasource implementation:
- Simple properties
- For a property named
systemthis means that the underlying vendor provided datasource class would need to provide a method with the signaturesetSystem(String). - For a property named
URLthis means that the underlying vendor provided datasource class would need to provide a method with the signaturesetURL(String).
- For a property named
java.util.Propertiesbased setter-
By using reflection, JBoss is able to translate the below into a call to
<DataSource>.setConnectionProperties(Properties)(e.g. with Oracle) passing in ajava.util.Propertiesinstance containing one or more name/value pairs which may be supplied in a comma-separated list (if more than one property is to be passed). -
For an
xa-datasourcepool:<xa-datasource-property name="ConnectionProperties">some.property.x=valueOne,some.property.y=valueTwo,some.property.z=valueThree</xa-datasource-property> -
For a (non-xa)
datasourcepool:<connection-property name="ConnectionProperties">some.property.x=valueOne,some.property.y=valueTwo,some.property.z=valueThree</connection-property> -
NOTE There must be no whitespace (no space, newline, tab, etc. characters) within the name-value pairs nor should there be any leading or trailing whitespace around the value string.
-
- If a
set<PropertyName>method Content from github.com is not included.cannot be found an exception should be thrown both for Content from github.com is not included.XA and Content from github.com is not included.non-XA datasources.
This approach may be problematic in cases where properties must be set using a custom API that does not correspond to the name of the property (e.g. a DataSource implementation does not provide a setChar.encoding(String) method that can correspond to a property named "char.encoding" like that mentioned in the java.sql.Driver example above).
Additional References
- Specifying driver-class or datasource-class for non-XA JDBC datasource pools in JBoss EAP
- Configure JBoss EAP Datasources
Diagnostic Steps
Byteman rules may often be used to verify what is passed to the vendor specific implementation of java.sql.Driver.connect(...) or to set<Property>(...) methods of the underlying implementation of javax.sql.DataSource or javax.sql.XADataSource. Consult relevant 3rd party vendor documentation for the JDBC driver/version to verify the exact interface or class and method names.
-
The rule below could be used to verify what is supplied to an arbitrary 3rd party JDBC implementation of
Driver.connect(...)RULE java.sql.Driver.connect INTERFACE ^java.sql.Driver METHOD connect IF true # Show url (the first parameter) and other properties provided to Driver.connect(String url, Properties properties) DO traceStack("[BMAN] Driver.connect(" + $1 + ",[" + $2 + "])\n", 15); ENDRULE -
The rule below could be used to verify what is supplied to an Oracle datasource
setURL(...)method:RULE oracle.jdbc.datasource.OracleCommonDataSource.setURL INTERFACE ^oracle.jdbc.datasource.OracleCommonDataSource METHOD setURL IF true DO traceStack("[BMAN] setURL(" + $1 + ")\n", 15); ENDRULE -
The rule below could be to verify what is suplied to an Oracle datasource
setConnectionProperties(...)methodRULE oracle.jdbc.datasource.OracleCommonDataSource.setConnectionProperties INTERFACE ^oracle.jdbc.datasource.OracleCommonDataSource METHOD setConnectionProperties IF true DO traceStack("[BMAN] setConnectionProperties(" + $1 + ")\n", 15); ENDRULE
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.