SlideShare a Scribd company logo
1 of 58
Download to read offline
High-Performance JDBC
VLAD MIHALCEA
About me
โ€ข @Hibernate Developer
โ€ข vladmihalcea.com
โ€ข @vlad_mihalcea
โ€ข vladmihalcea
Performance Facts
โ€œMore than half of application performance
bottlenecks originate in the databaseโ€
AppDynamics - http://www.appdynamics.com/database/
Data access layers
Poor manโ€™s JDBC
โ€ข High response time
โ€ข Low throughput
Photo by Amit Patel CC BY 2.0 https://www.flickr.com/photos/amitp/6069412747/
State of the art JDBC
โ€ข Low response time
โ€ข High throughput
Photo by zoetnet CC BY 2.0 https://www.flickr.com/photos/zoetnet/14288129197/
Response time
โ€ข connection acquisition time
โ€ข statements submission time
โ€ข statements execution time
โ€ข result set fetching time
โ€ข idle time prior to releasing the database connection
๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
Connection management
๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
Connection acquisition overhead
Metric DB_A (ms) DB_B (ms) DB_C (ms) DB_D (ms) HikariCP (ms)
min 11.174 5.441 24.468 0.860 0.001230
max 129.400 26.110 74.634 74.313 1.014051
mean 13.829 6.477 28.910 1.590 0.003458
p99 20.432 9.944 54.952 3.022 0.010263
Connection pooling
โ€ข Logical vs physical connections
โ€ข Lease vs create
โ€ข Release vs close
Connection pool sizing
FlexyPool
โ€ข Java EE
โ€ข Bitronix / Atomikos
โ€ข Apache DBCP / DBCP2
โ€ข C3P0
โ€ข HikariCP
โ€ข Tomcat CP
โ€ข Vibur DBCP
https://github.com/vladmihalcea/flexy-pool
FlexyPool
โ€ข concurrent connections histogram
โ€ข concurrent connection requests histogram
โ€ข connection acquisition time histogram
โ€ข connection lease time histogram
โ€ข maximum pool size histogram
โ€ข retry attempts histogram
https://github.com/vladmihalcea/flexy-pool
FlexyPool โ€“ Concurrent connection requests
1
28
55
82
109
136
163
190
217
244
271
298
325
352
379
406
433
460
487
514
541
568
595
622
649
676
703
730
757
784
811
838
865
892
919
946
973
1000
1027
0
2
4
6
8
10
12
Sample time (Index ร— 15s)
Connectionrequests
max mean p50 p95 p99
FlexyPool โ€“ Pool size growth
1
28
55
82
109
136
163
190
217
244
271
298
325
352
379
406
433
460
487
514
541
568
595
622
649
676
703
730
757
784
811
838
865
892
919
946
973
1000
1027
0
1
2
3
4
5
6
Sample time (Index ร— 15s)
Maxpoolsize
max mean p50 p95 p99
FlexyPool โ€“ Connection acquisition time
1
28
55
82
109
136
163
190
217
244
271
298
325
352
379
406
433
460
487
514
541
568
595
622
649
676
703
730
757
784
811
838
865
892
919
946
973
1000
1027
0
500
1000
1500
2000
2500
3000
3500
Sample time (Index ร— 15s)
Connectionacquisitiontime(ms)
max mean p50 p95 p99
FlexyPool โ€“ Connection lease time
1
29
57
85
113
141
169
197
225
253
281
309
337
365
393
421
449
477
505
533
561
589
617
645
673
701
729
757
785
813
841
869
897
925
953
981
1009
1037
0
5000
10000
15000
20000
25000
30000
35000
40000
Sample time (Index ร— 15s)
Connectionleasetime(ms)
max mean p50 p95 p99
Statement Batching
statement.addBatch(
"INSERT INTO post "(title, version, id) " +
"VALUES ('Post no. 1', 0, 1)");
statement.addBatch(
"INSERT INTO post_comment (post_id, review, version, id) " +
"VALUES (1, 'Post comment 1.1', 0, 1)");
int[] updateCounts = statement.executeBatch();
๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
Statement Batching (5k rows)
1 10 20 30 40 50 60 70 80 90 100 1000
0
500
1000
1500
2000
2500
Batch size
Time(ms)
DB_A DB_B DB_C DB_D
Oracle Statement batching
โ€ข For Statement and CallableStatement,
the Oracle JDBC Driver doesnโ€™t actually support batching,
each statement being executed separately.
MySQL Statement batching
โ€ข By default, the MySQL JDBC driver doesnโ€™t send the batched
statements in a single request.
โ€ข The rewriteBatchedStatements connection property
adds all batched statements to a String buffer.
Batch PreparedStatements
PreparedStatement postStatement = connection.prepareStatement(
"INSERT INTO Post (title, version, id) VALUES (?, ?, ?)");
postStatement.setString(1, String.format("Post no. %1$d", 1));
postStatement.setInt(2, 0);
postStatement.setLong(3, 1);
postStatement.addBatch();
postStatement.setString(1, String.format("Post no. %1$d", 2));
postStatement.setInt(2, 0);
postStatement.setLong(3, 2);
postStatement.addBatch();
int[] updateCounts = postStatement.executeBatch();
Batch PreparedStatements
โ€ข SQL Injection Prevention
โ€ข Better performance
โ€ข Hibernate can batch statements automatically
Insert PreparedStatement batching (5k rows)
1 10 20 30 40 50 60 70 80 90 100 1000
0
200
400
600
800
1000
1200
1400
1600
Batch size
Time(ms)
DB_A DB_B DB_C DB_D
Update PreparedStatement batching (5k rows)
1 10 20 30 40 50 60 70 80 90 100 1000
0
100
200
300
400
500
600
700
Batch size
Time(ms)
DB_A DB_B DB_C DB_D
Delete PreparedStatement batching (5k rows)
1 10 20 30 40 50 60 70 80 90 100 1000
0
200
400
600
800
1000
1200
Batch size
Time(ms)
DB_A DB_B DB_C DB_D
Statement caching
๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
Statement caching gain (one minute interval)
Database System No Caching
Throughput (SPM)
Caching Throughput
(SPM)
Percentage Gain
DB_A 419 833 507 286 20.83%
DB_B 194 837 303 100 55.56%
DB_C 116 708 166 443 42.61%
DB_D 15 522 15 550 0.18%
Oracle server-side statement caching
โ€ข Hard parse
โ€ข Soft parse
โ€ข Bind peeking
โ€ข Adaptive cursor sharing (since 11g)
SQL Server server-side statement caching
โ€ข Execution plan cache
โ€ข Parameter sniffing
โ€ข Force recompile
SELECT *
FROM task
WHERE status = ?
OPTION(RECOMPILE);
PostgreSQL server-side statement caching
โ€ข Prior to 9.2 โ€“ execution plan caching
โ€ข 9.2 โ€“ optimization and planning are deferred
โ€ข The prepareThreshold connection property
MySQL server-side statement caching
โ€ข No execution plan cache
โ€ข Since Connector/J 5.0.5 PreparedStatements are only
emulated
โ€ข To activate server-side prepared statements:
โ€ข useServerPrepStmts
โ€ข cachePrepStmts
Client-side statement caching
โ€ข Recycling Statement, PreparedStatement or
CallableStatement objects
โ€ข Reusing database cursors
Oracle implicit client-side statement caching
โ€ข Connection-level cache
โ€ข PreparedStatement and CallabledStatement only
connectionProperties.put(
"oracle.jdbc.implicitStatementCacheSize",
Integer.toString(cacheSize)
);
dataSource.setConnectionProperties(
connectionProperties
);
Oracle implicit client-side statement caching
โ€ข Can be disabled on a per statement basis
if (statement.isPoolable()) {
statement.setPoolable(false);
}
Oracle explicit client-side statement caching
โ€ข Caches both metadata and execution state with data
OracleConnection oracleConnection =
(OracleConnection) connection;
oracleConnection.setExplicitCachingEnabled(true);
oracleConnection.setStatementCacheSize(cacheSize);
Oracle explicit client-side statement caching
โ€ข Vendor-specific API
PreparedStatement statement = oracleConnection.
getStatementWithKey(SELECT_POST_KEY);
if (statement == null)
statement = connection.prepareStatement(SELECT_POST);
try {
statement.setInt(1, 10);
statement.execute();
} finally {
((OraclePreparedStatement) statement).
closeWithKey(SELECT_POST_KEY);
}
SQL Server client-side statement caching
โ€ข Microsoft JDBC Driver 4.2 disableStatementPooling
โ€ข jTDS 1.3.1 โ€“ JDBC 3.0
JtdsDataSource jdtsDataSource =
(JtdsDataSource) dataSource;
jdtsDataSource.setMaxStatements(cacheSize);
PostgreSQL Server client-side statement caching
โ€ข PostgreSQL JDBC Driver 9.4-1202 makes client-side
statement connection-bound instead of statement-bound
โ€ข Configurable:
โ€ข preparedStatementCacheQueries (default is 256)
โ€ข preparedStatementCacheSizeMiB (default is 5MB)
โ€ข Statement.setPoolable(false) is not supported
MySQL Server client-side statement caching
โ€ข Configurable:
โ€ข cachePrepStmts (default is false)
Required for server-side statement caching as well
โ€ข prepStmtCacheSize (default is 25)
โ€ข prepStmtCacheSqlLimit (default is 256)
โ€ข Statement.setPoolable(false) works for client-side
statements only
ResultSet fetch size
โ€ข ResultSet - application-level cursor
๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
statement.setFetchSize(fetchSize);
Oracle ResultSet fetch size
โ€ข Default fetch size is 10
โ€ข Oracle 10i and 11g JDBC Driver maximum ResultSet size
memory preallocation
โ€ข VARCHAR2(4000) โ€“ allocates 8000 bytes (even for 1 character)
โ€ข Memory buffers are recycled only when using Statement caching
โ€ข Oracle 12c allocates memory on demand
โ€ข VARCHAR2(4000) โ€“ 15 bytes + the actual row column size
SQL Server ResultSet fetch size
โ€ข Adaptive buffering
โ€ข Only for the default read-only and forward-only ResultSet
โ€ข Updatable cursors use fixed data blocks
PostgreSQL ResultSet fetch size
โ€ข Fetch all โ€“ one database roundtrip
โ€ข Custom fetch size โ€“ database cursor
MySQL ResultSet fetch size
โ€ข Fetch all โ€“ one database roundtrip
โ€ข Streaming โ€“ only one record at a time
ResultSet fetch size (10k rows)
1 10 100 1000 10000
0
100
200
300
400
500
600
Fetch size
Time(ms)
DB_A DB_B DB_C DB_D
ResultSet size
โ€ข Avoid fetching data that is not required
โ€ข Hibernate addresses the max-size vendor-specific SQL
statement syntax
SQL:2008 ResultSet size limit
โ€ข Oracle 12c, SQL Server 2012 and PostgreSQL 8.4
SELECT
pc.id AS pc_id, p.title AS p_title
FROM post_comment pc
INNER JOIN post p ON p.id = pc.post_id
ORDER BY pc_id
OFFSET ? ROWS
FETCH FIRST (?) ROWS ONLY;
Oracle ResultSet size limit
SELECT *
FROM (
SELECT
pc.id AS pc_id, p.title AS p_title
FROM post_comment pc
INNER JOIN post p ON p.id = pc.post_id
ORDER BY pc_id
)
WHERE ROWNUM <= ?
SQL Server ResultSet size limit
SELECT
TOP (?) pc.id AS pc_id, p.title AS p_title
FROM post_comment pc
INNER JOIN post p ON p.id = pc.post_id
ORDER BY pc_id
PostgreSQL and MySQL ResultSet size limit
SELECT
pc.id AS pc_id, p.title AS p_title
FROM post_comment pc
INNER JOIN post p ON p.id = pc.post_id
ORDER BY pc_id
LIMIT ?
Statement max rows
โ€ข Vendor-independent syntax
โ€ข Might not influence the execution plan
โ€ข According to the documentation:
โ€œIf the limit is exceeded, the excess rows are silently dropped.โ€
statement.setMaxRows(maxRows);
Max size: 1 million vs 100 rows
Fetch all Fetch max rows Fetch limit
0
500
1000
1500
2000
2500
3000
3500
4000
4500
5000
Time(ms)
DB_A DB_B DB_C DB_D
Fetching too many columns
โ€ข Fetching all column (ORM tools)
SELECT *
FROM post_comment pc
INNER JOIN post p ON p.id = pc.post_id
INNER JOIN post_details pd ON p.id = pd.id
Fetching too many columns
โ€ข Fetching a custom SQL projection
SELECT pc.version
FROM post_comment pc
INNER JOIN post p ON p.id = pc.post_id
INNER JOIN post_details pd ON p.id = pd.id
Fetching too many columns performance impact
All columns Custom projection
0
5
10
15
20
25
30
Time(ms)
DB_A DB_B DB_C DB_D
Processing Logic
โ€ข Hibernate defers connection acquisition
โ€ข Release connection as soon as possible
๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
Questions and Answers
๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
โ€ข Response time
โ€ข Connection management
โ€ข Batch updates
โ€ข Statement caching
โ€ข ResultSet fetching
โ€ข https://leanpub.com/high-performance-java-persistence

More Related Content

What's hot

Peer DIDs: a secure and scalable method for DIDs thatโ€™s entirely off-ledger โ€“...
Peer DIDs: a secure and scalable method for DIDs thatโ€™s entirely off-ledger โ€“...Peer DIDs: a secure and scalable method for DIDs thatโ€™s entirely off-ledger โ€“...
Peer DIDs: a secure and scalable method for DIDs thatโ€™s entirely off-ledger โ€“...
SSIMeetup
ย 

What's hot (20)

What are Decentralized Identifiers (DIDs)?
What are Decentralized Identifiers (DIDs)?What are Decentralized Identifiers (DIDs)?
What are Decentralized Identifiers (DIDs)?
ย 
Verifiable Credentials 101 for SSI and Decentralized Digital Identity - Tyler...
Verifiable Credentials 101 for SSI and Decentralized Digital Identity - Tyler...Verifiable Credentials 101 for SSI and Decentralized Digital Identity - Tyler...
Verifiable Credentials 101 for SSI and Decentralized Digital Identity - Tyler...
ย 
Decentralized Key Management (DKMS): An Essential Missing Piece of the SSI Pu...
Decentralized Key Management (DKMS): An Essential Missing Piece of the SSI Pu...Decentralized Key Management (DKMS): An Essential Missing Piece of the SSI Pu...
Decentralized Key Management (DKMS): An Essential Missing Piece of the SSI Pu...
ย 
ํ•˜์ดํผ๋ ˆ์ € ํŒจ๋ธŒ๋ฆญ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
ํ•˜์ดํผ๋ ˆ์ € ํŒจ๋ธŒ๋ฆญ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐํ•˜์ดํผ๋ ˆ์ € ํŒจ๋ธŒ๋ฆญ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
ํ•˜์ดํผ๋ ˆ์ € ํŒจ๋ธŒ๋ฆญ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
ย 
Peer DIDs: a secure and scalable method for DIDs thatโ€™s entirely off-ledger โ€“...
Peer DIDs: a secure and scalable method for DIDs thatโ€™s entirely off-ledger โ€“...Peer DIDs: a secure and scalable method for DIDs thatโ€™s entirely off-ledger โ€“...
Peer DIDs: a secure and scalable method for DIDs thatโ€™s entirely off-ledger โ€“...
ย 
WebAuthn and Security Keys
WebAuthn and Security KeysWebAuthn and Security Keys
WebAuthn and Security Keys
ย 
Hyperledger Indy Platform - Privacy, Security and Power for Digital Identity ...
Hyperledger Indy Platform - Privacy, Security and Power for Digital Identity ...Hyperledger Indy Platform - Privacy, Security and Power for Digital Identity ...
Hyperledger Indy Platform - Privacy, Security and Power for Digital Identity ...
ย 
CNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbgCNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbg
ย 
Digital Identity Wallets: What They Mean For Banks
Digital Identity Wallets: What They Mean For BanksDigital Identity Wallets: What They Mean For Banks
Digital Identity Wallets: What They Mean For Banks
ย 
Reverse proxies & Inconsistency
Reverse proxies & InconsistencyReverse proxies & Inconsistency
Reverse proxies & Inconsistency
ย 
Zero-Knowledge Proofs: Privacy-Preserving Digital Identity with Clare Nelson
Zero-Knowledge Proofs: Privacy-Preserving Digital Identity with Clare NelsonZero-Knowledge Proofs: Privacy-Preserving Digital Identity with Clare Nelson
Zero-Knowledge Proofs: Privacy-Preserving Digital Identity with Clare Nelson
ย 
CNIT 152 12 Investigating Windows Systems (Part 1 of 3)
CNIT 152 12 Investigating Windows Systems (Part 1 of 3)CNIT 152 12 Investigating Windows Systems (Part 1 of 3)
CNIT 152 12 Investigating Windows Systems (Part 1 of 3)
ย 
Getting Started With WebAuthn
Getting Started With WebAuthnGetting Started With WebAuthn
Getting Started With WebAuthn
ย 
The Hyperledger Indy Public Blockchain Node
The Hyperledger Indy Public Blockchain NodeThe Hyperledger Indy Public Blockchain Node
The Hyperledger Indy Public Blockchain Node
ย 
Verifiable Credentials & Legal Entity Identifiers (LEIs) | Evernym & GLEIF
Verifiable Credentials & Legal Entity Identifiers (LEIs) | Evernym & GLEIFVerifiable Credentials & Legal Entity Identifiers (LEIs) | Evernym & GLEIF
Verifiable Credentials & Legal Entity Identifiers (LEIs) | Evernym & GLEIF
ย 
(Ab)Using GPOs for Active Directory Pwnage
(Ab)Using GPOs for Active Directory Pwnage(Ab)Using GPOs for Active Directory Pwnage
(Ab)Using GPOs for Active Directory Pwnage
ย 
Ready player 2 Multiplayer Red Teaming Against macOS
Ready player 2  Multiplayer Red Teaming Against macOSReady player 2  Multiplayer Red Teaming Against macOS
Ready player 2 Multiplayer Red Teaming Against macOS
ย 
Time based CAPTCHA protected SQL injection through SOAP-webservice
Time based CAPTCHA protected SQL injection through SOAP-webserviceTime based CAPTCHA protected SQL injection through SOAP-webservice
Time based CAPTCHA protected SQL injection through SOAP-webservice
ย 
OSCON 2018 Getting Started with Hyperledger Indy
OSCON 2018 Getting Started with Hyperledger IndyOSCON 2018 Getting Started with Hyperledger Indy
OSCON 2018 Getting Started with Hyperledger Indy
ย 
OpenID Connect Explained
OpenID Connect ExplainedOpenID Connect Explained
OpenID Connect Explained
ย 

Viewers also liked

ORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate Overview
Brett Meyer
ย 
Navigating the Incubator at the Apache Software Foundation
Navigating the Incubator at the Apache Software FoundationNavigating the Incubator at the Apache Software Foundation
Navigating the Incubator at the Apache Software Foundation
Brett Porter
ย 
Java libraries you can't afford to miss
Java libraries you can't afford to missJava libraries you can't afford to miss
Java libraries you can't afford to miss
Andres Almiray
ย 
CodeFest 2013. ะ—ะธะฝะพะฒัŒะตะฒ ะ. โ€” MyBatis & Hibernate, ะดะฐะฒะฐะนั‚ะต ะถะธั‚ัŒ ะดั€ัƒะถะฝะพ!
CodeFest 2013. ะ—ะธะฝะพะฒัŒะตะฒ ะ. โ€” MyBatis & Hibernate, ะดะฐะฒะฐะนั‚ะต ะถะธั‚ัŒ ะดั€ัƒะถะฝะพ!CodeFest 2013. ะ—ะธะฝะพะฒัŒะตะฒ ะ. โ€” MyBatis & Hibernate, ะดะฐะฒะฐะนั‚ะต ะถะธั‚ัŒ ะดั€ัƒะถะฝะพ!
CodeFest 2013. ะ—ะธะฝะพะฒัŒะตะฒ ะ. โ€” MyBatis & Hibernate, ะดะฐะฒะฐะนั‚ะต ะถะธั‚ัŒ ะดั€ัƒะถะฝะพ!
CodeFest
ย 
ะžัะพะทะฝะฐะฝะฝะพัั‚ัŒ ั€ะตั„ะฐะบั‚ะพั€ะธะฝะณะฐ: ะœะพะดะตะปัŒ ะฟั€ะธะฝัั‚ะธั ะธะฝะถะตะฝะตั€ะฝั‹ั… ั€ะตัˆะตะฝะธะน
ะžัะพะทะฝะฐะฝะฝะพัั‚ัŒ ั€ะตั„ะฐะบั‚ะพั€ะธะฝะณะฐ: ะœะพะดะตะปัŒ ะฟั€ะธะฝัั‚ะธั ะธะฝะถะตะฝะตั€ะฝั‹ั… ั€ะตัˆะตะฝะธะนะžัะพะทะฝะฐะฝะฝะพัั‚ัŒ ั€ะตั„ะฐะบั‚ะพั€ะธะฝะณะฐ: ะœะพะดะตะปัŒ ะฟั€ะธะฝัั‚ะธั ะธะฝะถะตะฝะตั€ะฝั‹ั… ั€ะตัˆะตะฝะธะน
ะžัะพะทะฝะฐะฝะฝะพัั‚ัŒ ั€ะตั„ะฐะบั‚ะพั€ะธะฝะณะฐ: ะœะพะดะตะปัŒ ะฟั€ะธะฝัั‚ะธั ะธะฝะถะตะฝะตั€ะฝั‹ั… ั€ะตัˆะตะฝะธะน
Evgeniy Krivosheev
ย 
Developing With JAAS
Developing With JAASDeveloping With JAAS
Developing With JAAS
rahmed_sct
ย 

Viewers also liked (20)

High-Performance Hibernate Devoxx France 2016
High-Performance Hibernate Devoxx France 2016High-Performance Hibernate Devoxx France 2016
High-Performance Hibernate Devoxx France 2016
ย 
Transactions and Concurrency Control Patterns
Transactions and Concurrency Control PatternsTransactions and Concurrency Control Patterns
Transactions and Concurrency Control Patterns
ย 
High Performance Hibernate JavaZone 2016
High Performance Hibernate JavaZone 2016High Performance Hibernate JavaZone 2016
High Performance Hibernate JavaZone 2016
ย 
High Performance Databases
High Performance DatabasesHigh Performance Databases
High Performance Databases
ย 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance Techniques
ย 
ORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate Overview
ย 
Not Just ORM: Powerful Hibernate ORM Features and Capabilities
Not Just ORM: Powerful Hibernate ORM Features and CapabilitiesNot Just ORM: Powerful Hibernate ORM Features and Capabilities
Not Just ORM: Powerful Hibernate ORM Features and Capabilities
ย 
Navigating the Incubator at the Apache Software Foundation
Navigating the Incubator at the Apache Software FoundationNavigating the Incubator at the Apache Software Foundation
Navigating the Incubator at the Apache Software Foundation
ย 
Reflection Madness - Dr. Heinz Kabutz
Reflection Madness - Dr. Heinz KabutzReflection Madness - Dr. Heinz Kabutz
Reflection Madness - Dr. Heinz Kabutz
ย 
Java libraries you can't afford to miss
Java libraries you can't afford to missJava libraries you can't afford to miss
Java libraries you can't afford to miss
ย 
Tdd Workshop Disscussions
Tdd Workshop DisscussionsTdd Workshop Disscussions
Tdd Workshop Disscussions
ย 
CodeFest 2013. ะ—ะธะฝะพะฒัŒะตะฒ ะ. โ€” MyBatis & Hibernate, ะดะฐะฒะฐะนั‚ะต ะถะธั‚ัŒ ะดั€ัƒะถะฝะพ!
CodeFest 2013. ะ—ะธะฝะพะฒัŒะตะฒ ะ. โ€” MyBatis & Hibernate, ะดะฐะฒะฐะนั‚ะต ะถะธั‚ัŒ ะดั€ัƒะถะฝะพ!CodeFest 2013. ะ—ะธะฝะพะฒัŒะตะฒ ะ. โ€” MyBatis & Hibernate, ะดะฐะฒะฐะนั‚ะต ะถะธั‚ัŒ ะดั€ัƒะถะฝะพ!
CodeFest 2013. ะ—ะธะฝะพะฒัŒะตะฒ ะ. โ€” MyBatis & Hibernate, ะดะฐะฒะฐะนั‚ะต ะถะธั‚ัŒ ะดั€ัƒะถะฝะพ!
ย 
ะžัะพะทะฝะฐะฝะฝะพัั‚ัŒ ั€ะตั„ะฐะบั‚ะพั€ะธะฝะณะฐ: ะœะพะดะตะปัŒ ะฟั€ะธะฝัั‚ะธั ะธะฝะถะตะฝะตั€ะฝั‹ั… ั€ะตัˆะตะฝะธะน
ะžัะพะทะฝะฐะฝะฝะพัั‚ัŒ ั€ะตั„ะฐะบั‚ะพั€ะธะฝะณะฐ: ะœะพะดะตะปัŒ ะฟั€ะธะฝัั‚ะธั ะธะฝะถะตะฝะตั€ะฝั‹ั… ั€ะตัˆะตะฝะธะนะžัะพะทะฝะฐะฝะฝะพัั‚ัŒ ั€ะตั„ะฐะบั‚ะพั€ะธะฝะณะฐ: ะœะพะดะตะปัŒ ะฟั€ะธะฝัั‚ะธั ะธะฝะถะตะฝะตั€ะฝั‹ั… ั€ะตัˆะตะฝะธะน
ะžัะพะทะฝะฐะฝะฝะพัั‚ัŒ ั€ะตั„ะฐะบั‚ะพั€ะธะฝะณะฐ: ะœะพะดะตะปัŒ ะฟั€ะธะฝัั‚ะธั ะธะฝะถะตะฝะตั€ะฝั‹ั… ั€ะตัˆะตะฝะธะน
ย 
ะ’ะตะฑะธะฝะฐั€ ะฝะฐั‡ะฐะปะพ
ะ’ะตะฑะธะฝะฐั€ ะฝะฐั‡ะฐะปะพะ’ะตะฑะธะฝะฐั€ ะฝะฐั‡ะฐะปะพ
ะ’ะตะฑะธะฝะฐั€ ะฝะฐั‡ะฐะปะพ
ย 
Voxxed Days Thesaloniki 2016 - 5 must have patterns for your web-scale micros...
Voxxed Days Thesaloniki 2016 - 5 must have patterns for your web-scale micros...Voxxed Days Thesaloniki 2016 - 5 must have patterns for your web-scale micros...
Voxxed Days Thesaloniki 2016 - 5 must have patterns for your web-scale micros...
ย 
Google GIN
Google GINGoogle GIN
Google GIN
ย 
[Altibase] 6 what is the mvcc
[Altibase] 6 what is the mvcc[Altibase] 6 what is the mvcc
[Altibase] 6 what is the mvcc
ย 
ะ’ะฒะตะดะตะฝะธะต ะฒ ะฒะตะฑ ะบะฐั€ะบะฐั Struts2
ะ’ะฒะตะดะตะฝะธะต ะฒ ะฒะตะฑ ะบะฐั€ะบะฐั Struts2ะ’ะฒะตะดะตะฝะธะต ะฒ ะฒะตะฑ ะบะฐั€ะบะฐั Struts2
ะ’ะฒะตะดะตะฝะธะต ะฒ ะฒะตะฑ ะบะฐั€ะบะฐั Struts2
ย 
Android Architecture
Android ArchitectureAndroid Architecture
Android Architecture
ย 
Developing With JAAS
Developing With JAASDeveloping With JAAS
Developing With JAAS
ย 

