Java MySQL Connector & Connection Pool



Similar documents
The JAVA Way: JDBC and SQLJ

HP NonStop JDBC Type 4 Driver Performance Tuning Guide for Version 1.0

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Java DB Performance. Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

Kony MobileFabric. Sync Server Tomcat Installation Manual. On-Premises

tc Server Administration Guide

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

ITG Software Engineering

WSO2 Business Process Server Clustering Guide for 3.2.0

Using Apache Derby in the real world

MAGENTO HOSTING Progressive Server Performance Improvements

Detecting (and even preventing) SQL Injection Using the Percona Toolkit and Noinject!

Configuring Apache Derby for Performance and Durability Olav Sandstå

Course Description. Course Audience. Course Outline. Course Page - Page 1 of 5

Architecting ColdFusion For Scalability And High Availability. Ryan Stewart Platform Evangelist

Synchronous multi-master clusters with MySQL: an introduction to Galera

Blackboard Learn TM, Release 9 Technology Architecture. John Fontaine

Galera Replication. Synchronous Multi-Master Replication for InnoDB. ...well, why not for any other DBMS as well. Seppo Jaakola Alexey Yurchenko

High-Availability. Configurations for Liferay Portal. James Min. Senior Consultant / Sales Engineer, Liferay, Inc.

TimesTen Auditing Using ttaudit.java

Performance Tuning for the JDBC TM API

Configuring Apache Derby for Performance and Durability Olav Sandstå

Oracle WebLogic Server 11g Administration

How To Choose Between A Relational Database Service From Aws.Com

Applets, RMI, JDBC Exam Review

JBoss Seam Performance and Scalability on Dell PowerEdge 1855 Blade Servers

CHAPTER 1 - JAVA EE OVERVIEW FOR ADMINISTRATORS

Dave Stokes MySQL Community Manager

Configuring Apache HTTP Server With Pramati

Using MySQL for Big Data Advantage Integrate for Insight Sastry Vedantam

MySQL always-up with Galera Cluster

StoreGrid Backup Server With MySQL As Backend Database:

WebSphere Architect (Performance and Monitoring) 2011 IBM Corporation

WEBLOGIC ADMINISTRATION

MySQL Performance Blog. Measuring failover time for ScaleArc load balancer

JasperServer Localization Guide Version 3.5

Open Source DBMS CUBRID 2008 & Community Activities. Byung Joo Chung bjchung@cubrid.com

CDH installation & Application Test Report

Advanced Liferay Architecture: Clustering and High Availability

MySQL: Cloud vs Bare Metal, Performance and Reliability

HP OO 10.X - SiteScope Monitoring Templates

Informatica Corporation Proactive Monitoring for PowerCenter Operations Version 3.0 Release Notes May 2014

Various Load Testing Tools

White Paper. Cloud Native Advantage: Multi-Tenant, Shared Container PaaS. Version 1.1 (June 19, 2012)

This document summarizes the steps of deploying ActiveVOS on the IBM WebSphere Platform.

Enterprise Manager. Version 6.2. Installation Guide

NGASI AppServer Manager SaaS/ASP Hosting Automation for Cloud Computing Administrator and User Guide

Package sjdbc. R topics documented: February 20, 2015

MySQL performance in a cloud. Mark Callaghan

Configuring an Alternative Database for SAS Web Infrastructure Platform Services

High Availability Using MySQL in the Cloud:

Application Development A Cocktail of Java and MCP. MCP Guru Series Dan Meyer & Pramod Nair

KillTest. 半 年 免 费 更 新 服 务

SQL Anywhere 12 New Features Summary

CA Workload Automation Agent for Databases

Load balancing MySQL with HaProxy. Peter Boros Percona 4/23/13 Santa Clara, CA

Performance Monitoring API for Java Enterprise Applications

Switch to MySQLnd Already!

Configuring Nex-Gen Web Load Balancer

ColdFusion 8. Performance Tuning, Multi-Instance Management and Clustering. Sven Ramuschkat MAX 2008 Milan

Deployment Checklist. Liferay Portal 6.1 Enterprise Edition

Flexible Routing and Load Control on Back-End Servers. Controlling the Request Load and Quality of Service

Failover Support. DataDirect Connect for ODBC Drivers. Introduction. Connection Failover

Installation Guide for contineo

LICENSE4J AUTO LICENSE GENERATION AND ACTIVATION SERVER USER GUIDE

Andrew Moore Amsterdam 2015

Learn Oracle WebLogic Server 12c Administration For Middleware Administrators

High-availability with Galera Cluster for MySQL

FAQ: Data Services Real Time Set Up

MyOra 3.5. User Guide. SQL Tool for Oracle. Kris Murthy

Configuring Microsoft IIS 5.0 With Pramati Server

SQL and programming languages

CSc 230 Software System Engineering FINAL REPORT. Project Management System. Prof.: Doan Nguyen. Submitted By: Parita Shah Ajinkya Ladkhedkar

Alfresco Enterprise on AWS: Reference Architecture

MySQL synchronous replication in practice with Galera

High Availability Solutions for the MariaDB and MySQL Database

Scaling Database Performance in Azure

SOLUTION BRIEF. Advanced ODBC and JDBC Access to Salesforce Data.

JBS-102: Jboss Application Server Administration. Course Length: 4 days

TG Web. Technical FAQ

Weaving Stored Procedures into Java at Zalando

.NET UI Load Balancing & Failover

Performance Optimization of Teaching Web Application based SSH Framework

No.1 IT Online training institute from Hyderabad URL: sriramtechnologies.com

Oracle9i Data Guard: SQL Apply Best Practices. An Oracle White Paper September 2003

How to deploy console cable to connect WIAS-3200N and PC, to reset setting or check status via console

Veritas CommandCentral Disaster Recovery Advisor Release Notes 5.1

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

Qualogy M. Schildmeijer. Whitepaper Oracle Exalogic FMW Optimization

Set Up BA Server and Tools

agileworkflow Manual 1. agileworkflow 2. The repository 1 of 29 Contents Definition

Best Practices for Using MySQL in the Cloud

How To Use The Listerv Maestro With A Different Database On A Different System (Oracle) On A New Computer (Orora)

.NET UI Load Balancing & Clustering

How To Improve Performance On An Asa 9.4 Web Application Server (For Advanced Users)

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

Transcription:

Java MySQL Connector & Connection Pool Features & Optimization Kenny Gryp <kenny.gryp@percona.com> November 4, 2014 @gryp

2 Please excuse me for not being a Java developer DISCLAIMER

What I Don t Like 3 Brussels Sprouts Taxes Calories Java( s chattiness)

4 MYSQL CONNECTORS CONNECTION POOLS

5 Connectors Configuring Connector Creating A Database Connection Prepared Statements Example Transaction MYSQL CONNECTORS CONNECTION POOLS

MySQL Connector/J & MariaDB Java Client 6 MySQL Connector/J Oracle 5.1.33 Latest Compatible with MySQL Percona Server MariaDB

MySQL Connector/J & MariaDB Java Client 7 MariaDB Java Client MariaDB Fork from Drizzle Connector Latest 1.1.7 Compatible with MySQL Percona Server MariaDB

MySQL Connector/J Features 8 Enterprise Plugin: Query Analyzer MySQL Fabric Integration Load Balancing Failover Replication

9 Connectors Configuring Connector Creating A Database Connection Prepared Statements Example Transaction MYSQL CONNECTORS CONNECTION POOLS

Creating Connection 10 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"

Creating Connection - Tomcat w. JDBC- Pool 11 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"!

Creating Connection - JDBC URL 12! jdbc:mysql://node2:3306/employees? useserverprepstmts=true&...!!

13 Connectors Configuring Connector Creating A Database Connection Prepared Statements Example Transaction MYSQL CONNECTORS CONNECTION POOLS

Connector/J - Creating Connection 14 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';!

MariaDB - Creating Connection 15 set autocommit=1;! USE employees;! show variables like 'sql_mode';!

Creating Connection - Defaults 16 Connector/J:!!!! MariaDB Java Client:

Connector/J & MariaDB Java Client - Verbosity 17 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.

Optimization 18 MariaDB Java Client vs MySQL Connector/J Prepared Statements

Connector Performance - SELECT 1 localhost 19 16000 12000 13.477 15.213 QPS 8000 4000 0 ConnectorJ MariaDB localhost

Connector Performance - MariaDB %faster 20 20% 15% 13% Faster % 10% 5% SELECT1 LO MariaDB Connector +Speed% ConnectorJ

Connector Performance - MariaDB %faster 21 20% 15% 13% Faster % 10% 5% 4% 4% SELECT1 LO SELECT1 net pk range MariaDB Connector +Speed% ConnectorJ 0%

22 Connectors Configuring Connector Creating A Database Connection Prepared Statements Example Transaction MYSQL CONNECTORS CONNECTION POOLS

Client or Server Side Prepared Statements 23 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

Server Side Prepared Statements 24 PREPARE stmt1 FROM!! select * from employees!!! where emp_no =?;! EXECUTE # API CALL!! select * from employees!!! where emp_no = 20000;! DEALLOCATE PREPARE stmt1;

Connector/J: Server Side Prepared Statements 25 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

Benchmark: Prepared Statements 26 3600 3.400QPS 3.342QPS 3.506QPS 2700 2.047QPS QPS 1800 900 MariaDB CONN/J CONN/J SRV CONN/J SRV+Cache 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

28 Connectors Configuring Connector Creating A Database Connection Prepared Statements Example Transaction MYSQL CONNECTORS CONNECTION POOLS

Connector/J Optimization + Default JDBC- Pool 29 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();

Connector/J Optimization + Default JDBC- Pool 30 SET SESSION TRANSACTION!!! ISOLATION LEVEL READ COMMITTED;!! SET autocommit=0;!! # administrator command: Prepare;!! select * from employees!!!! where emp_no = 20000;! Taxes! # administrator command: Close stmt;!! commit;

Connector/J Optimization 31 useconfigs=maxperformance! cacheprepstmts=true! cachecallablestmts=true! cacheserverconfiguration=true! uselocalsessionstate=true! elidesetautocommits=true! alwayssendsetisolation=false! enablequerytimeouts=false

Connector/J Optimization 32 uselocaltransactionstate=true commit() / rollback()

Connector/J Optimization - Tuned 33 JDBC URL: useconfigs=maxperformance&! useserverprepstmts=true:!! select * from employees!! where emp_no = 20000;! commit;

MariaDB Java Client Optimization 34 SET SESSION TRANSACTION!!! ISOLATION LEVEL READ COMMITTED;!! select * from employees!!!! where emp_no = 20000;!! COMMIT;!

MariaDB Java Client Optimization - Code 35 if! (! con.gettransactionisolation()!=!!!! Connection.TRANSACTION_READ_COMMITTED! )! {! con.settransactionisolation!!! (Connection.TRANSACTION_READ_COMMITTED);! }

MariaDB Java Client Optimization - Interceptors 36 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"/>!

MariaDB Java Client Optimization - Optimized! 37 select * from employees!! where emp_no = 20000;! COMMIT;

Benchmark - Connector Concurrency - SELECT 1 38 HikariCP- bench with JDBC Pool, 4 Threads, SELECT 1 (4,8,16,32 Pool Size) 70.000 52.500 QPS 35.000 17.500 MariaDB CONN/J

Benchmark - Connector Concurrency - TRX 39 HikariCP- bench with JDBC Pool, 4 Threads, TRX (4,8,16,32 Pool Size) 19.000 14.250 QPS 9.500 4.750 Conn/J MariaDB Conn/J Oprm MariaDB Oprm

40 MYSQL CONNECTORS CONNECTION POOLS

41 Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features MYSQL CONNECTORS CONNECTION POOLS

Java Connection Pools 42 The Usual: C3P0 Commons- DBCP (v1&v2) JDBC Pool (fork commons- DBCP) Out In The Wild: Vibur- DBCP HikariCP BoneCP

Java Connection Pools 43 The Usual: C3P0 Commons- DBCP (v1&v2) JDBC Pool (fork commons- DBCP) Out In The Wild: Vibur- DBCP HikariCP BoneCP

Connection Pool Key Points 44 Connection Management Pool Sizing Connection Testing Avoid Lingering Transactions

45 Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features MYSQL CONNECTORS CONNECTION POOLS

Connection Pool Issues 46 Coming from DBA side, I do not like chattiness

Connection Pool Issues 47

Connection Pool Issues 48

Connection Pool Issues 49

50 Because of application bugs WHY DOES IT HAVE TO DO THAT?

Connection Pools - Why Chattiness Examples 51 *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 Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features MYSQL CONNECTORS CONNECTION POOLS

Connection Pool - Resetting Status 53 JDBC- Pool C3P0 DBCP2 HikariCP Rollback rollbackonreturn=false autocommitonclose=false rollbackonreturn =true Commit commitonreturn=false autocommitonclose=false n/a n/a always rollback() Avoid see above forceignoreunresolvedtrans actions=false see above Auto Commit Driver Driver enableautocommit OnReturn=true Driver

54 Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features MYSQL CONNECTORS CONNECTION POOLS

Connection Pool - Testing 55 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

Connection Pool - Testing 56 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();

Connection Pool - Testing 57 JDBC- Pool C3P0 DBCP2 HikariCP Test Before testonborrow=false testconnectiononcheckout =false Test After testonreturn=false testconnectiononcheckin =false Test While Idle testwhileidle=false idleconnectiontestperiod =0 testonborrow=false testonreturn=false testwhileidle=false n/a n/a 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 58 JDBC: validationinterval=30s WHY? It defeats the whole purpose!

59 Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features MYSQL CONNECTORS CONNECTION POOLS

Connection Pool - Pool Sizing 60 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

Connection Pool - Pool Sizing 61 Amount of Connections Maximum Idle Connections Minimum Idle Connections JDBC- Pool C3P0 DBCP2 HikariCP maxactive=100 maxpoolsize=15 maxtotal=8 maximumpoolsize=10 maxidle=100 maxidletime=0** maxidle=8 n/a minidle=10 minpoolsize=3 minidle=0 minimumidle=max Startup Size initialsize=10 initialpoolsize=3 initialsize=0 minimumidle

62 Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features MYSQL CONNECTORS CONNECTION POOLS

Connection Pool - Avoid Lingering Transactions 63 Application forgets to return the connection Statements that take longer than! Avoid this! Fix Application

Connection Pool - Avoid Lingering Transactions 64 JDBC- Pool KILL removeabandoned=false removeabandonedtimeout=60 abandonwhenpercentagefull=0 Warning suspecttimeout=0 C3P0 unreturnedconnectiontimeout=0 n/a DBCP removeabandoned=false removeabandonedtimeout=300 Only When: getnumidle() < 2 and getnumactive() > getmaxtotal() - 3) HikariCP n/a leakdetectionthreshold=0 n/a

65 Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features MYSQL CONNECTORS CONNECTION POOLS

Connection Pools - How To Look At Workload? 66 Slow Query Log tcpdump! pt-query-digest! Percona Cloud Tools

67 Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features MYSQL CONNECTORS CONNECTION POOLS

Connection Pool - Example Transaction 68 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();

For Connectors - RECAP 69 MySQL Connector/J useconfigs=maxperformance! useserverprepstmts=true! MariaDB Java Client JDBC- Pool: jdbcinterceptors="connectionstate" Other Pools: UNKNOWN

Connection Pool - TRX JDBC 70 select * from employees!!!! where emp_no = 20000;!! commit; 200

Connection Pool - TRX C3P0 71 SET SESSION TRANSACTION!! ISOLATION LEVEL READ COMMITTED;! SET autocommit=0;! select * from employees!!!! where emp_no = 20000;! commit;! SET autocommit=1;! SET SESSION TRANSACTION! 600!! ISOLATION LEVEL REPEATABLE READ;

Connection Pool - TRX C3P0 72 mysql> set global!! tx_isolation= READ-COMMITTED ;!! forceignoreunresolvedtransactions=true! 200

Connection Pool - TRX DBCP 73 SET autocommit=1;! # administrator command: Ping;! SET autocommit=0;! select * from employees!!!! where emp_no = 20000;! commit;! rollback;! SET autocommit=1;! 700

Connection Pool - TRX DBCP 74 testonborrow=false! rollbackonreturn=false! enableautocommitonreturn=false!! jdbcurl: uselocaltransactionstate=true! 200

Connection Pool - TRX HikariCP 75 SET SESSION TRANSACTION!! ISOLATION LEVEL READ COMMITTED;! SET autocommit=0;! select * from employees!!!! where emp_no = 20000;! commit;! rollback;! SET autocommit=1;! 700 SET SESSION TRANSACTION!!! ISOLATION LEVEL REPEATABLE READ;!

Connection Pool - TRX HikariCP 76 mysql> set global!! tx_isolation= READ-COMMITTED ;!! autocommit=false!!! --> will always do rollback!!!!! 300

Connection Pools 77

MariaDB vs. Connector/J 78

79 Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features MYSQL CONNECTORS CONNECTION POOLS

Connection Pools - Graceful Failover 80

Connection Pools - Graceful Failover 81 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

Connection Pools - Graceful Failover 82

Connection Pools - Graceful Failover 83 During maintenance, what do we do? KILL old connections? Wait until connections are closed? (Define lifetimes?) Ignore it?

Connection Pools - Graceful Failover 84 Some connection pools can close connections gracefully, when idle. For synchronous replication systems using JMX No Application Errors! Method JDBC- Pool purgeonreturn() C3P0 softresetallusers() DBCP n/a HikariCP closeidleconnections() Only closes current idle connections

Connection Pools - Graceful Failover 85

Connection Pools - Graceful Failover 86

Connection Pools - Graceful Failover 87

Connection Pools - Graceful Failover 88 0 Application Errors Completely seamless

89 Connection Pools Issues Resetting Environment Testing Connectivity Pool Sizing Lingering Transactions Analysis Examples Graceful Failover Conn/J Extra Features MYSQL CONNECTORS CONNECTION POOLS

Connector/J - Extra Features 90 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

Victory! 91

Java MySQL Connector & Connection Pool Optimization 92 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 MYSQL CONNECTORS CONNECTION POOLS Kenny Gryp <kenny.gryp@percona.com> November 4, 2014 @gryp