- Java EE 8 High Performance
- Romain Manni Bucau
- 349字
- 2021-06-30 19:14:29
DataSource configuration
To illustrate the configuration let's use the one we rely on in the quote manager: the datasource. As shown in Chapter 1, Money – 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:
- Plain configuration files (often XML based).
- A command line interface.
- A REST API.
- A UI. For instance, here is a screenshot of Glassfish JDBC pool configuration where you will find all the parameters we talked about:

- Linux運維之道(第3版)
- pcDuino開發實戰
- Linux系統架構與運維實戰
- Windows Server 2012 Hyper-V:Deploying the Hyper-V Enterprise Server Virtualization Platform
- Windows Vista基礎與應用精品教程
- Containerization with LXC
- 蘋果電腦玩全攻略 OS X 10.8 Mountain Lion
- 嵌入式Linux應用開發菜鳥進階
- Social Data Visualization with HTML5 and JavaScript
- Introduction to R for Quantitative Finance
- Windows 10從新手到高手
- 統信UOS應用開發進階教程
- Windows Server 2008組網技術與實訓(第3版)
- Linux 從入門到項目實踐(超值版)
- 鴻蒙HarmonyOS手機應用開發實戰