667514 members! Sign up to stay informed.

Sponsored Links


Resources

Enterprise Java
Research Library

Get Java white papers, product information, case studies and webcasts

News News News Messages: 18 Messages: 18 Messages: 18 Printer friendly Printer friendly Printer friendly Post reply Post reply Post reply XML XML XML

JDBC Tips: Selecting Functions that Optimize Performance

Posted by: Nitin Bharti on June 04, 2004 DIGG
In this JDBC Performance Tip, Part 3 of the JDBC Peformance Series, John Goodson discusses JDBC objects and methods that improve performance. He looks at how to use parameter markers as arguments to stored procedures, the benefits of using the Statement vs. PreparedStatement object, how to reduce network overhead by using the addBatch() method and more.

Read Performance Tips for the Data Tier (JDBC) - Part 3: Selecing Functions that Optimize Performance

Threaded replies

·  JDBC Tips: Selecting Functions that Optimize Performance by Nitin Bharti on Fri Jun 04 18:06:10 EDT 2004
  ·  Good -- if it is supported by Avi Abrami on Sat Jun 05 01:09:30 EDT 2004
    ·  Use of parameter Markers by R Damle on Sat Jun 05 06:15:44 EDT 2004
      ·  Use of parameter Markers by Mileta Cekovic on Sat Jun 05 18:17:59 EDT 2004
        ·  Use of parameter Markers by Calum Shaw-Mackay on Sun Jun 06 09:27:28 EDT 2004
    ·  Using get method effectively by mahendra malviya on Sun Jun 06 15:58:54 EDT 2004
      ·  Using get method effectively by Juozas Baliuka on Mon Jun 07 03:04:17 EDT 2004
        ·  Using get method effectively by Eunmi Jang on Mon Jun 07 04:22:14 EDT 2004
          ·  Using get method effectively by Juozas Baliuka on Mon Jun 07 05:36:01 EDT 2004
  ·  JDBC Tips: Selecting Functions that Optimize Performance by Mileta Cekovic on Sat Jun 05 18:21:57 EDT 2004
    ·  When will TheServerSide allow post editing ? by Mileta Cekovic on Sat Jun 05 18:25:28 EDT 2004
      ·  When will TheServerSide allow post editing ? by Andrew Stevens on Mon Jun 07 13:15:05 EDT 2004
  ·  Pagination by Hisham MK on Sun Jun 06 21:34:59 EDT 2004
    ·  Pagination by Juozas Baliuka on Mon Jun 07 03:09:01 EDT 2004
      ·  Pagination by Mileta Cekovic on Mon Jun 07 05:42:58 EDT 2004
      ·  Pagination by Marcin Szczepanski on Mon Jun 07 20:57:49 EDT 2004
  ·  PreparedStatement to do insert not always the best technique by kjlkjlkj lkjlkjlkj on Tue Jun 08 04:54:44 EDT 2004
  ·  Retrieving Auto-Generated Keys by Dirk Ludwig on Tue Jun 08 09:02:03 EDT 2004
  ·  Bummed about PreparedStatements by tr mo on Tue Jun 08 16:06:11 EDT 2004
  Message #124739 Post reply Post reply Post reply Go to top Go to top Go to top

Good -- if it is supported

Posted by: Avi Abrami on June 05, 2004 in response to Message #124723
Retrieving Auto-Generated KeysUnfortunately, not all JDBC drivers (and DBMS's) support this feature -- like Oracle.
Use the Right Data-Type
Again, the Oracle database only has the NUMBER data-type -- no INTEGER, BYTE, SHORT, FLOAT, etc.

Good Luck,
Avi.

  Message #124748 Post reply Post reply Post reply Go to top Go to top Go to top

Use of parameter Markers

Posted by: R Damle on June 05, 2004 in response to Message #124739
It is true that using parameter markers increases performance
but I think it creates maintainability problems. What if order of parameters of stored procedure changes sometime later? Using parameter names makes code readable and maintainable.
Unless and until good documentation is done and certain rules for stored procedure modifications are followed, using parameter markers can create problems.

  Message #124771 Post reply Post reply Post reply Go to top Go to top Go to top

Use of parameter Markers

Posted by: Mileta Cekovic on June 05, 2004 in response to Message #124748
You can use named parameters from JDBC 3.0 (not all drivers support it).
Or you can write simple JDBC wrapper that supports named parameters. I wrote one myself and it is easy.

MC

  Message #124772 Post reply Post reply Post reply Go to top Go to top Go to top

JDBC Tips: Selecting Functions that Optimize Performance

Posted by: Mileta Cekovic on June 05, 2004 in response to Message #124723
>> Using get Methods Effectively

I relay can't see how column named lookup from result set is expensive. If JDBC driver is implemented well that should be not more than a HashMap lookup. Compared to network serilization and wast object creation in JDBC drivers this is realy nothing.

MC

  Message #124773 Post reply Post reply Post reply Go to top Go to top Go to top

When will TheServerSide allow post editing ?

Posted by: Mileta Cekovic on June 05, 2004 in response to Message #124772
Bl..dy typos...
>> I relay
should be: 'I realy'

When will TheServerSide allow post editing ?

  Message #124792 Post reply Post reply Post reply Go to top Go to top Go to top

Use of parameter Markers

Posted by: Calum Shaw-Mackay on June 06, 2004 in response to Message #124771
If it is supported, use databaseMetaData to get the columns for a specific procedure and then store the name, index, type and paramtype(IN,OUT,INOUT) of each column grouped into an object stored in a hashmap keyed by the procedure name.

--Calum

  Message #124802 Post reply Post reply Post reply Go to top Go to top Go to top

Using get method effectively

Posted by: mahendra malviya on June 06, 2004 in response to Message #124739
I was just wondering , in a enterprise project where Java developer and database DBA guys are two end, How easy to maintain the coulmn number in your code .
I would rather use the column name.

  Message #124808 Post reply Post reply Post reply Go to top Go to top Go to top

Pagination

Posted by: Hisham MK on June 06, 2004 in response to Message #124723
In general, do not write code that relies on the number of result rows from a query because drivers must fetch all rows in a result set to know how many rows the query will return.
What is the best way to do pagination (in web pages) without relying on number of rows?

  Message #124836 Post reply Post reply Post reply Go to top Go to top Go to top

Using get method effectively

Posted by: Juozas Baliuka on June 07, 2004 in response to Message #124802
I was just wondering , in a enterprise project where Java developer and database DBA guys are two end, How easy to maintain the coulmn number in your code . I would rather use the column name.
I found it is not a problem in practice. You can validate coulmn numbers on application startup automaticaly too.

  Message #124837 Post reply Post reply Post reply Go to top Go to top Go to top

Pagination

Posted by: Juozas Baliuka on June 07, 2004 in response to Message #124808
In general, do not write code that relies on the number of result rows from a query because drivers must fetch all rows in a result set to know how many rows the query will return.
What is the best way to do pagination (in web pages) without relying on number of rows?
You do not relying on number of rows for pagination if you use something like "LIMIT/OFFSET".

  Message #124843 Post reply Post reply Post reply Go to top Go to top Go to top

Using get method effectively

Posted by: Eunmi Jang on June 07, 2004 in response to Message #124836
I was just wondering , in a enterprise project where Java developer and database DBA guys are two end, How easy to maintain the coulmn number in your code . I would rather use the column name.
I found it is not a problem in practice. You can validate coulmn numbers on application startup automaticaly too.
When you need to change your code, you mean you don't need to know the column number and the order? Automaticaly? How do you make it?
I just want to know it.

  Message #124848 Post reply Post reply Post reply Go to top Go to top Go to top

Using get method effectively

Posted by: Juozas Baliuka on June 07, 2004 in response to Message #124843
I was just wondering , in a enterprise project where Java developer and database DBA guys are two end, How easy to maintain the coulmn number in your code . I would rather use the column name.
I found it is not a problem in practice. You can validate coulmn numbers on application startup automaticaly too.
When you need to change your code, you mean you don't need to know the column number and the order? Automaticaly? How do you make it? I just want to know it.
There are a few ways, the most simple way is to use static data structure (bean) and utils to populate it.
I use framework to do this, it can validate all queries before to start application too. But you can use some class for metadata and compare it to resultset metadata retrieved from DB at application startup (it can be generated from DB metadata too).

 rs.getString( Metadata.EMAIL_INDEX );


This progamming style works good in practice too.

 int index = 1;
 email = rs.getString( index ++ );
 name = rs.getString( index ++ );

It works because you add fields in the most of cases.

  Message #124850 Post reply Post reply Post reply Go to top Go to top Go to top

Pagination

Posted by: Mileta Cekovic on June 07, 2004 in response to Message #124837
In general, do not write code that relies on the number of result rows from a query because drivers must fetch all rows in a result set to know how many rows the query will return.
What is the best way to do pagination (in web pages) without relying on number of rows?
You do not relying on number of rows for pagination if you use something like "LIMIT/OFFSET".
And you keep the full list in the session or application context and the pagination (and sort order) state in the session context. If you keep the full list in the application context keep in mind that if can be accessed by multiple users.

MC

  Message #124904 Post reply Post reply Post reply Go to top Go to top Go to top

When will TheServerSide allow post editing ?

Posted by: Andrew Stevens on June 07, 2004 in response to Message #124773
Bl..dy typos...
>> I relay
should be: 'I realy'
should be: 'I really' ;-)

  Message #124970 Post reply Post reply Post reply Go to top Go to top Go to top

