Java EE 8 High Performance
上QQ阅读APP看书,第一时间看更新

DataSource configuration

To illustrate the configuration let's use the one we rely on in the quote manager: the datasource. As shown in Chapter 1Money – The Quote Manager Application you can define the datasource this way:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN"
"http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>

<1>
<jdbc-connection-pool allow-non-component-callers="false"
associate-with-thread="false"
connection-creation-retry-attempts="0"
connection-creation-retry-interval-in-seconds="10"
connection-leak-reclaim="false"
connection-leak-timeout-in-seconds="0"
connection-validation-method="auto-commit"
datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
fail-all-connections="false"
idle-timeout-in-seconds="300"
is-connection-validation-required="false"
is-isolation-level-guaranteed="true"
lazy-connection-association="false"
lazy-connection-enlistment="false"
match-connections="false"
max-connection-usage-count="0"
max-pool-size="10"
max-wait-time-in-millis="120000"
name="MySQLConnectinoPool"
non-transactional-connections="false"
pool-resize-quantity="2"
res-type="javax.sql.DataSource"
statement-timeout-in-seconds="-1"
steady-pool-size="8"
validate-atmost-once-period-in-seconds="0"
validation-table-name="DUAL" wrap-jdbc-objects="false">
<property name="URL" value="jdbc:mysql://localhost:3306/quote_manager"/>
<property name="User" value="root"/>
<property name="Password" value="password"/>
</jdbc-connection-pool>

<2>
<jdbc-resource jndi-name="java:app/jdbc/quote_manager" pool-name="MySQLConnectinoPool" enabled="true"/>
</resources>

This XML configuration defines the datasource our JPA provider will use thanks to two declarations allowing the container to create the datasource instance and allowing the JPA provider to find this datasource:

  • The pool definition which defines how the database connections will be created, cached and validated
  • The link between the pool and the application through its JNDI name to let the application use it - this is how JPA will look up the instance

The properties are the datasource instance (based on the configured class) configuration but the jdbc-connection-pool attributes are mostly the pool configuration.

It is very important to note that the configuration depends on the server. As an example, in Wildly, you would use this kind of declaration:

<datasources>
  <xa-datasource jndi-name="java:jboss/quote_manager" pool-name="QuoteManagerPool">
    <driver>mysql</driver>
    <xa-datasource-property name="ServerName">localhost</xa-datasource-property>
    <xa-datasource-property name="DatabaseName">quote_manager</xa-datasource-property>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>50</max-pool-size>
</pool> <security> <user-name>root</user-name> <password>secret</password> </security> <validation> <valid-connection-checker class-
name=
"org.jboss.jca.adapters.jdbc.extensions.mysql
.MySQLValidConnectionChecker"
></valid-connection-checker> <exception-sorter class-
name=
"org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"></exception-sorter> </validation> </xa-datasource> <drivers> <driver name="mysql" module="com.mysql"> <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class> </driver> </drivers> </datasources>

Here again we find a property part and a pool part. Still, it is no more in attributes but with plain tags.

In Apache TomEE the same resource declaration looks like:

<Resource id="quote_manager" type="DataSource">
JdbcDriver = com.mysql.jdbc.Driver
JdbcUrl = jdbc:mysql://localhost:3306/quote_manager?tcpKeepAlive=true
UserName = root
Password = secret
ValidationQuery = SELECT 1
ValidationInterval = 30000
NumTestsPerEvictionRun = 5
TimeBetweenEvictionRuns = 30 seconds
TestWhileIdle = true
MaxActive = 50
</Resource>

Here the configuration is not fully XML but it is mixed with properties (as java.util.Properties) that contains the pool configuration and connection information which will be passed either to tomcat-jdbc or commons-dbcp2 pooling library.

What is interesting to note is the overall idea. Most of the servers share the same kind of configuration and here are the crucial configuration entries you need to care about:

Last note about resources is that most servers allow multiple ways to configure them:

  1. Plain configuration files (often XML based).
  2. A command line interface.
  3. A REST API.
  4. A UI. For instance, here is a screenshot of Glassfish JDBC pool configuration where you will find all the parameters we talked about: