SlideShare una empresa de Scribd logo
1 de 25
Descargar para leer sin conexión
JDBC Best
Practices for DB2
Programmers
Derek C. Ashmore,
Delta Vortex Technologies

Session B1 – 10:30am
May 10, 2004
JDBC Best Practices for DB2
Programmers

• Presentation Summary
   –   JDBC Coding for Performance
   –   JDBC Coding for Maintainability
   –   JDBC Coding for Portability
   –   Future Directions
Who Am I?

• Derek C. Ashmore
• Author of The J2EE™ Architect’s Handbook
   – Downloadable at:
   – http://www.dvtpress.com/javaarch
• Over 6 years of Java-related experience
• Over 10 years of database design/administration experience.
• Can be reached at dashmore@dvt.com
Data Access Options

•   JDBC (dynamic access)
•   SQL/J (static access)
•   J2EE Entity Beans
•   Object-Relational Mapping Toolsets
    – Hibernate
    – JDO
    – Many others…..
Why focus on JDBC?

• JDBC the most commonly used.
   – Not a technical judgment – just an observation.
• Why is JDBC the most common access method?
   –   It was the first access method available.
   –   It works
   –   It satisfies developers needs
   –   Most DBMSs support it.
   –   JDBC skills are easy to find in the market
• JDBC tutorial at: http://java.sun.com/docs/books/tutorial/jdbc/
JDBC Coding for Performance

• Use PreparedStatements with host variable markers instead of
  Statements.
• Beware that selects issue shared locks by default
• Consider query fetch sizing
• Utilize connection pooling features
Use PreparedStatements

Use PreparedStatements with parameter markers instead of Statements
   Use “select name from Customer where id = ?”
   Instead of “…. where id = ‘H23’”
Statements are less typing but…….
    Extra String Processing to assemble the where clause.
    Circumvents Dynamic Caching
        Prevents reuse of the query access path by the database.
        This means that statements will be Slower
Dynamic Caching improves performance of dynamic SQL
    DB2/UDB (all environments) caches plans associated with dynamic SQL
       statements
    Like statements will reuse that dynamically stored plan
    Dynamic SQL gets many of the performance benefits that static SQL has.
Beware of Shared Locks

• Beware of shared locking with Select statements
   –   Common Myth: Reading is harmless
   –   Cursor Stability is default == Shared Locks
   –   When only Reading: Commit as early as possible (or use autocommit)
   –   Use “commit” or JDBCs autocommit feature for selects, but don’t use
       both.
        • Issues the commit twice   lower performance
Consider Setting the query fetch size

•   Instruct database to return rows in batches of 10 to 100.
•   Example Query Fetch Sizing
     – statement.setFetchSize(10)
•   Higher isn’t always better
     – Can degrade performance if used for small ResultSets.
•   Has Fewer network round-trips
     – Most benefit using batches of 10 to 100 – diminishing returns after that.
          • Larger benefit reducing network trips from 100,000 to 1,000 than from 100,000 to 100.
          • The larger the batch, the more memory required.
•   Needs to be tested on a case-by-case basis.
Utilize Connection Pooling

• Connection Pools eliminate wait time for database
  connections by creating them ahead of time.
   – I’ve seen enough J2EE apps managing connection creation directly to
     warrant this practice.
   – Connections take 30 – 50 ms depending on platform.
   – Allows for capacity planning of database resources
   – Provides automatic recovery from database or network outages
• Issuing close() on a pooled connection merely returns it to the
  pool for use by another request.
JDBC Coding for Maintainability

• Close JDBC Objects in a “finally” block.
• Consolidate SQL string formation.
• Always specify column names in select and insert statements.
Close all JDBC Objects

•   Most JDBC Objects require a Statement Handle from DB2 Client.
     – Includes PreparedStatement, Statement, and ResultSet
     – This is a finite resource (e.g. either 600 or 1300 depending upon version).
•   Close all JDBC Objects in a finally block
     – Stranded JDBC consume scarce db resources
          • Cause errors down the line
          • DB2/UDB (all environments) w/DB2 Client   Statement handles are consumed
•   Close JDBC objects in the method that creates them.
     – Easier to implement this habit.
     – Easier to visually identify objects not being closed.
•   As the garbage collector “closes” these objects, but no guarantee of being
    under the resource limit.
     – You may not see problems until stress testing or production.
Closure Issues

• Closing JDBC Objects is inconvenient
   – Close() throws a SQLException
   – Leads to nested try/catch logic in the finally block
   – A lot to type
• Utility Support can make this easier
   – Use generic close utility that logs SQLExceptions received, but
     doesn’t throw an exception
   – Gets the “close” down to one line.
   – CementJ – http://sourceforge.net/projects/cementj
       • org.cementj.util.DatabaseUtility
Penalty for Object Leaks

• Applies if you’re using DB2/Client (which most do)
• Each JDBC Object acquires a statement handle within
  DB2/Client.
• Limited to between 600 and 1300 (depending on version of
  DB2/Client)
Closure Issues (con’t)

•   Finding Stranded JDBC Objects Problematic
     – This is especially difficult if you need to identify leaks in an application you
       didn’t write.
     – Use P6Spy with an extension library
     – P6Spy is a JDBC Profiler that logs SQL statements, and their execution time.
     – I’ve extended P6Spy so that it will identify all stranded objects and list SQL
       statements associated with them.
     – P6Spy available at http://www.p6spy.com/
     – P6Spy Extension at “Resources” link from www.dvtpress.com/javaarch
Consolidate SQL String formation

Some developers dynamically build the SQL string with scattered
  concatenation logic
    String sqlStmt = “select col1, col2 from tab1”;
    <<< more application code >>>
    sqlStmt = sqlStmt + “ where col2 > 200”;
    <<< more application code >>>
    sqlStmt = sqlStmt + “ and col3 < 5”;
With a small number of apps, this is necessary, but most can consolidate the
   logic.
Disadvantages
    Harder to use dynamic caching
    Harder to read
    More String Processing
    More Memory Allocation
Consolidate SQL String Example

Using “static” variables for SQL text
   Reduces string processing and memory allocation as happens
     when the class is first referenced.
   Consolidates SQL text so that it’s easier to read.
Example
  public static final String CUST_SQL=
      “select name from Cust where id = ?”;
   ……
   pStmt = conn.prepareStatement(CUST_SQL)
Specify Column Names

• Always specify column names in select and insert statements.
   – Code won’t break if DBA changes column order
   – Clearer for maintenance purposes
       • Imagine a select or insert statement involving 20-30 columns
            –   Hard to tell which value pertains to which column

• Specify column name instead of offset when using ResultSets
   – Use resultSet.getString(“col1”);
   – Instead of resultSet.getString(3);
JDBC Coding for Portability

• Limit use of platform-specific features.
• Reference java.sql or javax.sql classes only
   – Avoid DB2-specific classes
Limit use of Platform-specific features

•   Portability == The ability to switch DBMSs.
•   Use of platform-specific features create portability obstacles
     – Your code might live longer than you think (Y2K).
•   Only use when clear benefit – not out of habit
•   Examples
     – Stored procedures using proprietary language
     – Proprietary Column Functions
         • ENCRYPT
         • NULLIF
     – Proprietary Operators
         • CASE
         • OLAP (e.g. RANK)
Reference java.sql or javax.sql classes
only
• Avoid vendor-specific class implementations unless required
  for performance
   – Usually not necessary now
       • Was necessary in early days before formal support for
            –   Fetch sizing/Array Processing
            –   Statement Batching

   – Creates a portability issue
       • Harder to switch DBMSs
   – Creates a maintenance issue
       • The JDBC interfaces are familiar
       • Proprietary objects may not be
Latest Developments

• JDBC 3.0 Specification
   –   Return generated PK value on insert.
   –   ResultSet Holdability – exist through commits
   –   Support multiple ResultSets for stored procedure fans
   –   Standardizes Connection Pooling
   –   Adds PreparedStatement pooling
   –   Savepoint support
Future Directions

• JDBC is a maturing spec
   – Expect frequency of change to slow considerably
