Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 09:43 AM
Member
 
Join Date: Jul 2007
Posts: 2
liorb is on a distinguished road
PredifinedStatment is too slow for me?
Hi, I have a performance issue with PreparedStatement, my code is attached,it is very simple, it inserts simple beans into a DB where each bean is a line in the DB.
I tested it 1420 beans, it took around 2 minutes no matter How I did it.
I'm using tomcat5.0 and Mysql5.0.

I tried both executing the PS each time the loop runs (execution for each bean), here I'm trying to batch every 100 beans, execute them and then move to the next 100. it seems like a lot of time is spent preparing the statement while inserting the data into the DB is relatively fast.

I'm relatively new to java so please feel free to comment and tell me what I might be doing wrong, thanks in advance, Lior.
Code:
public static boolean insertList(ArrayList dwhBeabList, String logFileName) throws Exception { Connection conn = null; PreparedStatement ps = null; ArrayList failedBeans = new ArrayList(); try{ if(dwhBeabList != null){ conn = DBManager.getConnection(); StringBuffer query = new StringBuffer(); query.append("insert into "+TABLE_NAME+"("); query.append("full_log_id, "); /***following insertion 18 fields into the query***/ query.append("log_file_name, db_insert_time) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); /**attach the query into the PS**/ ps = conn.prepareStatement(query.toString()); DWHBean dwhBean = null; logger.error("srating insering the beans in to the DB"); int dwhBeanCounter = 0; /**around 2 minutes are spent inside this loop***/ for(int i = 0; i< dwhBeabList.size(); i++){ try{ dwhBean = (DWHBean) dwhBeabList.get(i); if(dwhBean != null){ ps.setString(2,dwhBean.getUserId()); /**following insertion of 18 params from the bean into the PreparedStatment**/ /**Here the beans properties are inserted into a batch, this takes 15 seconds for 100 beans**/ if(dwhBeanCounter<99){ /**it takes 3 seconds to actually insert the data from 100 beans into the DB**/ ps.addBatch(); dwhBeanCounter++; }else{ ps.executeBatch(); ps.clearBatch(); dwhBeanCounter=0; } } }catch(Exception ex){ } } if(dwhBeanCounter<99 && dwhBeanCounter >0){ ps.executeBatch(); } } }

Last edited by liorb : 07-31-2007 at 12:12 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-31-2007, 05:53 PM
Member
 
Join Date: Jul 2007
Posts: 2
liorb is on a distinguished road
I found something that might explain my problem, the question now is, what alternatives can I use?

Some databases don't support the concept of prepared statements in on way or another. What happens in these cases is that the JDBC driver itself actually does the work of the PreparedStatement itself. This means that what happens is that the driver parses the statement, does the parameter binding for you and then when you execute it actually sends a regular old SQL statement (with the formatting it has done for you) to the database. As a popular example of a database that does this MySQL would be one.




About PreparedStatement's performance
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
slow JasperReport bbq Advanced Java 3 02-11-2008 09:34 AM
eclipse very slow katie Eclipse 2 11-05-2007 11:20 AM
Oracle Resultset slow Peter Database 2 07-04-2007 02:56 PM
Export query result, procces is to slow Daniel Enterprise JavaBeans 2 06-28-2007 07:35 PM
Slow reports in Netbeans Alan NetBeans 1 05-29-2007 04:47 PM


All times are GMT +3. The time now is 09:46 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org