Pagination

Posted by: Marcin Szczepanski on June 07, 2004 in response to Message #124837
In general, do not write code that relies on the number of result rows from a query because drivers must fetch all rows in a result set to know how many rows the query will return.
What is the best way to do pagination (in web pages) without relying on number of rows?
You do not relying on number of rows for pagination if you use something like "LIMIT/OFFSET".
You can do next/previous buttons using this method, but you wouldn't be able to do a "paging" thing where you've got links for 1-10, 11-20, etc along the bottom without knowing the total number of rows.

I guess what this is trying to say is not do get a recordset and get the number of rows from it to determine how many rows you have, instead do a "select count(foo) ..." as that won't result in the data coming down the pipe.

  Message #125008 Post reply Post reply Post reply Go to top Go to top Go to top

PreparedStatement to do insert not always the best technique

Posted by: kjlkjlkj lkjlkjlkj on June 08, 2004 in response to Message #124723
On Oracle, using PreparedStatement like in the following example might not be performant if the table in which you insert has a lot of columns (whatever you use addBatch or not)

PreparedStatement ps = conn.prepareStatement(
   "INSERT into employees values (?, ?, ?)");

for (n = 0; n < 100; n++) {

  ps.setString(name[n]);
  ps.setLong(id[n]);
  ps.setInt(salary[n]);
  ps.addBatch();
}
ps.executeBatch();

Indeed, we experienced performance degrade using this technique on tables with 50 or more colums (in our case, these tables were storing banking products, so it was normal that we had so many business columns in one table). The time spent in all the set... method calls was the bottleneck.

It was more performant to use Statement and litteral values in combination with addBatch when inserting in these tables.

  Message #125056 Post reply Post reply Post reply Go to top Go to top Go to top

Retrieving Auto-Generated Keys

Posted by: Dirk Ludwig on June 08, 2004 in response to Message #124723
Maybe I'm a bid simple minded, but I don't see why anyone would create tables without primary keys. If that's really the case, I'm sure you have other problems than performance.

Further, the solution explained in the article only works (in my opinion) if you implement the DB access directly using JDBC. Using, for example, CMP Entity Beans, this approach does not work, since you have no real control over the statement used to create the dataset. I wonder if that's also true for O/R mapping tools (TopLink, Cocobase, Hibernate, etc). Does anyone have experience with that?

Regards,
    Dirk

  Message #125122 Post reply Post reply Post reply Go to top Go to top Go to top

Bummed about PreparedStatements

Posted by: tr mo on June 08, 2004 in response to Message #124723
Maybe parameterized queries have a lot of overhead to compile, but composing queries with all that quote junk is awful. Trying to compose a given vendor's proper date query format is irritating enough alone; a simple call to setDate() (or better yet, setObject()) is so much easier and totally cross-platform. And there's issues with decimals, etc., etc....

Furthermore, composing sql "by hand" (at least theoretically) opens you up to attacks like somebody inserting raw sql into a user interface query field and fooling your code into adding it to the end of a query.

It seems like JDBC should provide a single-execution-optimized PreparedStatement that can take advantage of all the benefits of PreparedStatement.setObject() without the overhead issues.

Then again, I wonder if JDBC drivers/databases can gain any performance benefit when the same query is passed many times to Connection.prepareStatement() when using a JDBC 3.x connection pool? It isn't really feasible to hang onto statements & connections between http requests in a server-side app like I am building.

Either way, my stuff is fast enough that I doubt I'll stop using PreparedStatement. I'd sooner upgrade all the CPU's and buy a new network hub.

New content on TheServerSide.comNew content on TheServerSide.comNew content on TheServerSide.com

Dependency Injection in Java EE 6 - Part 1