• Use of Object-Relational mapping toolsets is increasing
   – Hibernate (www.hibernate.org)
   – JDO (www.jdocentral.com)
• Despite technical advances, entity beans are close to
  becoming a part of history.
Stored Procedure Use

•   Aren’t Stored Procedures better performing?
     – Depends on platform
         • Sybase – yes, Oracle/DB2 – not always
     – As a general rule, CPU intensive actions are bad as stored procedures
     – SQL are statically bound
         • Used to be more significant before dynamic caching
     – As a rule, stored procedures help performance by reducing the number of
       network transmissions.
         • Conditional selects or updates
         • As a batch update surrogate (combining larger numbers of SQL statements)
•   Ask: How many network transmissions will be saved by making this a
    stored procedure? If the answer is “0”, performance is not likely to be
    improved.
Header Text


Questions

•   JDBC Best Practices for DB2 Programmers
•   Session B1
•   Derek C. Ashmore
•   Email: dashmore@dvt.com

Más contenido relacionado

La actualidad más candente

Dao pattern
Dao patternDao pattern
Dao patternciriako
 
Database design i_-_1_dl300
Database design i_-_1_dl300Database design i_-_1_dl300
Database design i_-_1_dl300KOI Lastone
 
Real World Experience: Integrating DB2 with XPages
Real World Experience: Integrating DB2 with XPagesReal World Experience: Integrating DB2 with XPages
Real World Experience: Integrating DB2 with XPagesSteve_Zavocki
 
Hibernate Developer Reference
Hibernate Developer ReferenceHibernate Developer Reference
Hibernate Developer ReferenceMuthuselvam RS
 
Hibernate in XPages
Hibernate in XPagesHibernate in XPages
Hibernate in XPagesToby Samples
 
JPA and Coherence with TopLink Grid
JPA and Coherence with TopLink GridJPA and Coherence with TopLink Grid
JPA and Coherence with TopLink GridJames Bayer
 
Java EE 7 for WebLogic 12c Developers
Java EE 7 for WebLogic 12c DevelopersJava EE 7 for WebLogic 12c Developers
Java EE 7 for WebLogic 12c DevelopersBruno Borges
 
Create non-cdb (traditional) oracle database 12c on windows
Create non-cdb (traditional) oracle database 12c on windowsCreate non-cdb (traditional) oracle database 12c on windows
Create non-cdb (traditional) oracle database 12c on windowsBiju Thomas
 
Working with Oracle Queues - Choosing between AQ and JMS
Working with Oracle Queues - Choosing between AQ and JMSWorking with Oracle Queues - Choosing between AQ and JMS
Working with Oracle Queues - Choosing between AQ and JMSRevelation Technologies
 
IaC MeetUp Active Directory Setup for Oracle Security LAB
IaC MeetUp Active Directory Setup for Oracle Security LABIaC MeetUp Active Directory Setup for Oracle Security LAB
IaC MeetUp Active Directory Setup for Oracle Security LABStefan Oehrli
 
Distributed Caching Using the JCACHE API and ehcache, Including a Case Study ...
Distributed Caching Using the JCACHE API and ehcache, Including a Case Study ...Distributed Caching Using the JCACHE API and ehcache, Including a Case Study ...
Distributed Caching Using the JCACHE API and ehcache, Including a Case Study ...elliando dias
 
PostgreSQL and Benchmarks
PostgreSQL and BenchmarksPostgreSQL and Benchmarks
PostgreSQL and BenchmarksJignesh Shah
 
Exadata and the Oracle Optimizer: The Untold Story
Exadata and the Oracle Optimizer: The Untold StoryExadata and the Oracle Optimizer: The Untold Story
Exadata and the Oracle Optimizer: The Untold StoryEnkitec
 
App Grid Dev With Coherence
App Grid Dev With CoherenceApp Grid Dev With Coherence
App Grid Dev With CoherenceJames Bayer
 
3.java database connectivity
3.java database connectivity3.java database connectivity
3.java database connectivityweb360
 
Best practices for_large_oracle_apps_r12_implementations
Best practices for_large_oracle_apps_r12_implementationsBest practices for_large_oracle_apps_r12_implementations
Best practices for_large_oracle_apps_r12_implementationsAjith Narayanan
 

La actualidad más candente (19)

Dao pattern
Dao patternDao pattern
Dao pattern
 
Database design i_-_1_dl300
Database design i_-_1_dl300Database design i_-_1_dl300
Database design i_-_1_dl300
 
Real World Experience: Integrating DB2 with XPages
Real World Experience: Integrating DB2 with XPagesReal World Experience: Integrating DB2 with XPages
Real World Experience: Integrating DB2 with XPages
 
Hibernate Developer Reference
Hibernate Developer ReferenceHibernate Developer Reference
Hibernate Developer Reference
 
Hibernate in XPages
Hibernate in XPagesHibernate in XPages
Hibernate in XPages
 
JPA and Coherence with TopLink Grid
JPA and Coherence with TopLink GridJPA and Coherence with TopLink Grid
JPA and Coherence with TopLink Grid
 
JSP - Part 1
JSP - Part 1JSP - Part 1
JSP - Part 1
 
Persistence hibernate
Persistence hibernatePersistence hibernate
Persistence hibernate
 
Java EE 7 for WebLogic 12c Developers
Java EE 7 for WebLogic 12c DevelopersJava EE 7 for WebLogic 12c Developers
Java EE 7 for WebLogic 12c Developers
 
Connection Pooling
Connection PoolingConnection Pooling
Connection Pooling
 
Create non-cdb (traditional) oracle database 12c on windows
Create non-cdb (traditional) oracle database 12c on windowsCreate non-cdb (traditional) oracle database 12c on windows
Create non-cdb (traditional) oracle database 12c on windows
 
Working with Oracle Queues - Choosing between AQ and JMS
Working with Oracle Queues - Choosing between AQ and JMSWorking with Oracle Queues - Choosing between AQ and JMS
Working with Oracle Queues - Choosing between AQ and JMS
 
IaC MeetUp Active Directory Setup for Oracle Security LAB
IaC MeetUp Active Directory Setup for Oracle Security LABIaC MeetUp Active Directory Setup for Oracle Security LAB
IaC MeetUp Active Directory Setup for Oracle Security LAB
 
Distributed Caching Using the JCACHE API and ehcache, Including a Case Study ...
Distributed Caching Using the JCACHE API and ehcache, Including a Case Study ...Distributed Caching Using the JCACHE API and ehcache, Including a Case Study ...
Distributed Caching Using the JCACHE API and ehcache, Including a Case Study ...
 
PostgreSQL and Benchmarks
PostgreSQL and BenchmarksPostgreSQL and Benchmarks
PostgreSQL and Benchmarks
 
Exadata and the Oracle Optimizer: The Untold Story
Exadata and the Oracle Optimizer: The Untold StoryExadata and the Oracle Optimizer: The Untold Story
Exadata and the Oracle Optimizer: The Untold Story
 
App Grid Dev With Coherence
App Grid Dev With CoherenceApp Grid Dev With Coherence
App Grid Dev With Coherence
 
3.java database connectivity
3.java database connectivity3.java database connectivity
3.java database connectivity
 
Best practices for_large_oracle_apps_r12_implementations
Best practices for_large_oracle_apps_r12_implementationsBest practices for_large_oracle_apps_r12_implementations
Best practices for_large_oracle_apps_r12_implementations
 

Similar a Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004

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 ISivaSankari36
 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Modelkunj desai
 
Java Developers, make the database work for you (NLJUG JFall 2010)
Java Developers, make the database work for you (NLJUG JFall 2010)Java Developers, make the database work for you (NLJUG JFall 2010)
Java Developers, make the database work for you (NLJUG JFall 2010)Lucas Jellema
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivityVaishali Modi
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivityVaishali Modi
 
OOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptxOOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptxTanzila Kehkashan
 
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 JDBCSimba Technologies
 
High Performance Jdbc
High Performance JdbcHigh Performance Jdbc
High Performance JdbcSam Pattsin
 
OOW09 Ebs Tuning Final
OOW09 Ebs Tuning FinalOOW09 Ebs Tuning Final
OOW09 Ebs Tuning Finaljucaab
 
PROGRAMMING IN JAVA- unit 5-part II
PROGRAMMING IN JAVA- unit 5-part IIPROGRAMMING IN JAVA- unit 5-part II
PROGRAMMING IN JAVA- unit 5-part IISivaSankari36
 
R2DBC Reactive Relational Database Connectivity
R2DBC Reactive Relational Database ConnectivityR2DBC Reactive Relational Database Connectivity
R2DBC Reactive Relational Database ConnectivityMaarten Smeets
 
Java Course 13: JDBC & Logging
Java Course 13: JDBC & LoggingJava Course 13: JDBC & Logging
Java Course 13: JDBC & LoggingAnton Keks
 
Vision_Academy_Ajava_final(sachin_sir9823037693)_22 (1).pdf
Vision_Academy_Ajava_final(sachin_sir9823037693)_22 (1).pdfVision_Academy_Ajava_final(sachin_sir9823037693)_22 (1).pdf
Vision_Academy_Ajava_final(sachin_sir9823037693)_22 (1).pdfbhagyashri686896
 
Vision_Academy_Ajava_final(sachin_sir9823037693)_22.pdf
Vision_Academy_Ajava_final(sachin_sir9823037693)_22.pdfVision_Academy_Ajava_final(sachin_sir9823037693)_22.pdf
Vision_Academy_Ajava_final(sachin_sir9823037693)_22.pdfVisionAcademyProfSac
 

Similar a Jdbc Best Practices - DB2/ IDUG - Orlando, May 10, 2004 (20)

Jdbc
JdbcJdbc
Jdbc
 
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
 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Model
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
10 jdbc
10 jdbc10 jdbc
10 jdbc
 
Java Developers, make the database work for you (NLJUG JFall 2010)
Java Developers, make the database work for you (NLJUG JFall 2010)Java Developers, make the database work for you (NLJUG JFall 2010)
Java Developers, make the database work for you (NLJUG JFall 2010)
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
OOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptxOOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptx
 
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
 
MongoDB
MongoDBMongoDB
MongoDB
 
High Performance Jdbc
High Performance JdbcHigh Performance Jdbc
High Performance Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
OOW09 Ebs Tuning Final
OOW09 Ebs Tuning FinalOOW09 Ebs Tuning Final
OOW09 Ebs Tuning Final
 
PROGRAMMING IN JAVA- unit 5-part II
PROGRAMMING IN JAVA- unit 5-part IIPROGRAMMING IN JAVA- unit 5-part II
PROGRAMMING IN JAVA- unit 5-part II
 
R2DBC Reactive Relational Database Connectivity
R2DBC Reactive Relational Database ConnectivityR2DBC Reactive Relational Database Connectivity
R2DBC Reactive Relational Database Connectivity
 
Java Course 13: JDBC & Logging
Java Course 13: JDBC & LoggingJava Course 13: JDBC & Logging
Java Course 13: JDBC & Logging
 
Vision_Academy_Ajava_final(sachin_sir9823037693)_22 (1).pdf
Vision_Academy_Ajava_final(sachin_sir9823037693)_22 (1).pdfVision_Academy_Ajava_final(sachin_sir9823037693)_22 (1).pdf
Vision_Academy_Ajava_final(sachin_sir9823037693)_22 (1).pdf
 
Vision_Academy_Ajava_final(sachin_sir9823037693)_22.pdf
Vision_Academy_Ajava_final(sachin_sir9823037693)_22.pdfVision_Academy_Ajava_final(sachin_sir9823037693)_22.pdf
Vision_Academy_Ajava_final(sachin_sir9823037693)_22.pdf
 
