Application-defined data sources
You can define a data source within your application, through annotations or in the deployment descriptor, as defined by the Java™ EE specification.
Note: The commonLibraryRef class loader
attribute is recommended for application defined data sources. The privateLibraryRef attribute cannot be used for the java:global namespace and is discouraged for the other scopes.
If multiple applications declare the same java:global namespace to specify the data source, the server.xml files of the applications must all specify a commonLibraryRef attribute to the same shared library.
When you define a data source in an application, the JDBC driver must be made available to the application. This definition is accomplished by configuring a shared library in the server.xml for your application.
For example:
<application id="myApp" name="myApp" location="myApp.war" type="war">
<classloader commonLibraryRef="DB2Lib"/>
</application>
<library id="DB2Lib">
<fileset dir="C:/DB2/java" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>
</library>
Then, you can define a data source in your application either through annotations or in the deployment descriptor.
- Use annotations as in the following example:
@DataSourceDefinition( name = "java:comp/env/jdbc/db2", className = "com.ibm.db2.jcc.DB2DataSource", databaseName = "SAMPLEDB", serverName = "localhost", portNumber = 50000, properties = { "driverType=4" }, user = "user1", password = "pwd1" ) public class MyServlet extends HttpServlet { @Resource(lookup="java:comp/env/jdbc/db2") DataSource ds;
- Use the deployment descriptor as in the following example, for
example, in a web.xml file:
<data-source> <name>java:comp/env/jdbc/db2</name> <class-name>com.ibm.db2.jcc.DB2DataSource</class-name> <server-name>localhost</server-name> <port-number>50000</port-number> <database-name>SAMPLEDB</database-name> <user>user1</user> <password>pwd1</password> <property><name>driverType</name><value>4</value></property> </data-source>
In general, properties that can be defined on dataSource or
connectionManager in the server.xml files can also be
specified on application defined data sources. Two exceptions to this are
connectionManagerRef and jdbcDriverRef, which you cannot specify
because the application defined data source implicitly defines the connection manager and JDBC
driver. When you use application defined data sources for two-phase commit, you can specify the
recoveryAuthDataRef property to select the authentication data that is used for
transaction recovery. However, it is important to be aware that recovery of transactions is only
possible while the application is running. You can use variables, encoded passwords, and duration
syntax in application defined data sources.
Note: The duration syntax does not apply to properties
that are explicitly defined in the annotation, such as loginTimeout or
maxIdleTime.
Here is an example of two data sources that use connection manager properties, variables, encoded passwords, and duration syntax.
@DataSourceDefinitions(value = {
@DataSourceDefinition(
name = "java:comp/env/jdbc/derby",
className = "org.apache.derby.jdbc.EmbeddedDataSource40",
databaseName = "${shared.resource.dir}/data/SAMPLEDB",
minPoolSize = 1,
maxPoolSize = 10,
maxIdleTime = 180,
properties = { "agedTimeout=10m", "connectionTimeout=30s", "createDatabase=create" }
),
@DataSourceDefinition(
name = "java:comp/env/jdbc/oracle",
className = "oracle.jdbc.pool.OracleDataSource",
url = "jdbc:oracle:thin:@//localhost:1521/SAMPLEDB",
user = "user1",
password = "{xor}Oz0vKDtt"
)
})