SlideShare a Scribd company logo
Java MySQL Connector &
Connection Pool
Features & Optimization
Kenny Gryp
<kenny.gryp@percona.com>
April 14, 2015
@gryp
DISCLAIMER
Please excuse me for
not being a Java
developer
2
What I Don’t Like
• Brussels Sprouts
• Taxes
• Calories
• Java(’s chattiness)
3
MYSQL CONNECTORS
CONNECTION POOLS
4
MYSQL CONNECTORS
CONNECTION POOLS
Connectors
Configuring Connector
Creating A Database
Connection
Prepared Statements
Example Transaction
5
MySQL Connector/J & MariaDB Java
Client
• MySQL Connector/J
– Oracle
– 5.1.35 Latest
– Compatible with
• MySQL
• Percona Server
• MariaDB
6
MySQL Connector/J & MariaDB Java
Client
• MariaDB Java Client
• MariaDB
• Fork from Drizzle Connector
• Latest 1.1.8
• Compatible with
– MySQL
– Percona Server
– MariaDB
7
MySQL Connector/J Features
• Enterprise Plugin: Query Analyzer
• MySQL Fabric Integration
• Load Balancing
• Failover
• Replication
8
MYSQL CONNECTORS
CONNECTION POOLS
Connectors
Configuring
Connector
Creating A Database
Connection
Prepared Statements Example Transaction
9
Creating Connection
Connection con =
DriverManager.getConnection
(“jdbc:mysql://node2/employees?
user=connj&password=test");
Statement stmt = con.createStatement();
String query =
"select * from employees
where emp_no = 20000;";
ResultSet rs = stmt.executeQuery(query);
...
MariaDB:
jdbc:mariadb://node2/employees
?user=connj&password=test"
10
Creating Connection - Tomcat w. JDBC-
Pool
context.xml (local):
<Resource name="jdbc/test"
auth="Container"
type="javax.sql.DataSource"
username=“jdbc-pool" password="test"
driverClassName="com.mysql.jdbc.Driver"
url=“jdbc:mysql://node2:3306/employees”
/>
MariaDB:
driverClassName="org.mariadb.jdbc.Driver"
11
Creating Connection - JDBC URL
jdbc:mysql://node2:3306/employees?
useServerPrepStmts=true&...
12
MYSQL CONNECTORS
CONNECTION POOLS
Connectors
Configuring Connector
Creating A Database
Connection
Prepared Statements
Example Transaction
13
Connector/J - Creating Connection
SHOW VARIABLES WHERE
Variable_name ='language' OR…
SELECT
@@session.auto_increment_increment;
SET NAMES latin1;
SET character_set_results = NULL;
SET autocommit=1;
SET sql_mode=
'NO_ENGINE_SUBSTITUTION,STRICT_TRAN
S_TABLES';
14
MariaDB - Creating Connection
set autocommit=1;
USE employees;
show variables like 'sql_mode';
15
Creating Connection - Defaults
• Connector/J:
• MariaDB Java

Client:
16
Connector/J & MariaDB Java Client -
Verbosity
• Connector/J is more verbose when starting a
connection
• Usually not a problem:
– connection pools are commonly used

(more coming soon…)
– connections are reused
– Actually I like but not too much.
17
Optimization
• MariaDB Java Client vs MySQL Connector/J
• Prepared Statements
18
Connector Performance - SELECT 1
localhost, single threaded 19
QPS
0
4000
8000
12000
16000
localhost
ConnectorJ MariaDB
15.213
13.477
Connector Performance - MariaDB %faster
20
Faster%
5%
10%
15%
20%
MariaDB Connector +Speed% ConnectorJ
SELECT1 LO
13%
Connector Performance - MariaDB %faster
21
Faster%
5%
10%
15%
20%
MariaDB Connector +Speed% ConnectorJ
SELECT1 LO SELECT1 net pk range
0%
4%4%
13%
Benefit is relative!
MYSQL CONNECTORS
CONNECTION POOLS
Connectors
Configuring Connector
Creating A Database
Connection
Prepared Statements
Example Transaction
22
Client or Server Side Prepared Statements
• Server side Prepared statements:
– reduce network traffic
– query is already optimized on server.
• Support:
– MariaDB Java client only supports client
side
– Connector/J default in client side
23
Server Side Prepared Statements
PREPARE stmt1 FROM
select * from employees
where emp_no = ?;
EXECUTE # API CALL
select * from employees
where emp_no = 20000;
DEALLOCATE PREPARE stmt1;
24
Connector/J: Server Side Prepared
Statements
• useServerPrepStmts = false
– disabled by default
• Mind looking at:
– cachePrepStmts = false
• do PREPARE, EXECUTE, DEALLOCATE
every time…, 3 round trips?
– prepStmtCacheSize = 25
– prepStmtCacheSqlLimit = 256
• low defaults
25
https://bugs.mysql.com/bug.php?id=74932
Benchmark: Prepared Statements
26
QPS
900
1800
2700
3600
MariaDB CONN/J CONN/J SRVCONN/J SRV+Cache
3.506QPS
2.047QPS
3.342QPS3.400QPS
select * from employees_alotofindexes
where first_name='moss' and birth_date > "1954-06-14"
and gender="M" and hire_date > "1998-01-01"G
Cracked!!
27
MYSQL CONNECTORS
CONNECTION POOLS
Connectors
Configuring Connector
Creating A Database
Connection
Prepared Statements
Example Transaction
28
Connector/J Optimization + Default JDBC-
Pool
Connection con = ds.getConnection();
con.setTransactionIsolation
(Connection.TRANSACTION_READ_COMMITTED);
con.setAutoCommit(false);
PreparedStatement stmt =
con.prepareStatement("select * from
employees where emp_no = ?");
stmt.setInt(1, 20000);
ResultSet rs = stmt.executeQuery();
stmt.close();
rs.close();
con.commit();
con.close();
29
Connector/J Optimization + Default JDBC-
Pool
SET SESSION TRANSACTION
ISOLATION LEVEL READ COMMITTED;
SET autocommit=0;
# administrator command: Prepare;
select * from employees
where emp_no = 20000;
# administrator command: Close stmt;
commit;
30
Taxes
Connector/J Optimization
• useConfigs=maxPerformance
– cachePrepStmts=true
– cacheCallableStmts=true
– cacheServerConfiguration=true
– useLocalSessionState=true
– elideSetAutoCommits=true
– alwaysSendSetIsolation=false
– enableQueryTimeouts=false
31
Connector/J Optimization
• useLocalTransactionState=true

commit() / rollback()
32
Connector/J Optimization - Tuned
JDBC URL: useConfigs=maxPerformance&
useServerPrepStmts=true:
select * from employees
where emp_no = 20000;
commit;
33
MariaDB Java Client Optimization
SET SESSION TRANSACTION
ISOLATION LEVEL READ COMMITTED;
select * from employees
where emp_no = 20000;
COMMIT;
34
MariaDB Java Client Optimization - Code
if
(
con.getTransactionIsolation() !=
Connection.TRANSACTION_READ_COMMITTED
)
{
con.setTransactionIsolation
(Connection.TRANSACTION_READ_COMMITTED);
}
35
MariaDB Java Client Optimization -
Interceptors
SELECT @@tx_isolation;
select * from employees
where emp_no = 20000;
COMMIT;
Still @@tx_isolation. Now add JDBC Interceptor:
<Resource name="jdbc/test"
auth="Container"
factory=
"org.apache.tomcat.jdbc.pool.DataSourceFactory"
jdbcInterceptors="ConnectionState"
driverClassName="org.mariadb.jdbc.Driver"
url="jdbc:mysql://node2:3306/employees"/>
36
MariaDB Java Client Optimization -
Optimized!
select * from employees
where emp_no = 20000;
COMMIT;
37
Benchmark - Connector Concurrency -
SELECT 1 38
HikariCP-bench with JDBC Pool, 4 Threads, SELECT 1 (4,8,16,32
Pool Size)
QPS
17.500
35.000
52.500
70.000
MariaDB CONN/J
Benchmark - Connector Concurrency -
TRX 39
HikariCP-bench with JDBC Pool, 4 Threads, TRX (4,8,16,32 Pool
Size)
QPS
4.750
9.500
14.250
19.000
Conn/J MariaDB Conn/J Optim MariaDB Optim
MYSQL CONNECTORS
CONNECTION POOLS
40
MYSQL CONNECTORS
CONNECTION POOLS
Connection Pools
Issues
Resetting Environment
Testing Connectivity
Pool Sizing
Lingering Transactions
Analysis
Examples
Graceful Failover
Conn/J Extra Features
41
Java Connection Pools
– The Usual:
– C3P0
– Commons-DBCP (v1&v2)
– JDBC Pool (fork commons-DBCP)
– Out In The Wild:
– Vibur-DBCP
– HikariCP
– BoneCP
42
Java Connection Pools
– The Usual:
– C3P0
– Commons-DBCP (v1&v2)
– JDBC Pool (fork commons-DBCP)
– Out In The Wild:
– Vibur-DBCP
– HikariCP
– BoneCP
43
Connection Pool Key Points
• Connection Management
• Pool Sizing
• Connection Testing
• Avoid Lingering Transactions
44
MYSQL CONNECTORS
CONNECTION POOLS
Connection Pools
Issues
Resetting Environment
Testing Connectivity
Pool Sizing
Lingering Transactions
Analysis
Examples
Graceful Failover
Conn/J Extra Features
45
Connection Pool Issues
• Coming from DBA side, I do not like
‘chattiness’
46
Connection Pool Issues
47
Connection Pool Issues
48
Connection Pool Issues
49
Connection Pool Issues
50
WHY DOES IT HAVE TO DO
THAT?
Because of
‘application bugs’
51
Connection Pools - Why Chattiness
Examples
• *maybe* forgot to COMMIT / ROLLBACK
• wanting AUTOCOMMIT=1

but a previous TRX set it to 0
• Changing TRX Isolation Level
• Is connection still working?
52
MYSQL CONNECTORS
CONNECTION POOLS
Connection Pools
Issues
Resetting
Environment
Testing Connectivity
Pool Sizing
Lingering Transactions
Analysis
Examples
Graceful Failover
Conn/J Extra Features
53
Connection Pool - Resetting Status
54
JDBC-Pool C3P0 DBCP2 HikariCP
Rollback rollbackOnReturn=false autoCommitOnClose=false rollbackOnReturn

=true
Commit commitOnReturn=false autoCommitOnClose=false n/a n/a
Avoid see above forceIgnoreUnresolvedTransa
ctions=false
see above
Auto
Commit
Driver Driver enableAutoCommit

OnReturn=true
Driver
MYSQL CONNECTORS
CONNECTION POOLS
Connection Pools
Issues
Resetting Environment
Testing Connectivity
Pool Sizing
Lingering Transactions
Analysis
Examples
Graceful Failover
Conn/J Extra Features
55
Connection Pool - Testing
• Making sure the connection is still active
• If not, maybe reopen a connection
• Not recommended as DB
• However, applications:
• do not like errors
• do not retry gracefully
56
Connection Pool - Testing
• If connections REALLY need to be tested…
• do not specify test query like:
• SELECT 1
• SELECT * FROM DUAL
• Leave default, all of the connection pools
use:

JDBC4 isValid();
57
Connection Pool - Testing
58
JDBC-Pool C3P0 DBCP2 HikariCP
Test Before testOnBorrow=false testConnectionOnCheckOut

=false
testOnBorrow=false n/a
Test After testOnReturn=false testConnectionOnCheckIn

=false
testOnReturn=false n/a
Test While Idle testWhileIdle=false idleConnectionTestPeriod

=0
testWhileIdle=false n/a
JDBC4
isValid()
default default default jdbc4ConnectionTest

=true (default)
Query validationQuery

(isValid)
preferredTestQuery=null validationQuery

(isValid)
connectionTestQuery

=none
Interval? validationInterval=30000 n/a n/a n/a
Connection Pool - Testing
• JDBC: validationInterval=30s

WHY? It defeats the whole purpose!
59
MYSQL CONNECTORS
CONNECTION POOLS
Connection Pools
Issues
Resetting Environment
Testing Connectivity
Pool Sizing
Lingering Transactions
Analysis
Examples
Graceful Failover
Conn/J Extra Features
60
Connection Pool - Pool Sizing
• Funnelling on Application Level, is good
• Smaller Number is Better
• +- * CPU’s on DB
• maybe a bit more (waiting on IO…)
• all application servers combined
• Response Time vs Throughput
61
Connection Pool - Pool Sizing
62
JDBC-Pool C3P0 DBCP2 HikariCP
Amount of
Connections
maxActive=100 maxPoolSize=15 maxTotal=8 maximumPoolSize=10
Maximum Idle
Connections
maxIdle=100 maxIdleTime=0** maxIdle=8 n/a
Minimum Idle
Connections
minIdle=10 minPoolSize=3 minIdle=0 minimumIdle=max
Startup Size initialSize=10 initialPoolSize=3 initialSize=0 minimumIdle
MYSQL CONNECTORS
CONNECTION POOLS
Connection Pools
Issues
Resetting Environment
Testing Connectivity
Pool Sizing
Lingering
Transactions
Analysis
Examples
Graceful Failover
Conn/J Extra Features
63
Connection Pool - Avoid Lingering
Transactions
• Application forgets to return the connection
• Statements that take longer than …
• Avoid this!
• Fix Application
64
Connection Pool - Avoid Lingering
Transactions 65
KILL Warning
JDBC-Pool removeAbandoned=false

removeAbandonedTimeout=60

abandonWhenPercentageFull=0
suspectTimeout=0
C3P0 unreturnedConnectionTimeout=0 n/a
DBCP removeAbandoned=false

removeAbandonedTimeout=300

Only When: 

getNumIdle() < 2 and
getNumActive() > getMaxTotal() - 3)
n/a
HikariCP n/a leakDetectionThreshold=0
MYSQL CONNECTORS
CONNECTION POOLS
Connection Pools
Issues
Resetting Environment
Testing Connectivity
Pool Sizing
Lingering Transactions
Analysis
Examples
Graceful Failover
Conn/J Extra Features
66
Connection Pools - How To Look At
Workload?
• Slow Query Log
• tcpdump
• pt-query-digest
• Percona Cloud Tools
67
MYSQL CONNECTORS
CONNECTION POOLS
Connection Pools
Issues
Resetting Environment
Testing Connectivity
Pool Sizing
Lingering Transactions
Analysis
Examples
Graceful Failover
Conn/J Extra Features
68
Connection Pool - Example Transaction
Connection con = ds.getConnection();
con.setTransactionIsolation
(Connection.TRANSACTION_READ_COMMITTED);
con.setAutoCommit(false);
PreparedStatement stmt =
con.prepareStatement("select * from
employees where emp_no = ?");
stmt.setInt(1, 20000);
ResultSet rs = stmt.executeQuery();
stmt.close();
rs.close();
con.commit();
con.close();
69
For Connectors - RECAP
• MySQL Connector/J
• useConfigs=maxPerformance
• useServerPrepStmts=true
• MariaDB Java Client
• HikariCP: Built in
• JDBC-Pool:
jdbcInterceptors=“ConnectionState"
• Other Pools: UNKNOWN
70
Connection Pool - TRX JDBC
select * from employees
where emp_no = 20000;
commit;
71
200
Connection Pool - TRX C3P0
SET SESSION TRANSACTION
ISOLATION LEVEL READ COMMITTED;
SET autocommit=0;
select * from employees
where emp_no = 20000;
commit;
SET autocommit=1;
SET SESSION TRANSACTION
ISOLATION LEVEL REPEATABLE READ;
72
600
Connection Pool - TRX C3P0
mysql> set global
tx_isolation=“READ-COMMITTED”;
forceIgnoreUnresolvedTransactions=true
73
200
Connection Pool - TRX DBCP
SET autocommit=1;
# administrator command: Ping;
SET autocommit=0;
select * from employees
where emp_no = 20000;
commit;
rollback;
SET autocommit=1;
74
700
Connection Pool - TRX DBCP
testOnBorrow=false
rollbackOnReturn=false
enableAutoCommitOnReturn=false
jdbcUrl: useLocalTransactionState=true
75
200
Connection Pool - TRX HikariCP
SET SESSION TRANSACTION
ISOLATION LEVEL READ COMMITTED;
SET autocommit=0;
select * from employees
where emp_no = 20000;
commit;
SET autocommit=1;
SET SESSION TRANSACTION
ISOLATION LEVEL REPEATABLE READ;
76
600
Connection Pool - TRX HikariCP
mysql> set global
tx_isolation=“READ-COMMITTED”;
autoCommit=false
77
200
Connection Pools
78
MariaDB vs. Connector/J
79
MYSQL CONNECTORS
CONNECTION POOLS
Connection Pools
Issues
Resetting Environment
Testing Connectivity
Pool Sizing
Lingering Transactions
Analysis
Examples
Graceful Failover
Conn/J Extra Features
80
Connection Pools - Graceful Failover
81
Connection Pools - Graceful Failover
• HAProxy ‘stats socket’
/etc/haproxy/haproxy.cfg
global
. . .
stats socket /tmp/haproxy.sock level admin
• Disable Node
# echo "disable server database/node1" 

| socat stdio /tmp/haproxy.sock
82
Connection Pools - Graceful Failover
83
Connection Pools - Graceful Failover
• During ‘maintenance’, what do we do?
• KILL old connections?
• Wait until connections are closed? (Define
lifetimes?)
• Ignore it?
84
Connection Pools - Graceful Failover
• Some connection pools can close
connections gracefully, when idle.
• For ‘synchronous’ replication systems
• using JMX
• No Application Errors!
85
Method
JDBC-Pool purgeOnReturn()
C3P0 softResetAllUsers()
DBCP n/a
HikariCP softEvictConnections(),
suspendPool(), resumePool() <—- ASYNC
Connection Pools - Graceful Failover
86
Connection Pools - Graceful Failover
87
Connection Pools - Graceful Failover
88
Connection Pools - Graceful Failover
• 0 Application Errors
• Completely seamless
89
MYSQL CONNECTORS
CONNECTION POOLS
Connection Pools
Issues
Resetting Environment
Testing Connectivity
Pool Sizing
Lingering Transactions
Analysis
Examples
Graceful Failover
Conn/J Extra
Features
90
Connector/J - Extra Features
• Load Balancing
• jdbcUrl: ”jdbc:mysql:loadbalance://
node1,node2/db?
loadBalanceConnectionGroup=lb&

loadBalanceEnableJMX=true”
• loadBalanceStrategy 

(random/bestResponseTime)
• Failover
• ReplicationDriver (setReadOnly)
• Combining with Connection Pools is less useful
• Fabric
91
Java MySQL Connector & 

Connection Pool Optimization
• http://dev.mysql.com/doc/connector-j/en
• https://mariadb.com/kb/en/mariadb/client-libraries/mariadb-java-
client/
• http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
• http://www.mchange.com/projects/c3p0
• http://commons.apache.org/proper/commons-dbcp/
• https://github.com/brettwooldridge/HikariCP
92
MYSQL CONNECTORS
CONNECTION POOLS
Kenny Gryp
<kenny.gryp@percona.com>
November 4, 2014
@gryp

More Related Content

What's hot (20)

PPTX
PostgreSQL Database Slides
metsarin
 
ODP
Introduction to Spring Framework and Spring IoC
Funnelll
 
PDF
Modern SQL in Open Source and Commercial Databases
Markus Winand
 
PPTX
CQRS and Event Sourcing, An Alternative Architecture for DDD
Dennis Doomen
 
PPT
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
John Kanagaraj
 
PPTX
Spring Framework Petclinic sample application
Antoine Rey
 
PPTX
XML Key Management Protocol for Secure Web Service
Md. Hasan Basri (Angel)
 
PDF
Indexes and Indexing in Oracle 12c
Oren Nakdimon
 
PDF
IBM MQ - better application performance
MarkTaylorIBM
 
PPTX
WebSphere Application Server Liberty Profile and Docker
David Currie
 
PPT
MySql slides (ppt)
webhostingguy
 
PPTX
Nutanix - Expert Session - Metro Availability
Christian Johannsen
 
PDF
Migration to Oracle Multitenant
Jitendra Singh
 
PPTX
Practical examples of using extended events
Dean Richards
 
PPSX
Fast and Furious: Handling Edge Computing Data With Oracle 19c Fast Ingest an...
Jim Czuprynski
 
PPT
Jsp/Servlet
Sunil OS
 
PDF
[오픈소스컨설팅]Day #2 MySQL Tuning, Replication, Cluster
Ji-Woong Choi
 
PDF
MySQL Database Architectures - InnoDB ReplicaSet & Cluster
Kenny Gryp
 
PDF
Evolution of MongoDB Replicaset and Its Best Practices
Mydbops
 
PPT
OOUG: Oracle transaction locking
Kyle Hailey
 
PostgreSQL Database Slides
metsarin
 
Introduction to Spring Framework and Spring IoC
Funnelll
 
Modern SQL in Open Source and Commercial Databases
Markus Winand
 
CQRS and Event Sourcing, An Alternative Architecture for DDD
Dennis Doomen
 
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
John Kanagaraj
 
Spring Framework Petclinic sample application
Antoine Rey
 
XML Key Management Protocol for Secure Web Service
Md. Hasan Basri (Angel)
 
Indexes and Indexing in Oracle 12c
Oren Nakdimon
 
IBM MQ - better application performance
MarkTaylorIBM
 
WebSphere Application Server Liberty Profile and Docker
David Currie
 
MySql slides (ppt)
webhostingguy
 
Nutanix - Expert Session - Metro Availability
Christian Johannsen
 
Migration to Oracle Multitenant
Jitendra Singh
 
Practical examples of using extended events
Dean Richards
 
Fast and Furious: Handling Edge Computing Data With Oracle 19c Fast Ingest an...
Jim Czuprynski
 
Jsp/Servlet
Sunil OS
 
[오픈소스컨설팅]Day #2 MySQL Tuning, Replication, Cluster
Ji-Woong Choi
 
MySQL Database Architectures - InnoDB ReplicaSet & Cluster
Kenny Gryp
 
Evolution of MongoDB Replicaset and Its Best Practices
Mydbops
 
OOUG: Oracle transaction locking
Kyle Hailey
 

Viewers also liked (20)

PDF
Эффективная отладка репликации MySQL
Sveta Smirnova
 
ODP
Mastering InnoDB Diagnostics
guest8212a5
 
PDF
Reducing Risk When Upgrading MySQL
Kenny Gryp
 
PDF
Successful Scalability Principles - Part 1
Ronald Bradford
 
PDF
MySQL Server Defaults
Morgan Tocker
 
PDF
MHA (MySQL High Availability): Getting started & moving past quirks
Colin Charles
 
PDF
10x Performance Improvements - A Case Study
Ronald Bradford
 
PDF
MySQL InnoDB Cluster and Group Replication - OSI 2017 Bangalore
Sujatha Sivakumar
 
PDF
Why MySQL Replication Fails, and How to Get it Back
Sveta Smirnova
 
PDF
Using Apache Spark and MySQL for Data Analysis
Sveta Smirnova
 
PPTX
The nightmare of locking, blocking and isolation levels!
Boris Hristov
 
PDF
MySQL InnoDB Cluster - Group Replication
Frederic Descamps
 
PDF
A New Architecture for Group Replication in Data Grid
Editor IJCATR
 
PDF
Advanced Percona XtraDB Cluster in a nutshell... la suite
Kenny Gryp
 
PDF
MySQL Replication Performance Tuning for Fun and Profit!
Vitor Oliveira
 
PDF
MySQL High Availability Deep Dive
hastexo
 
PPT
Mysql展示功能与源码对应
zhaolinjnu
 
PDF
Advanced mysql replication techniques
Giuseppe Maxia
 
PDF
Lessons Learned: Troubleshooting Replication
Sveta Smirnova
 
PDF
Galera cluster for high availability
Mydbops
 
Эффективная отладка репликации MySQL
Sveta Smirnova
 
Mastering InnoDB Diagnostics
guest8212a5
 
Reducing Risk When Upgrading MySQL
Kenny Gryp
 
Successful Scalability Principles - Part 1
Ronald Bradford
 
MySQL Server Defaults
Morgan Tocker
 
MHA (MySQL High Availability): Getting started & moving past quirks
Colin Charles
 
10x Performance Improvements - A Case Study
Ronald Bradford
 
MySQL InnoDB Cluster and Group Replication - OSI 2017 Bangalore
Sujatha Sivakumar
 
Why MySQL Replication Fails, and How to Get it Back
Sveta Smirnova
 
Using Apache Spark and MySQL for Data Analysis
Sveta Smirnova
 
The nightmare of locking, blocking and isolation levels!
Boris Hristov
 
MySQL InnoDB Cluster - Group Replication
Frederic Descamps
 
A New Architecture for Group Replication in Data Grid
Editor IJCATR
 
Advanced Percona XtraDB Cluster in a nutshell... la suite
Kenny Gryp
 
MySQL Replication Performance Tuning for Fun and Profit!
Vitor Oliveira
 
MySQL High Availability Deep Dive
hastexo
 
Mysql展示功能与源码对应
zhaolinjnu
 
Advanced mysql replication techniques
Giuseppe Maxia
 
Lessons Learned: Troubleshooting Replication
Sveta Smirnova
 
Galera cluster for high availability
Mydbops
 
Ad

Similar to Java MySQL Connector & Connection Pool Features & Optimization (20)

PDF
JDBC in Servlets
Eleonora Ciceri
 
PDF
Overview Of JDBC
Mindfire Solutions
 
PDF
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
Marco Tusa
 
PDF
Accessing data through hibernate: what DBAs should tell to developers and vic...
Marco Tusa
 
PDF
High-Performance JDBC Voxxed Bucharest 2016
Vlad Mihalcea
 
PDF
Using advanced options in MariaDB Connector/J
MariaDB plc
 
PPT
MySQL Tech Tour 2015 - 5.7 Connector/J/Net
Mark Swarbrick
 
PDF
MySQL Connector/J Feature Review and How to Upgrade from Connector/J 5.1
Filipe Silva
 
PDF
Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004
derek_clark_ashmore
 
PDF
TWJUG August, MySQL JDBC Driver "Connector/J"
Ryusuke Kajiyama
 
PPTX
Jdbc ppt
AISHWARIYA1S
 
PPTX
Database connect
Yoga Raja
 
PPTX
Jdbc presentation
nrjoshiee
 
PPT
Jdbc
myrajendra
 
PPT
Jdbc
lathasiva
 
PPT
Plmce2015 java 101 - david bennett
David Bennett
 
PPT
Advance MySQL Docstore Features
sankalita chakraborty
 
PPT
High Performance Jdbc
Sam Pattsin
 
PPT
Basic Java Database Connectivity(JDBC)
suraj pandey
 
PPTX
3.java database connectivity
web360
 
JDBC in Servlets
Eleonora Ciceri
 
Overview Of JDBC
Mindfire Solutions
 
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
Marco Tusa
 
Accessing data through hibernate: what DBAs should tell to developers and vic...
Marco Tusa
 
High-Performance JDBC Voxxed Bucharest 2016
Vlad Mihalcea
 
Using advanced options in MariaDB Connector/J
MariaDB plc
 
MySQL Tech Tour 2015 - 5.7 Connector/J/Net
Mark Swarbrick
 
MySQL Connector/J Feature Review and How to Upgrade from Connector/J 5.1
Filipe Silva
 
Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004
derek_clark_ashmore
 
TWJUG August, MySQL JDBC Driver "Connector/J"
Ryusuke Kajiyama
 
Jdbc ppt
AISHWARIYA1S
 
Database connect
Yoga Raja
 
Jdbc presentation
nrjoshiee
 
Jdbc
lathasiva
 
Plmce2015 java 101 - david bennett
David Bennett
 
Advance MySQL Docstore Features
sankalita chakraborty
 
High Performance Jdbc
Sam Pattsin
 
Basic Java Database Connectivity(JDBC)
suraj pandey
 
3.java database connectivity
web360
 
Ad

More from Kenny Gryp (14)

PDF
MySQL Database Architectures - 2022-08
Kenny Gryp
 
PDF
MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11
Kenny Gryp
 
PDF
MySQL Operator for Kubernetes
Kenny Gryp
 
PDF
MySQL Database Architectures - 2020-10
Kenny Gryp
 
PDF
MySQL InnoDB Cluster / ReplicaSet - Tutorial
Kenny Gryp
 
PDF
MySQL Connectors 8.0.19 & DNS SRV
Kenny Gryp
 
PDF
MySQL InnoDB Cluster - New Features in 8.0 Releases - Best Practices
Kenny Gryp
 
PDF
MySQL Group Replication - Ready For Production? (2018-04)
Kenny Gryp
 
PDF
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Kenny Gryp
 
PDF
MySQL Group Replication
Kenny Gryp
 
PDF
Multi Source Replication With MySQL 5.7 @ Verisure
Kenny Gryp
 
PDF
MySQL Group Replication - HandsOn Tutorial
Kenny Gryp
 
PDF
Online MySQL Backups with Percona XtraBackup
Kenny Gryp
 
PDF
Percona XtraDB Cluster
Kenny Gryp
 
MySQL Database Architectures - 2022-08
Kenny Gryp
 
MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11
Kenny Gryp
 
MySQL Operator for Kubernetes
Kenny Gryp
 
MySQL Database Architectures - 2020-10
Kenny Gryp
 
MySQL InnoDB Cluster / ReplicaSet - Tutorial
Kenny Gryp
 
MySQL Connectors 8.0.19 & DNS SRV
Kenny Gryp
 
MySQL InnoDB Cluster - New Features in 8.0 Releases - Best Practices
Kenny Gryp
 
MySQL Group Replication - Ready For Production? (2018-04)
Kenny Gryp
 
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Kenny Gryp
 
MySQL Group Replication
Kenny Gryp
 
Multi Source Replication With MySQL 5.7 @ Verisure
Kenny Gryp
 
MySQL Group Replication - HandsOn Tutorial
Kenny Gryp
 
Online MySQL Backups with Percona XtraBackup
Kenny Gryp
 
Percona XtraDB Cluster
Kenny Gryp
 

Recently uploaded (20)

PPTX
big data eco system fundamentals of data science
arivukarasi
 
PPTX
Module-2_3-1eentzyssssssssssssssssssssss.pptx
ShahidHussain66691
 
PPTX
Data anlytics Hospitals Research India.pptx
SayantanChakravorty2
 
PPTX
美国史蒂文斯理工学院毕业证书{SIT学费发票SIT录取通知书}哪里购买
Taqyea
 
PPTX
RESEARCH-FINAL-GROUP-3, about the final .pptx
gwapokoha1
 
PDF
CT-2-Ancient ancient accept-Criticism.pdf
DepartmentofEnglishC1
 
PPTX
Krezentios memories in college data.pptx
notknown9
 
PPTX
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
 
PPTX
microservices-with-container-apps-dapr.pptx
vjay22
 
PDF
Orchestrating Data Workloads With Airflow.pdf
ssuserae5511
 
PDF
A Web Repository System for Data Mining in Drug Discovery
IJDKP
 
PDF
Predicting Titanic Survival Presentation
praxyfarhana
 
PDF
Blood pressure (3).pdfbdbsbsbhshshshhdhdhshshs
hernandezemma379
 
PPTX
Discrete Logarithm Problem in Cryptography (1).pptx
meshablinx38
 
DOCX
ACCOMPLISHMENT AS OF MAY 15 RCT ACCOMPLISHMENT AS OF MAY 15 RCT ACCOMPLISHMEN...
JoemarAgbayani1
 
PPTX
Generative AI Boost Data Governance and Quality- Tejasvi Addagada
Tejasvi Addagada
 
PDF
ilide.info-tg-understanding-culture-society-and-politics-pr_127f984d2904c57ec...
jed P
 
PPTX
Project_Update_Summary.for the use from PM
Odysseas Lekatsas
 
PPT
Reliability Monitoring of Aircrfat commerce
Rizk2
 
big data eco system fundamentals of data science
arivukarasi
 
Module-2_3-1eentzyssssssssssssssssssssss.pptx
ShahidHussain66691
 
Data anlytics Hospitals Research India.pptx
SayantanChakravorty2
 
美国史蒂文斯理工学院毕业证书{SIT学费发票SIT录取通知书}哪里购买
Taqyea
 
RESEARCH-FINAL-GROUP-3, about the final .pptx
gwapokoha1
 
CT-2-Ancient ancient accept-Criticism.pdf
DepartmentofEnglishC1
 
Krezentios memories in college data.pptx
notknown9
 
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
 
microservices-with-container-apps-dapr.pptx
vjay22
 
Orchestrating Data Workloads With Airflow.pdf
ssuserae5511
 
A Web Repository System for Data Mining in Drug Discovery
IJDKP
 
Predicting Titanic Survival Presentation
praxyfarhana
 
Blood pressure (3).pdfbdbsbsbhshshshhdhdhshshs
hernandezemma379
 
Discrete Logarithm Problem in Cryptography (1).pptx
meshablinx38
 
ACCOMPLISHMENT AS OF MAY 15 RCT ACCOMPLISHMENT AS OF MAY 15 RCT ACCOMPLISHMEN...
JoemarAgbayani1
 
Generative AI Boost Data Governance and Quality- Tejasvi Addagada
Tejasvi Addagada
 
ilide.info-tg-understanding-culture-society-and-politics-pr_127f984d2904c57ec...
jed P
 
Project_Update_Summary.for the use from PM
Odysseas Lekatsas
 
Reliability Monitoring of Aircrfat commerce
Rizk2
 

Java MySQL Connector & Connection Pool Features & Optimization

  • 1. Java MySQL Connector & Connection Pool Features & Optimization Kenny Gryp <[email protected]> April 14, 2015 @gryp
  • 2. DISCLAIMER Please excuse me for not being a Java developer 2
  • 3. What I Don’t Like • Brussels Sprouts • Taxes • Calories • Java(’s chattiness) 3
  • 5. MYSQL CONNECTORS CONNECTION POOLS Connectors Configuring Connector Creating A Database Connection Prepared Statements Example Transaction 5
  • 6. MySQL Connector/J & MariaDB Java Client • MySQL Connector/J – Oracle – 5.1.35 Latest – Compatible with • MySQL • Percona Server • MariaDB 6
  • 7. MySQL Connector/J & MariaDB Java Client • MariaDB Java Client • MariaDB • Fork from Drizzle Connector • Latest 1.1.8 • Compatible with – MySQL – Percona Server – MariaDB 7
  • 8. MySQL Connector/J Features • Enterprise Plugin: Query Analyzer • MySQL Fabric Integration • Load Balancing • Failover • Replication 8
  • 9. MYSQL CONNECTORS CONNECTION POOLS Connectors Configuring Connector Creating A Database Connection Prepared Statements Example Transaction 9
  • 10. Creating Connection Connection con = DriverManager.getConnection (“jdbc:mysql://node2/employees? user=connj&password=test"); Statement stmt = con.createStatement(); String query = "select * from employees where emp_no = 20000;"; ResultSet rs = stmt.executeQuery(query); ... MariaDB: jdbc:mariadb://node2/employees ?user=connj&password=test" 10
  • 11. Creating Connection - Tomcat w. JDBC- Pool context.xml (local): <Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource" username=“jdbc-pool" password="test" driverClassName="com.mysql.jdbc.Driver" url=“jdbc:mysql://node2:3306/employees” /> MariaDB: driverClassName="org.mariadb.jdbc.Driver" 11
  • 12. Creating Connection - JDBC URL jdbc:mysql://node2:3306/employees? useServerPrepStmts=true&... 12
  • 13. MYSQL CONNECTORS CONNECTION POOLS Connectors Configuring Connector Creating A Database Connection Prepared Statements Example Transaction 13
  • 14. Connector/J - Creating Connection SHOW VARIABLES WHERE Variable_name ='language' OR… SELECT @@session.auto_increment_increment; SET NAMES latin1; SET character_set_results = NULL; SET autocommit=1; SET sql_mode= 'NO_ENGINE_SUBSTITUTION,STRICT_TRAN S_TABLES'; 14
  • 15. MariaDB - Creating Connection set autocommit=1; USE employees; show variables like 'sql_mode'; 15
  • 16. Creating Connection - Defaults • Connector/J: • MariaDB Java
 Client: 16
  • 17. Connector/J & MariaDB Java Client - Verbosity • Connector/J is more verbose when starting a connection • Usually not a problem: – connection pools are commonly used
 (more coming soon…) – connections are reused – Actually I like but not too much. 17
  • 18. Optimization • MariaDB Java Client vs MySQL Connector/J • Prepared Statements 18
  • 19. Connector Performance - SELECT 1 localhost, single threaded 19 QPS 0 4000 8000 12000 16000 localhost ConnectorJ MariaDB 15.213 13.477
  • 20. Connector Performance - MariaDB %faster 20 Faster% 5% 10% 15% 20% MariaDB Connector +Speed% ConnectorJ SELECT1 LO 13%
  • 21. Connector Performance - MariaDB %faster 21 Faster% 5% 10% 15% 20% MariaDB Connector +Speed% ConnectorJ SELECT1 LO SELECT1 net pk range 0% 4%4% 13% Benefit is relative!
  • 22. MYSQL CONNECTORS CONNECTION POOLS Connectors Configuring Connector Creating A Database Connection Prepared Statements Example Transaction 22
  • 23. Client or Server Side Prepared Statements • Server side Prepared statements: – reduce network traffic – query is already optimized on server. • Support: – MariaDB Java client only supports client side – Connector/J default in client side 23
  • 24. Server Side Prepared Statements PREPARE stmt1 FROM select * from employees where emp_no = ?; EXECUTE # API CALL select * from employees where emp_no = 20000; DEALLOCATE PREPARE stmt1; 24
  • 25. Connector/J: Server Side Prepared Statements • useServerPrepStmts = false – disabled by default • Mind looking at: – cachePrepStmts = false • do PREPARE, EXECUTE, DEALLOCATE every time…, 3 round trips? – prepStmtCacheSize = 25 – prepStmtCacheSqlLimit = 256 • low defaults 25 https://bugs.mysql.com/bug.php?id=74932
  • 26. Benchmark: Prepared Statements 26 QPS 900 1800 2700 3600 MariaDB CONN/J CONN/J SRVCONN/J SRV+Cache 3.506QPS 2.047QPS 3.342QPS3.400QPS select * from employees_alotofindexes where first_name='moss' and birth_date > "1954-06-14" and gender="M" and hire_date > "1998-01-01"G
  • 28. MYSQL CONNECTORS CONNECTION POOLS Connectors Configuring Connector Creating A Database Connection Prepared Statements Example Transaction 28
  • 29. Connector/J Optimization + Default JDBC- Pool Connection con = ds.getConnection(); con.setTransactionIsolation (Connection.TRANSACTION_READ_COMMITTED); con.setAutoCommit(false); PreparedStatement stmt = con.prepareStatement("select * from employees where emp_no = ?"); stmt.setInt(1, 20000); ResultSet rs = stmt.executeQuery(); stmt.close(); rs.close(); con.commit(); con.close(); 29
  • 30. Connector/J Optimization + Default JDBC- Pool SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; SET autocommit=0; # administrator command: Prepare; select * from employees where emp_no = 20000; # administrator command: Close stmt; commit; 30 Taxes
  • 31. Connector/J Optimization • useConfigs=maxPerformance – cachePrepStmts=true – cacheCallableStmts=true – cacheServerConfiguration=true – useLocalSessionState=true – elideSetAutoCommits=true – alwaysSendSetIsolation=false – enableQueryTimeouts=false 31
  • 33. Connector/J Optimization - Tuned JDBC URL: useConfigs=maxPerformance& useServerPrepStmts=true: select * from employees where emp_no = 20000; commit; 33
  • 34. MariaDB Java Client Optimization SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; select * from employees where emp_no = 20000; COMMIT; 34
  • 35. MariaDB Java Client Optimization - Code if ( con.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED ) { con.setTransactionIsolation (Connection.TRANSACTION_READ_COMMITTED); } 35
  • 36. MariaDB Java Client Optimization - Interceptors SELECT @@tx_isolation; select * from employees where emp_no = 20000; COMMIT; Still @@tx_isolation. Now add JDBC Interceptor: <Resource name="jdbc/test" auth="Container" factory= "org.apache.tomcat.jdbc.pool.DataSourceFactory" jdbcInterceptors="ConnectionState" driverClassName="org.mariadb.jdbc.Driver" url="jdbc:mysql://node2:3306/employees"/> 36
  • 37. MariaDB Java Client Optimization - Optimized! select * from employees where emp_no = 20000; COMMIT; 37
  • 38. Benchmark - Connector Concurrency - SELECT 1 38 HikariCP-bench with JDBC Pool, 4 Threads, SELECT 1 (4,8,16,32 Pool Size) QPS 17.500 35.000 52.500 70.000 MariaDB CONN/J
  • 39. Benchmark - Connector Concurrency - TRX 39 HikariCP-bench with JDBC Pool, 4 Threads, TRX (4,8,16,32 Pool Size) QPS 4.750 9.500 14.250 19.000 Conn/J MariaDB Conn/J Optim MariaDB Optim
  • 41. MYSQL CONNECTORS CONNECTION POOLS Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features 41
  • 42. Java Connection Pools – The Usual: – C3P0 – Commons-DBCP (v1&v2) – JDBC Pool (fork commons-DBCP) – Out In The Wild: – Vibur-DBCP – HikariCP – BoneCP 42
  • 43. Java Connection Pools – The Usual: – C3P0 – Commons-DBCP (v1&v2) – JDBC Pool (fork commons-DBCP) – Out In The Wild: – Vibur-DBCP – HikariCP – BoneCP 43
  • 44. Connection Pool Key Points • Connection Management • Pool Sizing • Connection Testing • Avoid Lingering Transactions 44
  • 45. MYSQL CONNECTORS CONNECTION POOLS Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features 45
  • 46. Connection Pool Issues • Coming from DBA side, I do not like ‘chattiness’ 46
  • 51. WHY DOES IT HAVE TO DO THAT? Because of ‘application bugs’ 51
  • 52. Connection Pools - Why Chattiness Examples • *maybe* forgot to COMMIT / ROLLBACK • wanting AUTOCOMMIT=1
 but a previous TRX set it to 0 • Changing TRX Isolation Level • Is connection still working? 52
  • 53. MYSQL CONNECTORS CONNECTION POOLS Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features 53
  • 54. Connection Pool - Resetting Status 54 JDBC-Pool C3P0 DBCP2 HikariCP Rollback rollbackOnReturn=false autoCommitOnClose=false rollbackOnReturn
 =true Commit commitOnReturn=false autoCommitOnClose=false n/a n/a Avoid see above forceIgnoreUnresolvedTransa ctions=false see above Auto Commit Driver Driver enableAutoCommit
 OnReturn=true Driver
  • 55. MYSQL CONNECTORS CONNECTION POOLS Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features 55
  • 56. Connection Pool - Testing • Making sure the connection is still active • If not, maybe reopen a connection • Not recommended as DB • However, applications: • do not like errors • do not retry gracefully 56
  • 57. Connection Pool - Testing • If connections REALLY need to be tested… • do not specify test query like: • SELECT 1 • SELECT * FROM DUAL • Leave default, all of the connection pools use:
 JDBC4 isValid(); 57
  • 58. Connection Pool - Testing 58 JDBC-Pool C3P0 DBCP2 HikariCP Test Before testOnBorrow=false testConnectionOnCheckOut
 =false testOnBorrow=false n/a Test After testOnReturn=false testConnectionOnCheckIn
 =false testOnReturn=false n/a Test While Idle testWhileIdle=false idleConnectionTestPeriod
 =0 testWhileIdle=false n/a JDBC4 isValid() default default default jdbc4ConnectionTest
 =true (default) Query validationQuery
 (isValid) preferredTestQuery=null validationQuery
 (isValid) connectionTestQuery
 =none Interval? validationInterval=30000 n/a n/a n/a
  • 59. Connection Pool - Testing • JDBC: validationInterval=30s
 WHY? It defeats the whole purpose! 59
  • 60. MYSQL CONNECTORS CONNECTION POOLS Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features 60
  • 61. Connection Pool - Pool Sizing • Funnelling on Application Level, is good • Smaller Number is Better • +- * CPU’s on DB • maybe a bit more (waiting on IO…) • all application servers combined • Response Time vs Throughput 61
  • 62. Connection Pool - Pool Sizing 62 JDBC-Pool C3P0 DBCP2 HikariCP Amount of Connections maxActive=100 maxPoolSize=15 maxTotal=8 maximumPoolSize=10 Maximum Idle Connections maxIdle=100 maxIdleTime=0** maxIdle=8 n/a Minimum Idle Connections minIdle=10 minPoolSize=3 minIdle=0 minimumIdle=max Startup Size initialSize=10 initialPoolSize=3 initialSize=0 minimumIdle
  • 63. MYSQL CONNECTORS CONNECTION POOLS Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features 63
  • 64. Connection Pool - Avoid Lingering Transactions • Application forgets to return the connection • Statements that take longer than … • Avoid this! • Fix Application 64
  • 65. Connection Pool - Avoid Lingering Transactions 65 KILL Warning JDBC-Pool removeAbandoned=false
 removeAbandonedTimeout=60
 abandonWhenPercentageFull=0 suspectTimeout=0 C3P0 unreturnedConnectionTimeout=0 n/a DBCP removeAbandoned=false
 removeAbandonedTimeout=300
 Only When: 
 getNumIdle() < 2 and getNumActive() > getMaxTotal() - 3) n/a HikariCP n/a leakDetectionThreshold=0
  • 66. MYSQL CONNECTORS CONNECTION POOLS Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features 66
  • 67. Connection Pools - How To Look At Workload? • Slow Query Log • tcpdump • pt-query-digest • Percona Cloud Tools 67
  • 68. MYSQL CONNECTORS CONNECTION POOLS Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features 68
  • 69. Connection Pool - Example Transaction Connection con = ds.getConnection(); con.setTransactionIsolation (Connection.TRANSACTION_READ_COMMITTED); con.setAutoCommit(false); PreparedStatement stmt = con.prepareStatement("select * from employees where emp_no = ?"); stmt.setInt(1, 20000); ResultSet rs = stmt.executeQuery(); stmt.close(); rs.close(); con.commit(); con.close(); 69
  • 70. For Connectors - RECAP • MySQL Connector/J • useConfigs=maxPerformance • useServerPrepStmts=true • MariaDB Java Client • HikariCP: Built in • JDBC-Pool: jdbcInterceptors=“ConnectionState" • Other Pools: UNKNOWN 70
  • 71. Connection Pool - TRX JDBC select * from employees where emp_no = 20000; commit; 71 200
  • 72. Connection Pool - TRX C3P0 SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; SET autocommit=0; select * from employees where emp_no = 20000; commit; SET autocommit=1; SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ; 72 600
  • 73. Connection Pool - TRX C3P0 mysql> set global tx_isolation=“READ-COMMITTED”; forceIgnoreUnresolvedTransactions=true 73 200
  • 74. Connection Pool - TRX DBCP SET autocommit=1; # administrator command: Ping; SET autocommit=0; select * from employees where emp_no = 20000; commit; rollback; SET autocommit=1; 74 700
  • 75. Connection Pool - TRX DBCP testOnBorrow=false rollbackOnReturn=false enableAutoCommitOnReturn=false jdbcUrl: useLocalTransactionState=true 75 200
  • 76. Connection Pool - TRX HikariCP SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; SET autocommit=0; select * from employees where emp_no = 20000; commit; SET autocommit=1; SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ; 76 600
  • 77. Connection Pool - TRX HikariCP mysql> set global tx_isolation=“READ-COMMITTED”; autoCommit=false 77 200
  • 80. MYSQL CONNECTORS CONNECTION POOLS Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features 80
  • 81. Connection Pools - Graceful Failover 81
  • 82. Connection Pools - Graceful Failover • HAProxy ‘stats socket’ /etc/haproxy/haproxy.cfg global . . . stats socket /tmp/haproxy.sock level admin • Disable Node # echo "disable server database/node1" 
 | socat stdio /tmp/haproxy.sock 82
  • 83. Connection Pools - Graceful Failover 83
  • 84. Connection Pools - Graceful Failover • During ‘maintenance’, what do we do? • KILL old connections? • Wait until connections are closed? (Define lifetimes?) • Ignore it? 84
  • 85. Connection Pools - Graceful Failover • Some connection pools can close connections gracefully, when idle. • For ‘synchronous’ replication systems • using JMX • No Application Errors! 85 Method JDBC-Pool purgeOnReturn() C3P0 softResetAllUsers() DBCP n/a HikariCP softEvictConnections(), suspendPool(), resumePool() <—- ASYNC
  • 86. Connection Pools - Graceful Failover 86
  • 87. Connection Pools - Graceful Failover 87
  • 88. Connection Pools - Graceful Failover 88
  • 89. Connection Pools - Graceful Failover • 0 Application Errors • Completely seamless 89
  • 90. MYSQL CONNECTORS CONNECTION POOLS Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features 90
  • 91. Connector/J - Extra Features • Load Balancing • jdbcUrl: ”jdbc:mysql:loadbalance:// node1,node2/db? loadBalanceConnectionGroup=lb&
 loadBalanceEnableJMX=true” • loadBalanceStrategy 
 (random/bestResponseTime) • Failover • ReplicationDriver (setReadOnly) • Combining with Connection Pools is less useful • Fabric 91
  • 92. Java MySQL Connector & 
 Connection Pool Optimization • http://dev.mysql.com/doc/connector-j/en • https://mariadb.com/kb/en/mariadb/client-libraries/mariadb-java- client/ • http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html • http://www.mchange.com/projects/c3p0 • http://commons.apache.org/proper/commons-dbcp/ • https://github.com/brettwooldridge/HikariCP 92 MYSQL CONNECTORS CONNECTION POOLS Kenny Gryp <[email protected]> November 4, 2014 @gryp