22jdbc
22jdbc22jdbc
22jdbc
 

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

  • 1. JDBC Best Practices for DB2 Programmers Derek C. Ashmore, Delta Vortex Technologies Session B1 – 10:30am May 10, 2004
  • 2. JDBC Best Practices for DB2 Programmers • Presentation Summary – JDBC Coding for Performance – JDBC Coding for Maintainability – JDBC Coding for Portability – Future Directions
  • 3. Who Am I? • Derek C. Ashmore • Author of The J2EE™ Architect’s Handbook – Downloadable at: – http://www.dvtpress.com/javaarch • Over 6 years of Java-related experience • Over 10 years of database design/administration experience. • Can be reached at dashmore@dvt.com
  • 4. Data Access Options • JDBC (dynamic access) • SQL/J (static access) • J2EE Entity Beans • Object-Relational Mapping Toolsets – Hibernate – JDO – Many others…..
  • 5. Why focus on JDBC? • JDBC the most commonly used. – Not a technical judgment – just an observation. • Why is JDBC the most common access method? – It was the first access method available. – It works – It satisfies developers needs – Most DBMSs support it. – JDBC skills are easy to find in the market • JDBC tutorial at: http://java.sun.com/docs/books/tutorial/jdbc/
  • 6. JDBC Coding for Performance • Use PreparedStatements with host variable markers instead of Statements. • Beware that selects issue shared locks by default • Consider query fetch sizing • Utilize connection pooling features
  • 7. Use PreparedStatements Use PreparedStatements with parameter markers instead of Statements Use “select name from Customer where id = ?” Instead of “…. where id = ‘H23’” Statements are less typing but……. Extra String Processing to assemble the where clause. Circumvents Dynamic Caching Prevents reuse of the query access path by the database. This means that statements will be Slower Dynamic Caching improves performance of dynamic SQL DB2/UDB (all environments) caches plans associated with dynamic SQL statements Like statements will reuse that dynamically stored plan Dynamic SQL gets many of the performance benefits that static SQL has.
  • 8. Beware of Shared Locks • Beware of shared locking with Select statements – Common Myth: Reading is harmless – Cursor Stability is default == Shared Locks – When only Reading: Commit as early as possible (or use autocommit) – Use “commit” or JDBCs autocommit feature for selects, but don’t use both. • Issues the commit twice lower performance
  • 9. Consider Setting the query fetch size • Instruct database to return rows in batches of 10 to 100. • Example Query Fetch Sizing – statement.setFetchSize(10) • Higher isn’t always better – Can degrade performance if used for small ResultSets. • Has Fewer network round-trips – Most benefit using batches of 10 to 100 – diminishing returns after that. • Larger benefit reducing network trips from 100,000 to 1,000 than from 100,000 to 100. • The larger the batch, the more memory required. • Needs to be tested on a case-by-case basis.
  • 10. Utilize Connection Pooling • Connection Pools eliminate wait time for database connections by creating them ahead of time. – I’ve seen enough J2EE apps managing connection creation directly to warrant this practice. – Connections take 30 – 50 ms depending on platform. – Allows for capacity planning of database resources – Provides automatic recovery from database or network outages • Issuing close() on a pooled connection merely returns it to the pool for use by another request.
  • 11. JDBC Coding for Maintainability • Close JDBC Objects in a “finally” block. • Consolidate SQL string formation. • Always specify column names in select and insert statements.
  • 12. Close all JDBC Objects • Most JDBC Objects require a Statement Handle from DB2 Client. – Includes PreparedStatement, Statement, and ResultSet – This is a finite resource (e.g. either 600 or 1300 depending upon version). • Close all JDBC Objects in a finally block – Stranded JDBC consume scarce db resources • Cause errors down the line • DB2/UDB (all environments) w/DB2 Client Statement handles are consumed • Close JDBC objects in the method that creates them. – Easier to implement this habit. – Easier to visually identify objects not being closed. • As the garbage collector “closes” these objects, but no guarantee of being under the resource limit. – You may not see problems until stress testing or production.
  • 13. Closure Issues • Closing JDBC Objects is inconvenient – Close() throws a SQLException – Leads to nested try/catch logic in the finally block – A lot to type • Utility Support can make this easier – Use generic close utility that logs SQLExceptions received, but doesn’t throw an exception – Gets the “close” down to one line. – CementJ – http://sourceforge.net/projects/cementj • org.cementj.util.DatabaseUtility
  • 14. Penalty for Object Leaks • Applies if you’re using DB2/Client (which most do) • Each JDBC Object acquires a statement handle within DB2/Client. • Limited to between 600 and 1300 (depending on version of DB2/Client)
  • 15. Closure Issues (con’t) • Finding Stranded JDBC Objects Problematic – This is especially difficult if you need to identify leaks in an application you didn’t write. – Use P6Spy with an extension library – P6Spy is a JDBC Profiler that logs SQL statements, and their execution time. – I’ve extended P6Spy so that it will identify all stranded objects and list SQL statements associated with them. – P6Spy available at http://www.p6spy.com/ – P6Spy Extension at “Resources” link from www.dvtpress.com/javaarch
  • 16. Consolidate SQL String formation Some developers dynamically build the SQL string with scattered concatenation logic String sqlStmt = “select col1, col2 from tab1”; <<< more application code >>> sqlStmt = sqlStmt + “ where col2 > 200”; <<< more application code >>> sqlStmt = sqlStmt + “ and col3 < 5”; With a small number of apps, this is necessary, but most can consolidate the logic. Disadvantages Harder to use dynamic caching Harder to read More String Processing More Memory Allocation
  • 17. Consolidate SQL String Example Using “static” variables for SQL text Reduces string processing and memory allocation as happens when the class is first referenced. Consolidates SQL text so that it’s easier to read. Example public static final String CUST_SQL= “select name from Cust where id = ?”; …… pStmt = conn.prepareStatement(CUST_SQL)
  • 18. Specify Column Names • Always specify column names in select and insert statements. – Code won’t break if DBA changes column order – Clearer for maintenance purposes • Imagine a select or insert statement involving 20-30 columns – Hard to tell which value pertains to which column • Specify column name instead of offset when using ResultSets – Use resultSet.getString(“col1”); – Instead of resultSet.getString(3);
  • 19. JDBC Coding for Portability • Limit use of platform-specific features. • Reference java.sql or javax.sql classes only – Avoid DB2-specific classes
  • 20. Limit use of Platform-specific features • Portability == The ability to switch DBMSs. • Use of platform-specific features create portability obstacles – Your code might live longer than you think (Y2K). • Only use when clear benefit – not out of habit • Examples – Stored procedures using proprietary language – Proprietary Column Functions • ENCRYPT • NULLIF – Proprietary Operators • CASE • OLAP (e.g. RANK)
  • 21. Reference java.sql or javax.sql classes only • Avoid vendor-specific class implementations unless required for performance – Usually not necessary now • Was necessary in early days before formal support for – Fetch sizing/Array Processing – Statement Batching – Creates a portability issue • Harder to switch DBMSs – Creates a maintenance issue • The JDBC interfaces are familiar • Proprietary objects may not be
  • 22. Latest Developments • JDBC 3.0 Specification – Return generated PK value on insert. – ResultSet Holdability – exist through commits – Support multiple ResultSets for stored procedure fans – Standardizes Connection Pooling – Adds PreparedStatement pooling – Savepoint support
  • 23. Future Directions • JDBC is a maturing spec – Expect frequency of change to slow considerably • Use of Object-Relational mapping toolsets is increasing – Hibernate (www.hibernate.org) – JDO (www.jdocentral.com) • Despite technical advances, entity beans are close to becoming a part of history.
  • 24. Stored Procedure Use • Aren’t Stored Procedures better performing? – Depends on platform • Sybase – yes, Oracle/DB2 – not always – As a general rule, CPU intensive actions are bad as stored procedures – SQL are statically bound • Used to be more significant before dynamic caching – As a rule, stored procedures help performance by reducing the number of network transmissions. • Conditional selects or updates • As a batch update surrogate (combining larger numbers of SQL statements) • Ask: How many network transmissions will be saved by making this a stored procedure? If the answer is “0”, performance is not likely to be improved.
  • 25. Header Text Questions • JDBC Best Practices for DB2 Programmers • Session B1 • Derek C. Ashmore • Email: dashmore@dvt.com