- 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:

- Arch Linux Environment Setup How-to
- 精解Windows8
- 混沌工程:復(fù)雜系統(tǒng)韌性實(shí)現(xiàn)之道
- Java EE 8 Design Patterns and Best Practices
- 嵌入式實(shí)時操作系統(tǒng):RT-Thread設(shè)計(jì)與實(shí)現(xiàn)
- Windows 8實(shí)戰(zhàn)從入門到精通(超值版)
- Building Telephony Systems With Asterisk
- Linux操作系統(tǒng)
- iOS 10快速開發(fā):18天零基礎(chǔ)開發(fā)一個商業(yè)應(yīng)用
- 應(yīng)急指揮信息系統(tǒng)設(shè)計(jì)
- Linux內(nèi)核修煉之道
- Microsoft Azure Administrator:Exam Guide AZ-103
- Linux網(wǎng)絡(luò)操作系統(tǒng)項(xiàng)目教程(RHEL 6.4/CentOS 6.4)(第2版)
- PrimeFaces Beginner's Guide
- 用“芯”探核:基于龍芯的Linux內(nèi)核探索解析