Similar to High-Performance JDBC Voxxed Bucharest 2016

Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004
Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004
Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004
derek_clark_ashmore
ย 

Similar to High-Performance JDBC Voxxed Bucharest 2016 (20)

BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBCBI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
ย 
Advance java session 5
Advance java session 5Advance java session 5
Advance java session 5
ย 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
ย 
Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004
Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004
Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004
ย 
Copy Data Management for the DBA
Copy Data Management for the DBACopy Data Management for the DBA
Copy Data Management for the DBA
ย 
Jdbc presentation
Jdbc presentationJdbc presentation
Jdbc presentation
ย 
PROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part IPROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part I
ย 
Marmagna desai
Marmagna desaiMarmagna desai
Marmagna desai
ย 
Jdbc
JdbcJdbc
Jdbc
ย 
Jdbc Java Programming
Jdbc Java ProgrammingJdbc Java Programming
Jdbc Java Programming
ย 
Denver SQL Saturday The Next Frontier
Denver SQL Saturday The Next FrontierDenver SQL Saturday The Next Frontier
Denver SQL Saturday The Next Frontier
ย 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Model
ย 
Improve power bi performance
Improve power bi performanceImprove power bi performance
Improve power bi performance
ย 
SQL on Hadoop
SQL on HadoopSQL on Hadoop
SQL on Hadoop
ย 
20151010 my sq-landjavav2a
20151010 my sq-landjavav2a20151010 my sq-landjavav2a
20151010 my sq-landjavav2a
ย 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
ย 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
ย 
Running database infrastructure on containers
Running database infrastructure on containersRunning database infrastructure on containers
Running database infrastructure on containers
ย 
Jdbc
JdbcJdbc
Jdbc
ย 
Sqlite
SqliteSqlite
Sqlite
ย 

Recently uploaded

CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
anilsa9823
ย 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
ย 

Recently uploaded (20)

How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
ย 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
ย 
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
ย 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
ย 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
ย 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
ย 
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
ย 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
ย 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
ย 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
ย 
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
ย 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
ย 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
ย 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
ย 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
ย 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
ย 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
ย 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
ย 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
ย 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
ย 

High-Performance JDBC Voxxed Bucharest 2016

  • 2. About me โ€ข @Hibernate Developer โ€ข vladmihalcea.com โ€ข @vlad_mihalcea โ€ข vladmihalcea
  • 3. Performance Facts โ€œMore than half of application performance bottlenecks originate in the databaseโ€ AppDynamics - http://www.appdynamics.com/database/
  • 5. Poor manโ€™s JDBC โ€ข High response time โ€ข Low throughput Photo by Amit Patel CC BY 2.0 https://www.flickr.com/photos/amitp/6069412747/
  • 6. State of the art JDBC โ€ข Low response time โ€ข High throughput Photo by zoetnet CC BY 2.0 https://www.flickr.com/photos/zoetnet/14288129197/
  • 7. Response time โ€ข connection acquisition time โ€ข statements submission time โ€ข statements execution time โ€ข result set fetching time โ€ข idle time prior to releasing the database connection ๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
  • 8. Connection management ๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
  • 9. Connection acquisition overhead Metric DB_A (ms) DB_B (ms) DB_C (ms) DB_D (ms) HikariCP (ms) min 11.174 5.441 24.468 0.860 0.001230 max 129.400 26.110 74.634 74.313 1.014051 mean 13.829 6.477 28.910 1.590 0.003458 p99 20.432 9.944 54.952 3.022 0.010263
  • 10. Connection pooling โ€ข Logical vs physical connections โ€ข Lease vs create โ€ข Release vs close
  • 12. FlexyPool โ€ข Java EE โ€ข Bitronix / Atomikos โ€ข Apache DBCP / DBCP2 โ€ข C3P0 โ€ข HikariCP โ€ข Tomcat CP โ€ข Vibur DBCP https://github.com/vladmihalcea/flexy-pool
  • 13. FlexyPool โ€ข concurrent connections histogram โ€ข concurrent connection requests histogram โ€ข connection acquisition time histogram โ€ข connection lease time histogram โ€ข maximum pool size histogram โ€ข retry attempts histogram https://github.com/vladmihalcea/flexy-pool
  • 14. FlexyPool โ€“ Concurrent connection requests 1 28 55 82 109 136 163 190 217 244 271 298 325 352 379 406 433 460 487 514 541 568 595 622 649 676 703 730 757 784 811 838 865 892 919 946 973 1000 1027 0 2 4 6 8 10 12 Sample time (Index ร— 15s) Connectionrequests max mean p50 p95 p99
  • 15. FlexyPool โ€“ Pool size growth 1 28 55 82 109 136 163 190 217 244 271 298 325 352 379 406 433 460 487 514 541 568 595 622 649 676 703 730 757 784 811 838 865 892 919 946 973 1000 1027 0 1 2 3 4 5 6 Sample time (Index ร— 15s) Maxpoolsize max mean p50 p95 p99
  • 16. FlexyPool โ€“ Connection acquisition time 1 28 55 82 109 136 163 190 217 244 271 298 325 352 379 406 433 460 487 514 541 568 595 622 649 676 703 730 757 784 811 838 865 892 919 946 973 1000 1027 0 500 1000 1500 2000 2500 3000 3500 Sample time (Index ร— 15s) Connectionacquisitiontime(ms) max mean p50 p95 p99
  • 17. FlexyPool โ€“ Connection lease time 1 29 57 85 113 141 169 197 225 253 281 309 337 365 393 421 449 477 505 533 561 589 617 645 673 701 729 757 785 813 841 869 897 925 953 981 1009 1037 0 5000 10000 15000 20000 25000 30000 35000 40000 Sample time (Index ร— 15s) Connectionleasetime(ms) max mean p50 p95 p99
  • 18. Statement Batching statement.addBatch( "INSERT INTO post "(title, version, id) " + "VALUES ('Post no. 1', 0, 1)"); statement.addBatch( "INSERT INTO post_comment (post_id, review, version, id) " + "VALUES (1, 'Post comment 1.1', 0, 1)"); int[] updateCounts = statement.executeBatch(); ๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
  • 19. Statement Batching (5k rows) 1 10 20 30 40 50 60 70 80 90 100 1000 0 500 1000 1500 2000 2500 Batch size Time(ms) DB_A DB_B DB_C DB_D
  • 20. Oracle Statement batching โ€ข For Statement and CallableStatement, the Oracle JDBC Driver doesnโ€™t actually support batching, each statement being executed separately.
  • 21. MySQL Statement batching โ€ข By default, the MySQL JDBC driver doesnโ€™t send the batched statements in a single request. โ€ข The rewriteBatchedStatements connection property adds all batched statements to a String buffer.
  • 22. Batch PreparedStatements PreparedStatement postStatement = connection.prepareStatement( "INSERT INTO Post (title, version, id) VALUES (?, ?, ?)"); postStatement.setString(1, String.format("Post no. %1$d", 1)); postStatement.setInt(2, 0); postStatement.setLong(3, 1); postStatement.addBatch(); postStatement.setString(1, String.format("Post no. %1$d", 2)); postStatement.setInt(2, 0); postStatement.setLong(3, 2); postStatement.addBatch(); int[] updateCounts = postStatement.executeBatch();
  • 23. Batch PreparedStatements โ€ข SQL Injection Prevention โ€ข Better performance โ€ข Hibernate can batch statements automatically
  • 24. Insert PreparedStatement batching (5k rows) 1 10 20 30 40 50 60 70 80 90 100 1000 0 200 400 600 800 1000 1200 1400 1600 Batch size Time(ms) DB_A DB_B DB_C DB_D
  • 25. Update PreparedStatement batching (5k rows) 1 10 20 30 40 50 60 70 80 90 100 1000 0 100 200 300 400 500 600 700 Batch size Time(ms) DB_A DB_B DB_C DB_D
  • 26. Delete PreparedStatement batching (5k rows) 1 10 20 30 40 50 60 70 80 90 100 1000 0 200 400 600 800 1000 1200 Batch size Time(ms) DB_A DB_B DB_C DB_D
  • 27. Statement caching ๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
  • 28. Statement caching gain (one minute interval) Database System No Caching Throughput (SPM) Caching Throughput (SPM) Percentage Gain DB_A 419 833 507 286 20.83% DB_B 194 837 303 100 55.56% DB_C 116 708 166 443 42.61% DB_D 15 522 15 550 0.18%
  • 29. Oracle server-side statement caching โ€ข Hard parse โ€ข Soft parse โ€ข Bind peeking โ€ข Adaptive cursor sharing (since 11g)
  • 30. SQL Server server-side statement caching โ€ข Execution plan cache โ€ข Parameter sniffing โ€ข Force recompile SELECT * FROM task WHERE status = ? OPTION(RECOMPILE);
  • 31. PostgreSQL server-side statement caching โ€ข Prior to 9.2 โ€“ execution plan caching โ€ข 9.2 โ€“ optimization and planning are deferred โ€ข The prepareThreshold connection property
  • 32. MySQL server-side statement caching โ€ข No execution plan cache โ€ข Since Connector/J 5.0.5 PreparedStatements are only emulated โ€ข To activate server-side prepared statements: โ€ข useServerPrepStmts โ€ข cachePrepStmts
  • 33. Client-side statement caching โ€ข Recycling Statement, PreparedStatement or CallableStatement objects โ€ข Reusing database cursors
  • 34. Oracle implicit client-side statement caching โ€ข Connection-level cache โ€ข PreparedStatement and CallabledStatement only connectionProperties.put( "oracle.jdbc.implicitStatementCacheSize", Integer.toString(cacheSize) ); dataSource.setConnectionProperties( connectionProperties );
  • 35. Oracle implicit client-side statement caching โ€ข Can be disabled on a per statement basis if (statement.isPoolable()) { statement.setPoolable(false); }
  • 36. Oracle explicit client-side statement caching โ€ข Caches both metadata and execution state with data OracleConnection oracleConnection = (OracleConnection) connection; oracleConnection.setExplicitCachingEnabled(true); oracleConnection.setStatementCacheSize(cacheSize);
  • 37. Oracle explicit client-side statement caching โ€ข Vendor-specific API PreparedStatement statement = oracleConnection. getStatementWithKey(SELECT_POST_KEY); if (statement == null) statement = connection.prepareStatement(SELECT_POST); try { statement.setInt(1, 10); statement.execute(); } finally { ((OraclePreparedStatement) statement). closeWithKey(SELECT_POST_KEY); }
  • 38. SQL Server client-side statement caching โ€ข Microsoft JDBC Driver 4.2 disableStatementPooling โ€ข jTDS 1.3.1 โ€“ JDBC 3.0 JtdsDataSource jdtsDataSource = (JtdsDataSource) dataSource; jdtsDataSource.setMaxStatements(cacheSize);
  • 39. PostgreSQL Server client-side statement caching โ€ข PostgreSQL JDBC Driver 9.4-1202 makes client-side statement connection-bound instead of statement-bound โ€ข Configurable: โ€ข preparedStatementCacheQueries (default is 256) โ€ข preparedStatementCacheSizeMiB (default is 5MB) โ€ข Statement.setPoolable(false) is not supported
  • 40. MySQL Server client-side statement caching โ€ข Configurable: โ€ข cachePrepStmts (default is false) Required for server-side statement caching as well โ€ข prepStmtCacheSize (default is 25) โ€ข prepStmtCacheSqlLimit (default is 256) โ€ข Statement.setPoolable(false) works for client-side statements only
  • 41. ResultSet fetch size โ€ข ResultSet - application-level cursor ๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’ statement.setFetchSize(fetchSize);
  • 42. Oracle ResultSet fetch size โ€ข Default fetch size is 10 โ€ข Oracle 10i and 11g JDBC Driver maximum ResultSet size memory preallocation โ€ข VARCHAR2(4000) โ€“ allocates 8000 bytes (even for 1 character) โ€ข Memory buffers are recycled only when using Statement caching โ€ข Oracle 12c allocates memory on demand โ€ข VARCHAR2(4000) โ€“ 15 bytes + the actual row column size
  • 43. SQL Server ResultSet fetch size โ€ข Adaptive buffering โ€ข Only for the default read-only and forward-only ResultSet โ€ข Updatable cursors use fixed data blocks
  • 44. PostgreSQL ResultSet fetch size โ€ข Fetch all โ€“ one database roundtrip โ€ข Custom fetch size โ€“ database cursor
  • 45. MySQL ResultSet fetch size โ€ข Fetch all โ€“ one database roundtrip โ€ข Streaming โ€“ only one record at a time
  • 46. ResultSet fetch size (10k rows) 1 10 100 1000 10000 0 100 200 300 400 500 600 Fetch size Time(ms) DB_A DB_B DB_C DB_D
  • 47. ResultSet size โ€ข Avoid fetching data that is not required โ€ข Hibernate addresses the max-size vendor-specific SQL statement syntax
  • 48. SQL:2008 ResultSet size limit โ€ข Oracle 12c, SQL Server 2012 and PostgreSQL 8.4 SELECT pc.id AS pc_id, p.title AS p_title FROM post_comment pc INNER JOIN post p ON p.id = pc.post_id ORDER BY pc_id OFFSET ? ROWS FETCH FIRST (?) ROWS ONLY;
  • 49. Oracle ResultSet size limit SELECT * FROM ( SELECT pc.id AS pc_id, p.title AS p_title FROM post_comment pc INNER JOIN post p ON p.id = pc.post_id ORDER BY pc_id ) WHERE ROWNUM <= ?
  • 50. SQL Server ResultSet size limit SELECT TOP (?) pc.id AS pc_id, p.title AS p_title FROM post_comment pc INNER JOIN post p ON p.id = pc.post_id ORDER BY pc_id
  • 51. PostgreSQL and MySQL ResultSet size limit SELECT pc.id AS pc_id, p.title AS p_title FROM post_comment pc INNER JOIN post p ON p.id = pc.post_id ORDER BY pc_id LIMIT ?
  • 52. Statement max rows โ€ข Vendor-independent syntax โ€ข Might not influence the execution plan โ€ข According to the documentation: โ€œIf the limit is exceeded, the excess rows are silently dropped.โ€ statement.setMaxRows(maxRows);
  • 53. Max size: 1 million vs 100 rows Fetch all Fetch max rows Fetch limit 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 Time(ms) DB_A DB_B DB_C DB_D
  • 54. Fetching too many columns โ€ข Fetching all column (ORM tools) SELECT * FROM post_comment pc INNER JOIN post p ON p.id = pc.post_id INNER JOIN post_details pd ON p.id = pd.id
  • 55. Fetching too many columns โ€ข Fetching a custom SQL projection SELECT pc.version FROM post_comment pc INNER JOIN post p ON p.id = pc.post_id INNER JOIN post_details pd ON p.id = pd.id
  • 56. Fetching too many columns performance impact All columns Custom projection 0 5 10 15 20 25 30 Time(ms) DB_A DB_B DB_C DB_D
  • 57. Processing Logic โ€ข Hibernate defers connection acquisition โ€ข Release connection as soon as possible ๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’
  • 58. Questions and Answers ๐‘‡ = ๐‘ก ๐‘Ž๐‘๐‘ž + ๐‘ก ๐‘Ÿ๐‘’๐‘ž + ๐‘ก ๐‘’๐‘ฅ๐‘’๐‘ + ๐‘ก ๐‘Ÿ๐‘’๐‘  + ๐‘ก๐‘–๐‘‘๐‘™๐‘’ โ€ข Response time โ€ข Connection management โ€ข Batch updates โ€ข Statement caching โ€ข ResultSet fetching โ€ข https://leanpub.com/high-performance-java-persistence