Reza Rahman explores the features of the proposed JSR 299, Contexts and Dependency Injection for Java EE (CDI). When approved, it promises to be a key feature of Java EE 6. (November 2, Article)

SAML: It's Not just for Web services

SAML is an XML-based standard for exchanging authentication and authorization data between security domains. The single most important problem that SAML was created to solve is the Web browser Single Sign-On problem. Many organizations are debating whether to stay with version 1.1 or move to 2.0. This article makes observations about both options. (September 28, Article)

Programming is Also Teaching Your Team

Joe Ottinger takes a look at how people learn, and applies it to the practice of programming. He notes that understanding how people learn is an essential part of working in a programming team. (September 22, Article)

Can Java EE Deliver The Asynchronous Web?

Stephen Maryka gave us an article about the Asynchronous Web and posed a number of questions that get examined like an approach to delivering Asynchronous Web capabilities through extensions to existing Java EE technologies. (July 14, Article)

JSF Flex

JavaServer Faces Flex goal is to provide users capability in creating standard Flex components, part of flexSDK which is open sourced through MPL license, as normal JSF components. This article by Ji Hoon Kim will provide an overview of creating a simple multilingual JSF page consisting of JSF Flex tags. (June 29, Article)

The Rules of SOA - A Road to a Successful SOA Implementation

In this session Jeff explores the key characteristics of successful SOA projects. He covers some of the patterns, and anti-patterns, tool sets, and strategies that he himself learned the hard way. Last, he provides a strategy and blueprint for achieving a high likelihood of success in your SOA project. (June 23, Tech Talk)

Ari Zilka Talks About Terracotta 3.1

Ari Zilka, CTO of Terracotta, Inc., talks about the new features in Terracotta 3.1, announced during JavaOne and available now. (June 15, Tech Talk)

Enterprise Application Integration, and Spring

In this Tech Talk, Josh Long explores an integration challenge using Spring Integration and walks through the implementation, employing and expanding on the basic patterns of Enterprise Application Integration to tie together components into a function integration solution, and then demonstrates how Spring Integration helps address the integration requirements. (June 15, Tech Talk)

Google Web Toolkit: An Introduction

In this Tech Talk, David Geary teaches you: The basics of Google Web Toolkit; How to implement Ajax-enabled applications in Java; Internationalization; Hooking into the browser history mechanism; Remote procedure calls. (June 4, Tech Talk)

Just Enough Early Architecture to Guide Development

Jon Kern discusses the best architecture/technical solutions and ensure that they are repeated by all developers. By tackling the architecture up-front in a serial manner, subsequent parallel development will be much more manageable and predictable. (May 28, Tech Talk)

Productive Programmer: On the Lam from the Furniture Police

This keynote describes the frustrations of modern knowledge workers in their quest to actually get some work done, and solutions for how to guard yourself against all those distractions. Neal Ford talks about environments, coding, acceleration, automation, and avoiding repetition as ways to defeat the misguided attempts to sap your ability to produce good work. (May 26, Tech Talk)

Auto-Scaling Your Existing Web Application

Gil demonstrates how new, aggressive uses of already abundant compute capacity by common applications offer competitive value for application designers. (May 21, Tech Talk)

Automating Hibernate Mapping and Queries For Java Web Development

Chris Keene introduces WaveMaker as a new way to automate the ability to generate Hibernate classes in order to more quickly bring OR mapping into an application. (May 19, Article)

Auto-Scaling Your Existing Web Application

In this session Nati Shalom demonstrates how to take a standard Java EE web application and scale it out or down dynamically without changes to the application code. Seeing as most web applications are over-provisioned to meet infrequent peak loads, this is a dramatic change because it enables growing your application as needed, when needed, without paying for unutilized resources. (May 19, Tech Talk)

Free Book: Jakarta-Struts Live

Download the entire book of Jakarta-Struts Live and learn about Struts MVC, Tiles, the Validator, DynaActionForms, plug-ins, internationalization, and more.
(Book PDF Download)

Application Server Matrix

The Application Server Matrix is a detailed listing of J2EE vendors and their application server products, with information on latest version numbers, J2EE spec support and licensing, pricing, platform support, and links to product downloads and reviews.
(Application Server Comparison Matrix)

News | Blogs | Discussions | Tech talks | Patterns | Reviews | White Papers | Downloads | Articles | Media kit | About
Java Solutions
All Content Copyright ©2007 TheServerSide Privacy Policy
Site Map