Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java EE 8 High Performance

You're reading from   Java EE 8 High Performance Master techniques such as memory optimization, caching, concurrency, and multithreading to achieve maximum performance from your enterprise applications.

Arrow left icon
Product type Paperback
Published in Jan 2018
Publisher Packt
ISBN-13 9781788473064
Length 350 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Romain Manni-Bucau Romain Manni-Bucau
Author Profile Icon Romain Manni-Bucau
Romain Manni-Bucau
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Money – The Quote Manager Application FREE CHAPTER 2. Looking Under the Cover – What is This EE Thing? 3. Monitor Your Application 4. Application Optimization – Memory Management and Server Configuration 5. Scale Up – Threading and Implications 6. Be Lazy; Cache Your Data 7. Be Fault-Tolerant 8. Loggers and Performances – A Trade-Off 9. Benchmarking Your Application 10. Continuous Performance Evaluation 11. Another Book You May Enjoy

Setting up MySQL

All the previous parts will work transparently in Glassfish, as it can provide you with a default database if none is set since Java EE 7. This default database is an Apache Derby one for Glassfish. Considering that we will work on the performance soon, we want a recent production database. To ensure this, we will set up MySQL.

Assuming that you installed MySQL for your operating system and that it runs on localhost:3306 (the default), we need to create a new database. Let's call it quote_manager:

$ mysql -u root -p
Enter password: ******
...
mysql> create database quote_manager;
Query OK, 1 row affected (0.00 sec)

Now that we have a database, we can configure it in Glassfish and let JPA 2.2 create the tables for us based on our model. For this, we need to create glassfish-resources.xml in the WEB-INF folder of the war package (put it in src/main/webapp/WEB-INF in the Maven project):

<?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>
<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>
<jdbc-resource jndi-name="java:app/jdbc/quote_manager" pool-name="MySQLConnectinoPool" enabled="true"/>
</resources>

Alternatively, you can also do it through code using the @DataSourceDefinition annotation, which is more portable than the specific descriptor of GlassFish (this is the solution we will rely on from now on):

@DataSourceDefinition(
name = "java:app/jdbc/quote_manager",
className = "com.mysql.jdbc.Driver",
url = "jdbc:mysql://localhost:3306/quote_manager",
user = "root",
password = "password"
)
public class DataSourceConfiguration {
}

If you recompile and restart the server, you will see that it has created the tables, thanks to our persistence.xml configuration:

mysql> show tables;
+-------------------------+
| Tables_in_quote_manager |
+-------------------------+
| CUSTOMER |
| QUOTE |
| QUOTE_CUSTOMER |
| SEQUENCE |
+-------------------------+

If you are waiting for the server to start and have kept the provisioning activated, you will also see some data in the QUOTE table:

mysql> select * from QUOTE limit 10;
+----+-------+-------+
| ID | NAME | VALUE |
+----+-------+-------+
| 1 | FLWS | 9 |
| 2 | VNET | 5.19 |
| 3 | XXII | 2.2 |
| 4 | TWOU | 50.1 |
| 5 | DDD | 12.56 |
| 6 | MMM | 204.32|
| 7 | WBAI | 10.34 |
| 8 | JOBS | 59.4 |
| 9 | WUBA | 62.63 |
| 10 | CAFD | 14.42 |
+----+-------+-------+
You have been reading a chapter from
Java EE 8 High Performance
Published in: Jan 2018
Publisher: Packt
ISBN-13: 9781788473064
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime