Академический Документы
Профессиональный Документы
Культура Документы
MySQL Connector/Java: Best Practices & Tips for Java-based Product Developers
Todd Farmer <todd.farmer@oracle.com> Director, MySQL Engineering
The following/preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracles products remains at the sole discretion of Oracle.
MySQL Enterprise Backup 3.7 Oracle VM Template for MySQL Enterprise Edition MySQL Enterprise Oracle Certifications MySQL Windows Installer MySQL Enterprise Security MySQL Enterprise Scalability
MySQL Cluster 7.2 MySQL Cluster Manager 1.4 MySQL Utilities 1.0.6 MySQL Migration Wizard MySQL Enterprise Backup 3.8 MySQL Enterprise Audit MySQL Database 5.6
All GA!
MySQL Database 5.6 DMR* MySQL Cluster 7.2 DMR MySQL Labs! (early and often)
A BETTER MySQL
2012-13
2010
2011
*Development Milestone Release
IMPROVED INNODB
Better transactional throughput and availability
IMPROVED OPTIMIZER
Better query exec times and diagnostics for query tuning and debugging
IMPROVED REPLICATION
Higher performance, availability and data integrity
Hardware/Appliances/Devices
SaaS
Telecommunications
Software
Rely on MySQL
Copyright 2012, Oracle and/or its affiliates. All rights reserved. 6
Agenda
Fault Tolerant Deployments & Applications Extending Connector/Java Optimizing Performance Monitoring and Management Strategies Q&A
<Insert Picture Here>
Integrate Logging
Use a logging framework Integrate Connector/J into it (category MySQL):
logger=Jdk14Logger logger=Slf4JLogger
10
Avoiding CommunicationExceptions
Configure wait_timeout server variable appropriately for maximum connection idle period
Use interactive_timeout if using interactiveClient C/J option)
Make sure socketTimeout is not set (or set sensibly high) Minimize duration connections left idle
Check connection pool connection testing policies Use lightweight validation query starting with /* ping /* Choose validation strategy wisely
11
12
13
High Availability
HA does not mean never any Exceptions! HA means quick to recover and resume processing Application needs to be prepared to handle failover situations
Implement retry logic, if appropriate See earlier discussion of CommunicationExceptions for details
14
15
Semi-Automated Failover
Only route to secondary server(s) if primary is offline
Use jdbc:mysql://master,secondary,[]/ URL format
Set failOverReadOnly=false if write operations are OK Will failover entail creating new Connection objects?
If so, leaving autoReconnect=false is fine
Failing back
secondsBeforeRetryMaster and queriesBeforeRetryMaster throttle how often Driver will try master again
16
Host management:
loadBalanceBlacklistTimeout keeps hosts from being retried loadBalanceValidateConnectionOnSwapServer checks connections before choosing loadBalanceEnableJMX and loadBalanceConnectionGroup allow live manipulation of hosts
17
Agenda
Fault Tolerant Deployments & Applications Extending Connector/Java Optimizing Performance Monitoring and Management Strategies Q&A
<Insert Picture Here>
18
Extension Points
Lifecycle Interceptors Statement Interceptors Exception Interceptors Loadbalancing Strategies
19
Lifecycle Interceptors
com.mysql.jdbc.ConnectionLifecycleInterceptor Intercept JDBC method calls, return false to suppress driver standard behavior
commit() rollback() setAutoCommit() close() setCatalog()
20
Statement Interceptors
com.mysql.jdbc.StatementInterceptorV2 Override pre or post-execution method hooks
Return null if no changes Return implementation of ResultSetInternalMethods to override actual response from server.
Useful to change behavior of without application-level changes Can be chained for different use cases
21
Exception Interceptors
mysql.jdbc.ExceptionInterceptor Can change/observe exceptions In interceptException(SQLException, Connection)
Return a different exception Add information to exception, such as output from SHOW WARNINGS Do something related to given Exception, then return same exception E.G. SQLState 08007 Transaction resolution unknown, panic() or at least invalidate cache state
22
23
Agenda
Fault Tolerant Deployments & Applications Extending Connector/Java Optimizing Performance Monitoring and Management Strategies Q&A
<Insert Picture Here>
24
26
Timeseries data (graphs), ETL, bulk unload are possible use cases Caveat: Only one active query at a time perconnection! Server-side locks for the duration of the statement (or transaction)
27
28
29
31
32
33
34
Caching ResultSetMetaData
Use cacheResultSetMetaData=true Caches ResultSet metadata per SQL statement, per connection Default cache size is 50, uses LRU ResultSets must be static
no dynamic SQL in stored procedures
35
Uses RAM, even when timeout threshold is not reached Disable if not using Statement.setQueryTimeout() in high-load environment
36
37
Uses RAM, even when timeout threshold is not reached Disable if not using Statement.setQueryTimeout() in high-load environment
38
Agenda
Fault Tolerant Deployments & Applications Extending Connector/Java Optimizing Performance Monitoring and Management Strategies Q&A
<Insert Picture Here>
39
40
41
Monitoring Performance
42
What to Collect
CPU (User, Sys, Idle) Memory (real, virtual, swap, per-important-process?) Throughput (http, sql) Service times (95 %-ile if possible) Slow SQL, app units of work (requests) I/O (disk, network) JVM (GC) MySQL (Connections, SQL, Sorts, Temp Tables, Buffer Pool)
43
How to Collect It
MySQL Enterprise Monitor(MEM) Already in-place monitoring tools iostat, sar, recorded to database tables Servlet filters (response times, counts)
44
Use Connector/Java plugin to get application stack trace for problematic/expensive/frequent statements Get EXPLAIN output for sample queries
45
46
47
Agenda
Fault Tolerant Deployments & Applications Extending Connector/Java Optimizing Performance Monitoring and Management Strategies Q&A
<Insert Picture Here>
48